×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Bluetooth problem: Service discovery does never complete

    I'm trying to develop a Java application that connects to a device using a LMX9838 IC. This IC implements SPP profile (RFCOMM/L2CAP). In my Linux PC I'm able to connect to it with these two simple commands:

    Code:
    doragasu@doragasu-C2D:~$ hcitool scan
    Scanning ...
    	10:00:E8:AE:1F:36	KuroneKo
    doragasu@doragasu-C2D:~$ sudo rfcomm connect 0 10:00:E8:AE:1F:36 1
    Connected /dev/rfcomm0 to 10:00:E8:AE:1F:36 on channel 1
    Press CTRL-C for hangup
    ^CDisconnected
    Now I'm trying to connect using a Java App with my Nokia 5800. I'm using the client example from here. I have only done two modifications:

    1.- Modified Major Device Class filter because my SPP device has Major Device Class = 0:
    Original:
    Code:
        if ((majorDC == 0x0100) || (majorDC == 0x0200))
          deviceList.addElement(dev);
    Modified:
    Code:
        if (majorDC == 0)
          deviceList.addElement(dev);
    2.- UUID for service scan:
    Original:
    Code:
        UUID[] uuids = new UUID[2];   // holds UUIDs used in the search
    
        /* Add the UUID for RFCOMM to make sure that the matching service
           support RFCOMM. */
        uuids[0] = new UUID(0x0003);
    
        // add the UUID for the service we're looking for
        uuids[1] = new UUID(UUIDStr, false);
    Modified:
    Code:
        UUID[] uuids = new UUID[1];
    
        uuids[0] = new UUID("1101", true);
    The original code works perfectly using the emulator, but both original and modified code doesn't work in the phone. Devices are properly discovered, but service discovery doesn't work, servicesDiscovered() and serviceSearchCompleted() methods ARE NEVER CALLED in the phone.

    I have also tried with other UUIDs like uuids[0] = new UUID("0000110100001000800000805F9B34FB", false); but nothing works.

    I'm a bit confused about the UUID. What UUID should I use? After some googling it looks like some people report that you have to use 128 bit UUID, but I can't get anything to work. Also it looks like there are more people with this problem but I have not found a solution.

    Any suggestions would be greatly appreciated.

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Bluetooth problem: Service discovery does never complete

    Hi,

    It is indeed very interesting that the application runs OK in the emulator but not on the phone. Are you using the Nokia S60 5th Edition SDK?

    Your problem could be non-UUID related.

    The servicesDiscovered can only be called if you have correctly implemented the DiscoveryListener, i.e. you have retrieved the local device, then you have retrieved the discovery agent from the local device and then you use the startInquiry method of your discovery agent object.

    For example you could write something like this:

    yourDiscoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);

    where this, in this case is the current class that needs to implement the DiscoveryListener interfece and therefore your custom servicesDiscovered and servicesSearchCompleted.

    Are you using GIAC or LIAC?

    Can you try to see how the echo example implements the Bluetooth connection and test it on your device? You can download it from here:
    http://library.developer.nokia.com/i...86C3A978A.html
    Last edited by skalogir; 2011-10-11 at 11:48.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    Hi,

    Thanks a lot for help.

    I'm not using a Nokia SDK. I'm developing my program using Netbeans + Java Wireless Toolkit 2.5.2 in a Linux box. You don't need a Nokia specific SDK to develop Java applications for nokia phones, do you? I'm a newbie in the J2ME world, so I could easily be wrong.

    This is the untouched "ServiceFinder" class from the example I'm using:

    Code:
    // ServiceFinder.java
    // Andrew Davison, ad@fivedots.coe.psu.ac.th, August 2005
    
    /* Create a Bluetooth discovery agent, and carry out a devices
       search followed by services search.
    
       Each matching device must be a PC or phone.
       The devices are stored in deviceList.
    
       Each of the service searches are carried out sequentially, 
       one at a time.
    
       A matching service must have the same UUID as that specified in 
       UUIDStr, and the same service name as srchServiceName. It must 
       use the RFCOMM protocol.
    
       Each service record is stored along with its device name in the 
       serviceTable hashtable, using the device name as the 
       key, the service record as the value.
    
       At the end, the hashtable is passed to the midlet via the
       midlet's showServices() method.
    */
    
    import java.io.*;
    import java.util.*;
    import javax.microedition.io.*;
    import javax.bluetooth.*;
    
    
    public class ServiceFinder implements DiscoveryListener
    {
      private EchoClientMIDlet ecm;
      private String UUIDStr;           // the UUID of the desired service
      private String srchServiceName;   // the name of the desired service
    
      private DiscoveryAgent agent;
    
      // stores the remote devices found during the device search
      private Vector deviceList;
    
      /* table of matching services, stored as pairs of the form
         {device name, serviceRecord} */
      private Hashtable serviceTable;
      private boolean searchDone;
    
    
    
      public ServiceFinder(EchoClientMIDlet ecm, String uuid, String nm)
      // create a discovery agent then perform device and services search.
      {
        this.ecm = ecm;
        UUIDStr = uuid;
        srchServiceName = nm;
    
        try {   
          // get the discovery agent, by asking the local device
          LocalDevice local = LocalDevice.getLocalDevice();
          agent = local.getDiscoveryAgent();
    
          // initialize device search data structure
          deviceList = new Vector();
    
          // start the searches: devices first, services later
          ecm.setStatus("Searching for Devices...");
          System.out.println("Searching for Devices...");
          agent.startInquiry(DiscoveryAgent.GIAC, this);   // non-blocking
        }
        catch (Exception e) {
          System.out.println(e);
          ecm.searchError("Search Error");
        }
      } // end of ServiceFinder()
    
    
     // -------------------------- device search methods ---------------
    
      /* deviceDiscovered() and inquiryCompleted() are called 
         automatically during the device search initiated by the 
         DiscoveryAgent.startInquiry() call.
      */
    
    
      public void deviceDiscovered(RemoteDevice dev, DeviceClass cod)
      /* A matching device was found during the device search.
         Only store it if it's a PC or phone. */
      {
        System.out.println("Device Name: " +  getDeviceName(dev)); 
    
        int majorDC = cod.getMajorDeviceClass();
        int minorDC = cod.getMinorDeviceClass();   // not used in the code
        System.out.println("Major Device Class: " + majorDC + 
                         "; Minor Device Class: " + minorDC);
        
        // restrict matching device to PC or Phone
        if ((majorDC == 0x0100) || (majorDC == 0x0200))
          deviceList.addElement(dev);
        else
          System.out.println("Device not PC or phone, so rejected");
      } // end of deviceDiscovered()
    
    
      private String getDeviceName(RemoteDevice dev)
      /* Return the 'friendly' name of the device being examined,
         or "Device ??" */
      {
        String devName;
        try {
          devName = dev.getFriendlyName(false);  // false to reduce connections
        }
        catch (IOException e) 
        { devName = "Device ??";  }
        return devName;
      }  // end of getDeviceName()
    
    
      public void inquiryCompleted(int inqType)
      // device search has finished; start the services search
      {
        showInquiryCode(inqType);
        System.out.println("No. of Matching Devices: " + deviceList.size());
    
        // start the services search
        ecm.setStatus("Searching for Services...");
        searchForServices(deviceList, UUIDStr);
      } // end of inquiryCompleted()
    
    
      private void showInquiryCode(int inqCode)
      {
        if(inqCode == INQUIRY_COMPLETED)
          System.out.println("Device Search Completed");
        else if(inqCode == INQUIRY_TERMINATED)
          System.out.println("Device Search Terminated");
        else if(inqCode == INQUIRY_ERROR)
          System.out.println("Device Search Error");
        else 
          System.out.println("Unknown Device Search Status: " + inqCode); 
      }  // end of showResponseCode()
    
    
      // --------------------- service search methods --------------------
      
    
      private void searchForServices(Vector deviceList, String UUIDStr)
      /* Carry out service searches for all the matching devices, looking
         for the RFCOMM service with UUID == UUIDStr. Also check the
         service name.
      */
      {
        UUID[] uuids = new UUID[2];   // holds UUIDs used in the search
    
        /* Add the UUID for RFCOMM to make sure that the matching service
           support RFCOMM. */
        uuids[0] = new UUID(0x0003);
    
        // add the UUID for the service we're looking for
        uuids[1] = new UUID(UUIDStr, false);
    
        /* we want the search to retrieve the service name attribute,
           so we can check it against the service name we're looking for */
        int[] attrSet = {0x0100};
    
        // initialize service search data structure
        serviceTable = new Hashtable();
    
        // carry out a service search for each of the devices
        RemoteDevice dev;
        for (int i = 0; i < deviceList.size(); i++) {
          dev = (RemoteDevice) deviceList.elementAt(i);
          searchForService(dev, attrSet, uuids);
        }
    
        // tell the top-level MIDlet the result of the searches
        if (serviceTable.size() > 0)
          ecm.showServices(serviceTable);
        else
          ecm.searchError("No Matching Services Found");
      } // end of searchForServices()
    
    
      private void searchForService(RemoteDevice dev, int[] attrSet, 
                                                         UUID[] uuids)
      // search device for a service with the desired attribute and uuid values
      {
        System.out.println("Searching device: " + getDeviceName(dev));
        try {
          int trans = agent.searchServices(attrSet, uuids, dev, this); // non-blocking
          waitForSearchEnd(trans); 
        }
        catch (BluetoothStateException e) {
          System.out.println(e);
        }
      }  // end of searchForService()
    
    
    
      private void waitForSearchEnd(int trans)
      // wait for the current service search to finish
      {
        System.out.println("Waiting for trans ID " + trans + "...");
        searchDone = false;
        while (!searchDone) {
          synchronized (this) {
            try {
              this.wait();
            }
            catch (Exception e) {}
          }
        }
        System.out.println("Done");
      }  // end of waitForSearchEnd()
    
    
      /* servicesDiscovered() and serviceSearchCompleted() are called 
         automatically during a services search initiated by a
         DiscoveryAgent.searchServices() call. We call it from
         searchForService().
      */
    
      public void servicesDiscovered(int transID, ServiceRecord[] servRecords)
      /* Called when matching services are found on a device. 
         The service record is only stored if its name matches the one
         being searched for (srchServiceName).
    
         The service record is stored with the device name in the serviceTable
         hashtable, using the device name as the key, the service record as the
         value.
      */
      {
        for (int i=0; i < servRecords.length; i++) {
          if (servRecords[i] != null) {
            // get the service record's name
            DataElement servNameElem = servRecords[i].getAttributeValue(0x0100);
            String servName = (String)servNameElem.getValue();
            System.out.println("Name of Discovered Service: " + servName);
    
            if (servName.equals(srchServiceName)) {  // check the name
              RemoteDevice dev = servRecords[i].getHostDevice();
              serviceTable.put( getDeviceName(dev), servRecords[i]); // add to table
            }
          }
        }
      } // end of servicesDiscovered()
    
    
      public void serviceSearchCompleted(int transID, int respCode)
      // Called when the service search has finished
      {
        showResponseCode(transID, respCode);
    
        /* Wake up waitForSearchEnd() for this search, allowing the next
           services search to commence in searchForServices(). */
        searchDone = true;
        synchronized (this) {  
          this.notifyAll();  // wake up
        }
      } // end of serviceSearchCompleted()
    
    
    
      private void showResponseCode(int transID, int respCode)
      {
        System.out.print("Trans ID " + transID + ". ");
    
        if(respCode == SERVICE_SEARCH_ERROR)
          System.out.println("Service Search Error");
        else if(respCode == SERVICE_SEARCH_COMPLETED)
          System.out.println("Service Search Completed");
        else if(respCode == SERVICE_SEARCH_TERMINATED)
          System.out.println("Service Search Terminated");
        else if(respCode == SERVICE_SEARCH_NO_RECORDS)
          System.out.println("Service Search: No Records found");
        else if(respCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
          System.out.println("Service Search: Device Not Reachable");   
        else 
          System.out.println("Unknown Service Search Status: " + respCode); 
      }  // end of showResponseCode()
    
    
    } // end of ServiceFinder class
    The example works perfect in the emulator, but I have tested it using two 5800 phones and client doesn't work, it finds the device, but is unable to find services in it.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    The servicesDiscovered can only be called if you have correctly implemented the DiscoveryListener, i.e. you have retrieved the local device, then you have retrieved the discovery agent from the local device and then you use the startInquiry method of your discovery agent object.
    The code retrieves the local device and local agent, and also starts inquiry using GIAC:

    Code:
          LocalDevice local = LocalDevice.getLocalDevice();
          agent = local.getDiscoveryAgent();
    
    [...]
    
          agent.startInquiry(DiscoveryAgent.GIAC, this);   // non-blocking
    Then searchs for devices, retrieves the remote device and starts searching services:

    Code:
    int trans = agent.searchServices(attrSet, uuids, dev, this); // non-blocking
    Neither servicesDiscovered() nor serviceSearchCompleted() are called when executing in the phone (even though it works flawlessly in the emulator).

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    I have found more people with the same problem and no solutions. Maybe a bug in Nokia Java interpreter?

    http://www.developer.nokia.com/Commu...vice-discovery
    http://www.developer.nokia.com/Commu...pp.-uuid-doubt

    I have also found this: http://stackoverflow.com/questions/2...t-discoverable
    I'll try to test the delay trick and see what happens but since my program isn't exiting due to an uncaught exception, I doubt this is the problem.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    Tried adding 5 s delay before searching for services, but no luck. It simply doesn't work

  7. #7
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Bluetooth problem: Service discovery does never complete

    Hello,

    It's highly recommended to use the appropriate Nokia SDK in order to compile and test your Midlets on your Nokia phone.
    Can you provide the entire code for both the Discovery and Advertising of Services (client and server)?

    Alternatively can you try to download and run the BT client and server examples from here:

    http://www.developer.nokia.com/Commu...ice_in_Java_ME
    http://www.developer.nokia.com/Commu...ces_in_Java_ME

    and see if that example works on your phone?

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    I have tested the midlet with a SonyEricsson W715 and service discovery is working with this phone, so it looks like there's a problem with Nokia implementation of the J2ME java interpreter. Maybe compiling with Nokia SDK can solve the problem.

    Thanks skalogir for suggestions, I'll post code and try them as soon as possible. BTW, Can Nokia J2SE SDK be run under Linux? I have browsed SDK tools and it looks like there is a Nokia SDK for Java (suitable only for Series 40 phones), and Symbian SDK (including java tools) that looks like windows only :-(

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    I got access to a Windows PC and installed Netbeans + S50 v5 SDK. When I try to debug using the emulator, the emulator starts but my application never starts. If I upload the application compiled with S60 SDK to my 5800, I get the same result I got with WTK: Application starts, finds devices and never ends searching for services.

    You can find the code I'm using for server and client here: http://fivedots.coe.psu.ac.th/~ad/jg/blue1/blueEcho.zip

    Bluetooth stuff for the client is located in class "ServiceFinder".

    I'm going to try with BT client and server examples skalogir pointed.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    I think there's definitely a problem with Nokia Java VM for 5800 devices. I have tested this example:

    http://www.developer.nokia.com/Commu...ces_in_Java_ME

    And it behaves exactly as my code. It finds devices, but when I start services search it prints:

    "Start services under L2CAP searching..."

    And it stays there forever :-(

    Should I report this as a bug?

    Also, is it possible to connect to a server skipping the service search?

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Re: Bluetooth problem: Service discovery does never complete

    Hi!
    "service discovery does never complete" Do you think that the same problem I can have with my mobile nokia n97 mini?? inquiryCompleted never ends when another device has got activated bluetooth :/ Any solutin? Or its a nokia's bug??

  12. #12
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: Bluetooth problem: Service discovery does never complete

    Hello doragasu and marko2marko,

    regarding the issues, in case there are no reponses given from the discussion community you might want to try the following services by Nokia Developer:

    -technical support
    -bug reporting

    The services can be found from online: https://www.developer.nokia.com/Reso..._support.xhtml

    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

  13. #13
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Bluetooth problem: Service discovery does never complete

    Hi doragasu,

    If the example codes I have provided:

    http://www.developer.nokia.com/Commu...ice_in_Java_ME
    http://www.developer.nokia.com/Commu...ces_in_Java_ME

    do not work, then you need to submit a platform bug report here:
    http://www.developer.nokia.com/Resou..._support.xhtml

    Please before you do, make sure that
    1) You have two phones, one running the advertising example (server) and another running the discovering example (client)

    2) You start the server (Options>Start)

    3) You accept all notifications on the phone's screen
    4) You run the client
    5) You select discover devices on the client
    6) You wait until the discovery of devices is completed (a notification will appear on the screen)
    7) You select the device from the list of discovered devices, on which the server is running

    The client then should send a simple message to the server and terminate the connection.

    Unfortunately Nokia's SDKs for Java Development run on Windows only.

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Re: Bluetooth problem: Service discovery does never complete

    Hi! Thanks for this post! It works very vell... (On my device: n97mini Discover Found Nokia N80) So probably I need to find the bug in my java code :/ Thanks!!!

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Re: Bluetooth problem: Service discovery does never complete

    Hi all,

    I have found a workaround: to skip the services search step. I do a device search and assume the found devices have the RFCOMM service. I manually build the URL and connect to the device. It works perfect this way, but definitely I would like to do it the right way.

    I'll try to follow all the steps skalogir pointed and if I don't get it to work, I'll send a platform bug report.

    Again, thanks for help.

Similar Threads

  1. problem about service discovery of bluetooth
    By andy205214 in forum Symbian
    Replies: 2
    Last Post: 2010-01-21, 16:14
  2. Bluetooth Obex service discovery problem
    By furrbie in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2008-08-19, 04:33
  3. Bluetooth Service Discovery Problem
    By maverick2k in forum Mobile Java General
    Replies: 3
    Last Post: 2007-12-17, 12:08
  4. Bluetooth Service Discovery Problem
    By rwan067 in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2007-03-18, 14:32
  5. Nokia SDK Bluetooth service re-discovery problem, Help Me!:(
    By tomsonxu in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2005-11-08, 07:39

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •