×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Cannot find where the issue is, Bluetooth service discovery

    Hi there,

    I am developing a client/server bluetooth messaging application and seem to have to run into a brick wall (Am testing the application using a Nokia N73 and and N81). I am able to perform a device discovery, and the client is able to locate the server, however I am not able to locate the service on the server even though the server and the client are both using the same UUID and service name.

    It gets to the point where it displays the status message, "Service search initiated" and then it seems like nothing else happens. I have been staring at the code for days and tried all sorts of tinkering, but have been unable to get it to work.

    The UUID and service name I am using is:

    Code:
    48dd1cf559bb41009d0686f7862d26a2
     
    server
    and below is the code for my class that I implement the device and services discovery in:

    Code:
    import javax.microedition.io.*;
    import java.util.*;
    import java.io.*;
    import javax.bluetooth.*;
     
    public class SearchForServices implements DiscoveryListener
    {
        private MessageClient client;
        private String StrUUID; //UUID of service
        private String nameOfService; // Name of service
        private LocalDevice local; //local device
        
        //Discovery Agent
        private DiscoveryAgent discover;
        
        //store list of found devices
        private Vector devicesFound;
        
        //table of matching services/ device name & service Record
        private Hashtable servicesFound;
        private boolean searchComplete;
        private boolean terminateSearch;
        
        public SearchForServices(MessageClient client, String uuid, String nameOfService)
        {
            //create the discovery listener and then perform device and services search
            this.client = client;
            this.StrUUID = uuid;
            this.nameOfService = nameOfService;
             //create service search data structure
            this.servicesFound = new Hashtable();
            
            try
            {
                //get discovery agent
                local = LocalDevice.getLocalDevice();
                discover = local.getDiscoveryAgent();
                
                //create storage for device search
                devicesFound = new Vector();
                
                //begin search: first devices, then services
                this.client.modifyStatus("Searching for devices...");
                discover.startInquiry(DiscoveryAgent.GIAC, this);
            }
            catch(Exception e)
            {
                this.client.reportError("Unable to perform search");
            }
        }
        
        /////////////Methods related to the device search called automatically///
        
        public void deviceDiscovered(RemoteDevice remote, DeviceClass rank)
        {
            // a matching device is found.Only store if it's a PC or phone
            int highRankDevice = rank.getMajorDeviceClass();
            int lowRankDevice = rank.getMinorDeviceClass();
            
            //restrict devices
            if((highRankDevice == 0x0100) || (highRankDevice == 0x0200))
            {
                devicesFound.addElement(remote);
                this.client.modifyStatus("Device Found");
            }
            else
            {
                this.client.reportError("Matching device not found");
            }
        }
        
        private String deviceName(RemoteDevice remote)
        {
            String name = null;
            try
            {
                name = remote.getFriendlyName(false);            
            }
            catch(IOException e)
            {
                this.client.modifyStatus("Unable to get Friendly name");
            }
            return name;
        }
        
        public void inquiryCompleted(int inquiryMode)
        {
            showInquiryStatus(inquiryMode);
            //update status
            this.client.modifyStatus("Number of devices found: " + devicesFound.size());
            
            // start searching for services
            this.client.modifyStatus("Service search initiated");
            findServices(devicesFound, this.StrUUID);
        }
        
        private void showInquiryStatus(int inquiryMode)
        {
            if(inquiryMode == INQUIRY_COMPLETED)
            {
                this.client.modifyStatus("Device Search Completed");
            }
            else if(inquiryMode == INQUIRY_TERMINATED)
            {
                this.client.modifyStatus("Device Search Terminated");
            }
            else if(inquiryMode == INQUIRY_ERROR)
            {
                this.client.modifyStatus("Error searching for devices");
            }       
            
        }
        
        //service search////
        private void findServices(Vector devFound, String strUuid)
        {
            //Perform search for services that have a matching UUID
            //and also check service name
            
            UUID[] uuids = new UUID[1]; //holds UUIDs for searching        
            //add the one for the service in question
            uuids[0] = new UUID(strUuid, false);
            
            //to include search for service name attribute
            int[] attributes = new int[1];
            attributes[0] = 0x100;       
           
            
            //carry out service search for each device
            //terminate search
            this.terminateSearch = false;
            RemoteDevice xremote;
            for(int i=0; i < devFound.size(); i++)
            {
                xremote = (RemoteDevice)devFound.elementAt(i);
                findService(xremote, attributes, uuids);
                
                if(terminateSearch)
                {
                    break;
                }
            }
            
            //report status
            if(servicesFound.size() > 0)
            {
                this.client.displayServices(servicesFound);
            }
            else
            {
                this.client.reportError("No Matching services found");
            }
        }
        
        private void findService(RemoteDevice remote, int[] attributes, UUID[] uuid)
        {
            try
            {
                int transaction = this.discover.searchServices(attributes, uuid, remote, this);       
                searchFinished(transaction);
            }
            catch(BluetoothStateException e)
            {
                
            }
        }
        
        private void searchFinished(int transaction)
        {
            this.searchComplete = false;
            while(!searchComplete)
            {
                synchronized(this)
                {
                    try
                    {                    
                        this.wait();
                    }
                    catch(Exception e)
                    {
                        
                    }
                }
            }
        }
        
        //below methods called automatically during a search for devices
        public void servicesDiscovered(int transID, ServiceRecord[] records)
        {
            for(int i=0; i < records.length; i++)
            {
                if(records[i] != null)
                {
                    //get service records name
                    DataElement servNameElem = records[i].getAttributeValue(0x0100);
                    String sName = (String)servNameElem.getValue();
                    //terminate search
                    this.terminateSearch = true;
                    if(sName.equals(this.nameOfService)) //check name
                    {
                        RemoteDevice rem = records[i].getHostDevice();
                        servicesFound.put(deviceName(rem), records[i]); //add to hashtable
                    }
                }
            }
        }
        
        public void serviceSearchCompleted(int transID, int respCode)
        {
            //wake up the waiting thread for this search, enabling the next services
            //search to commence
            this.searchComplete = true;
            synchronized(this)
            {
                this.notifyAll();
            }
        }
        
     
    }
    After doing abit of troubleshooting, I realised that somehow the client is not able to locate the service on the Server, but I do not understand why that should be so, because the UUID and service name are matching on both the client and the server. It just beats me, and I would appreciate it if a 2nd pair of eyes could have a look at the code. Maybe there is something that I am missing.

    I tried a few things such as

    1) Changing the UUID for my application, but that didn't make the system work.

    2) I then went on to modify the implementation and use the selectService() method of the bluetooth class, but still without any luck.

    The only way it works, is if I get the server side to display the connection parameters and I manually enter them into the client side to connect. I do not understand why it is not working the automatic way. I have scanned through my code and logic multiple times, and still cannot figure out what the issue is.

    Any help would be appreciated...

    Thanks

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

    Re: Cannot find where the issue is, Bluetooth service discovery

    Hello newbietoprofessional,

    check this Technical Solution in Forum Nokia Wiki for a possible workaround:

    http://wiki.forum.nokia.com/index.ph...tring_value%3F

    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]

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Re: Cannot find where the issue is, Bluetooth service discovery

    Hello r2j7,

    I do appreciate your response. I was beginning to wonder if I was ever going to get a response.

    Thanks alot for the link you provided; I had a look through the content. However, that workaround can only be applied if the services have actually been found.

    However in my case, the client cannot even detect the service. There is something I discovered, I do not know if its normal, or its peculiar. No matter what UUID I use, when I manually acquire the connection URL from the server end, its always the same.

    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    19

    Re: Cannot find where the issue is, Bluetooth service discovery

    Hi,
    I'm having the same problem than you. My source code worked on several Nokia, and not on the phone which finally interest me.
    I already saw the link given here. But it doesn't work better that way.
    Can it be a problem from the phone and not from a range of phone?

    Thanks

Similar Threads

  1. If you cannot find BT service on your phone
    By krampuls in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2007-03-22, 11:42
  2. Bluetooth Service Discovery Problem
    By rwan067 in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2007-03-18, 14:32
  3. 6680 and bluetooth service profiles
    By ceruault in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-10-08, 22:24
  4. How to find more bluetooth devices than one in discovery?
    By wudilp in forum Symbian User Interface
    Replies: 0
    Last Post: 2004-03-25, 15:23
  5. How to find more bluetooth devices than one in discovery?
    By Mito1982 in forum Bluetooth Technology
    Replies: 1
    Last Post: 2004-03-25, 15:21

Posting Permissions

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