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:
and below is the code for my class that I implement the device and services discovery in:Code:48dd1cf559bb41009d0686f7862d26a2 server
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.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(); } } }
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


Reply With Quote


