JSR-82 Sample : Device Discovery
- Posted by Bruse
This article provides Java sample codes to search for Bluetooth devices using a JSR-82 API. If you are new to JSR-82, we recommend you to read the JSR-82 Basics tutorial to get a basic idea Java and Bluetooth.
JSR-82 provides the class DiscoveryAgent for performing Device Discovery and Service Discovery. The LocalDevice class provides a factory method 'getDiscoveryAgent' that returns a singleton instance of the DiscoveryAgent. This instance can then be used to discover other bluetooth devices and services.
The following code sample shows you how to use the DiscoveryAgent to discover other bluetooth devices. For this sample to work, you need a JSR-82 Implmentation (Java Bluetooth Stack) like ElectricBlue or aveLink in the classpath.
BluetoothDeviceDiscovery.java
-
import java.io.IOException;
-
import java.util.Vector;
-
-
import javax.bluetooth.DeviceClass;
-
import javax.bluetooth.DiscoveryAgent;
-
import javax.bluetooth.DiscoveryListener;
-
import javax.bluetooth.LocalDevice;
-
import javax.bluetooth.RemoteDevice;
-
import javax.bluetooth.ServiceRecord;
-
-
-
/**
-
* Class that discovers all bluetooth devices in the neighbourhood
-
* and displays their name and bluetooth address.
-
*/
-
public class BluetoothDeviceDiscovery implements DiscoveryListener{
-
-
-
//object used for waiting
-
-
//vector containing the devices discovered
-
-
-
//main method of the application
-
-
//create an instance of this class
-
BluetoothDeviceDiscovery bluetoothDeviceDiscovery=new BluetoothDeviceDiscovery();
-
-
//display local device address and name
-
LocalDevice localDevice = LocalDevice.getLocalDevice();
-
-
//find devices
-
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
-
-
agent.startInquiry(DiscoveryAgent.GIAC, bluetoothDeviceDiscovery);
-
-
try {
-
synchronized(lock){
-
lock.wait();
-
}
-
}
-
e.printStackTrace();
-
}
-
-
-
-
//print all devices in vecDevices
-
int deviceCount=vecDevices.size();
-
-
if(deviceCount <= 0){
-
}
-
else{
-
//print bluetooth device addresses and names in the format [ No. address (name) ]
-
for (int i = 0; i <deviceCount; i++) {
-
RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
-
System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(true)+")");
-
}
-
}
-
-
-
}//end main
-
-
//methods of DiscoveryListener
-
-
/**
-
* This call back method will be called for each discovered bluetooth devices.
-
*/
-
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
-
//add the device to the vector
-
if(!vecDevices.contains(btDevice)){
-
vecDevices.addElement(btDevice);
-
}
-
}
-
-
//no need to implement this method since services are not being discovered
-
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
-
}
-
-
//no need to implement this method since services are not being discovered
-
public void serviceSearchCompleted(int transID, int respCode) {
-
}
-
-
-
/**
-
* This callback method will be called when the device discovery is
-
* completed.
-
*/
-
public void inquiryCompleted(int discType) {
-
synchronized(lock){
-
lock.notify();
-
}
-
-
switch (discType) {
-
case DiscoveryListener.INQUIRY_COMPLETED :
-
break;
-
-
case DiscoveryListener.INQUIRY_TERMINATED :
-
break;
-
-
case DiscoveryListener.INQUIRY_ERROR :
-
break;
-
-
default :
-
break;
-
}
-
}//end method
-
}//end class

May 14th, 2007 at 2:16 am
[...] the last article we found how to search for other bluetooth devices. Here we look in to how search for a service on a particular bluetooth [...]
October 2nd, 2007 at 9:43 am
Thanks for a good short well written tutorial - you have helped no end!