Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 6
Registered: ‎11-01-2009
My Device: Not Specified

Automated Device Discovery Problems

Hello,

     I am working on an application that requires a BlackBerry to perform an automated search for all bluetooth devices/discoverable modules in range and connect to one with a specific address. From everything I have read in forums and the BB Java API this should be possible. Please let me know if it is not. With the assumption(based on background research) that it was, I began writing an app to display the bluetooth addresses of devices in range. Using the DiscoveryListener interface and the DiscoveryAgent class I though this would be rather straight forward but now I'm having a bit of difficulty. I am writing my application using the Eclipse Plugin Component Package 4.5.0 and testing it on an 8110 running v4.3.0.93.

 

The basic problem I am having is that when I perform a device search nothing is returned. I have confirmed that the inquiry begins (with the returned boolean from startInquiry)  and when I run the code with my phone linked to the Eclipse debugger the addresses of local devices are printed to the console as,  BT: INQUIRY_RESULT: XX:XX:XX:XX:XX:XX and so on. I have also confirmed that although BT: INQUIRY_COMPLETE is displayed on the console, the method inquiryCompleted is never actually called. 

 

I also noticed that my phone app returns from the device search method that I wrote before any of the information about the inquiry and found devices is printed to the console. This leads me to believe that the phone was not waiting for the search to complete before continuing on with the program. In response to this I tried using a call to wait() to give the inquiry time to complete but the application just hangs with a timed wait (the bluetooth discovery info still prints to the console when debugging though.) The other logical (and preferred)  solution is to notify the object waiting in the inquiryCompleted method, but alas it is never called. 

 

I apoligize for the lengthy description but I figure it is best to be as explicit as possible. Below you will find the code I have written for device discovery. The findDevices() method is called with a simple UI. Thank you in advance for your help.

 

Luke

 

 

 

import java.io.IOException;
import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

public class DeviceSearch implements DiscoveryListener {
	
	//Vector of devices discovered
	Vector devices = new Vector();

	//Vector of discovered Bluetooth device addresses
	Vector devAdd = new Vector();
	
	//Object used to wait
	private static Object lock = new Object();
	
	public Vector findDevices() throws BluetoothStateException{
		
		DeviceSearch searchDevices = new DeviceSearch();
		
		//Get the local device
		LocalDevice localDevice = LocalDevice.getLocalDevice();

		//Get the local device's discovery agent
		DiscoveryAgent localAgent = localDevice.getDiscoveryAgent();
		
		//Begin search for devices
		boolean started = localAgent.startInquiry(DiscoveryAgent.GIAC, searchDevices);
		Boolean str = new Boolean(started);
		//Wait for search to complete
		try {
			lock.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//Once device search is complete, scan each device and extract their addresses
		for(int i=0;i<devices.size();i++){
				devAdd.addElement(((RemoteDevice) devices.elementAt(i)).getBluetoothAddress());
		}
		
		//No devices found, confirm inquiry began
		if(devAdd.size() == 0){
			devAdd.addElement(str.toString());
		}
		
		return devAdd;
	}
	


	public void deviceDiscovered(RemoteDevice discDevice, DeviceClass discDeviceClass) {
		//Add device to devices vector if it is not already stored in the vector
		if(!devices.contains(discDevice)){
			devices.addElement(discDevice);
		}
	}

	public void inquiryCompleted(int discType) {
		//Add element to confirm the inquiry was completed
		devices.addElement("COMPLETED");
		lock.notify();


	}

	public void serviceSearchCompleted(int transID, int respCode) {

	}

	public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
	}

}

 

 

 

 

 

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Automated Device Discovery Problems

Sorry, not looked at your problem in detail, but noted this:

"using the Eclipse Plugin Component Package 4.5.0 and testing it on an 8110 running v4.3.0.93."

 

If you are developing for 4.3, you should develop using Component Pack 4.3 or 4.2.1.  Using a Component Pack that is later than the OS on the phone is not supported.

 

In fact there is a good chance that this makes no difference, but to remove this possibility, could you compile with 4.3 and try it.  Let us know how you get on.

Developer
Posts: 484
Registered: ‎07-17-2008
My Device: Not Specified

Re: Automated Device Discovery Problems

You didn't implement searchcompleted or servicesdiscovered.

New Developer
Posts: 6
Registered: ‎11-01-2009
My Device: Not Specified

Re: Automated Device Discovery Problems

It's amazing the things one overlooks, thank you for pointing that out. After a bit of wrestling with Eclipse I was able to get the 4.3 Component Pack installed. Unfortunately you are correct, it did not make a difference.  I would greatly appreciate it if you could take a quick look at my code and details I have given about the debugging I have already done.  

 

Thanks

New Developer
Posts: 6
Registered: ‎11-01-2009
My Device: Not Specified

Re: Automated Device Discovery Problems

As for searchCompleted and servicesDiscovered, I have implemented them but since I never do a service search they need no content in the body. Perhaps I am missing something?

New Developer
Posts: 6
Registered: ‎11-01-2009
My Device: Not Specified

Re: Automated Device Discovery Problems

I would really appreciate some assistance on this. I have spent the last few days trying to figure out this trivial problem with no luck. I have upgraded to 4.5 on my 8110 and on Eclipse to rule out any bugs in earlier code. To paraphrase my problem, I am trying to discover devices in range but:

 

1. The phone returns to my UI class before the device inquiry is complete

2. Using wait() just causes the program to hang

 

I have confirmed:

1. The inquiry begins and devices are found (shown on console)

2. deviceDiscovered() and inquiryCompleted() are never called

 

Perhaps someone else has encountered this problem or has some sample code to perform device discovery (mine is shown above.) I have exhausted web resources on this topic and tried numerous different device search examples posted on other sites. I know this is a simple problem,  I've put a lot of time into it but just can't seem to figure it out. 

 

Thank you for your time. 

Highlighted
New Contributor
Posts: 6
Registered: ‎12-20-2011
My Device: 9105
My Carrier: null

Re: Automated Device Discovery Problems

I have the same issue. The methods deviceDiscovered and inquiryCompleted are never called from implemented interface DiscoveryListener.

Can you help me? Thanks in advance

Developer
Posts: 467
Registered: ‎07-16-2009
My Device: Z10 LE
My Carrier: Vodafone NL

Re: Automated Device Discovery Problems

[ Edited ]

From the listing I see that you are creating new object of DeviceSearch and whenever search is finished lock in that instance will be notified. But this instance will wait forever because its lock is not invokved to search.

 

To fix - remove creation of new object and put this as listener to search procedure.

 

Regards,

Eugen

New Contributor
Posts: 5
Registered: ‎01-21-2011
My Device: Not Specified

Re: Automated Device Discovery Problems

I'm also having the same issue. I would really appreciate it if someone could provide some insight to the solution of this problem.