JSR-82 Sample : Device Discovery
- Posted by Bruse
- 21 Comments »
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!
January 22nd, 2008 at 11:15 am
when you do
//agent.startInquiry(DiscoveryAgent.GIAC, bluetoothDeviceDiscovery);
you are not adding the found devices to your vector? vecDevices
so how are you looping through the vector and printing the devices???
April 12th, 2008 at 12:18 am
HELP ME!!!! PLZ!
How can I run this code using BlueJ? How can I import microedition?
May 23rd, 2008 at 10:23 am
i want some help ,during compilation of d above code i got dis error:
javax.bluetooth.BluetoothStateException: java.lang.NoClassDefFoundError: com/sun/kvem/jsr082/bluetooth/DiscoveryAgentImpl
how to solve dis pl help me
August 21st, 2008 at 3:42 am
this is the Output when i run it,, why it make an Error..???
--------------------Configuration: new_prj - JDK version 1.6.0_07 - --------------------
BlueCove version 2.0.2 on bluesoleil
Address: 111111111111
Name: winzter
Starting device inquiry...
Device discovered: 001E52E929E1
Device discovered: 001EC2983941
Device discovered: 00219E4EBEBA
INQUIRY_COMPLETED
Device Inquiry Completed.
Bluetooth Devices:
Exception in thread "main" java.io.IOException: Can't query remote device
at com.intel.bluetooth.RemoteDeviceHelper.getFriendlyName(RemoteDeviceHelper.java:215)
at javax.bluetooth.RemoteDevice.getFriendlyName(RemoteDevice.java:131)
at BluetoothDeviceDiscovery.main(BluetoothDeviceDiscovery.java:69)
BlueCove stack shutdown completed
Process completed.
sherwin,
November 16th, 2008 at 1:16 am
Including the line numbers makes examples impossible to copy and paste. Please don't do it.
December 5th, 2008 at 11:22 am
Hello,
Sir i tried this code it can sucessfully find the devices bt gives null pointer exception when we call agent.searchServices(); i tried different uuids set bt yet if you have any solution to it kindly email me i m frustrated
January 2nd, 2009 at 10:16 pm
great tutorial. There's something that bothers me thou:
it works fine on sun's wless toolkit emulator but it doesn't work at all if I deploy it to a nokia n95 8gb.
I'm clueless as to why it doesn't work. No exceptions are thrown.
Is there something I missed?
January 9th, 2009 at 8:11 am
Hello Sir
Its work fine but once the user disconnect the bluetooth connection it didnt refresh.In the sence it also shows that person is connected. help me what i do
Thanks in advance
vanitha
March 23rd, 2009 at 2:40 pm
when i run this code on netbeans, i get an error saying, Exception in thread."main".javax.bluetooth.BluetoothStateException: No supported stack installed or dongle available... what does this mean
March 23rd, 2009 at 2:41 pm
which dongle is compatible with this code?? my final year project needs this... please help me soon
March 23rd, 2009 at 2:47 pm
which is the first one of all the codes that i should run? spp server should be run first or device discovery should be run first?
March 23rd, 2009 at 2:49 pm
how can i run this code using bluesim simulator
???
please help
April 9th, 2009 at 1:08 pm
I installed JM2PC_BT_Client to n3250. After i opened it,information list in the server was "connected". But in my n3250 said "please wait... Authentication". There was no further reaction. The connection was through JSR-82. When I stoped the client, the information list in the server was "Error: java.io.EOFException". Please help me. My email is sigit_satata@yahoo.com
thanks..
May 4th, 2009 at 7:10 am
To Sherwin :
Please get the FriendlyName in the routine deviceDiscovered()and use it later on in System.out.println(...)
September 2nd, 2009 at 2:06 pm
I need help please
Exception in thread "main" javax.bluetooth.BluetoothStateException
at com.sun.kvem.jsr082.bluetooth.LocalDeviceImpl.(LocalDeviceImpl.java:185)
at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:94)
at Emulador.main(Emulador.java:40)
September 6th, 2009 at 4:51 pm
pls help
I get a NullPointerException on LocalDevice.getLocalDevice
November 11th, 2009 at 10:29 am
i hace xp2, java 1.6, and java me platform 3.0,,
how to use this program.
i gone burst at trying this program to run
January 16th, 2010 at 7:17 pm
Exception in thread "main" javax.bluetooth.BluetoothStateException: BluetoothSta
ck not detected
at com.intel.bluetooth.BlueCoveImpl.detectStack(BlueCoveImpl.java:476)
at com.intel.bluetooth.BlueCoveImpl.access$500(BlueCoveImpl.java:65)
at com.intel.bluetooth.BlueCoveImpl$1.run(BlueCoveImpl.java:1020)
at java.security.AccessController.doPrivileged(Native Method)
at com.intel.bluetooth.BlueCoveImpl.detectStackPrivileged(BlueCoveImpl.j
ava:1018)
at com.intel.bluetooth.BlueCoveImpl.getBluetoothStack(BlueCoveImpl.java:
1011)
at javax.bluetooth.LocalDevice.getLocalDeviceInstance(LocalDevice.java:7
5)
at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:95)
at BluetoothDeviceDiscovery.main(BluetoothDeviceDiscovery.java:61)
February 4th, 2010 at 2:58 am
I am getting the following Exception. Kindly help
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.jsrsoft.eb.nw.LD.gld(Unknown Source)
at javax.bluetooth.LocalDevice.(Unknown Source)
at javax.bluetooth.LocalDevice.getLocalDevice(Unknown Source)
at BluetoothDeviceDiscovery.main(BluetoothDeviceDiscovery.java:33)
Caused by: java.lang.RuntimeException: Include ElectricBlue library in the path
at com.jsrsoft.eb.nw.NW.(Unknown Source)
... 4 more