JSR-82 Sample : Bluetooth Service Search
- Posted by Bruse
In 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 device.
The following code sample shows you how to use the DiscoveryAgent to search for an OBEX Push bluetooth service. It first searches for all available Bluetooth Devices, and prompts to select a device on which to search the OBEX Push Service.
For this sample to work, you need a JSR-82 Implmentation (Java Bluetooth Stack) like ElectricBlue or aveLink in the class path.
-
import java.io.BufferedReader;
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
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;
-
import javax.bluetooth.UUID;
-
-
/**
-
*
-
* Class that discovers all bluetooth devices in the neighbourhood,
-
*
-
* Connects to the chosen device and checks for the presence of OBEX push service in it.
-
* and displays their name and bluetooth address.
-
*
-
*
-
*/
-
public class BluetoothServiceDiscovery implements DiscoveryListener{
-
-
//object used for waiting
-
-
//vector containing the devices discovered
-
-
-
-
/**
-
* Entry point.
-
*/
-
-
BluetoothServiceDiscovery bluetoothServiceDiscovery=new BluetoothServiceDiscovery();
-
-
//display local device address and name
-
LocalDevice localDevice = LocalDevice.getLocalDevice();
-
-
//find devices
-
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
-
-
agent.startInquiry(DiscoveryAgent.GIAC, bluetoothServiceDiscovery);
-
-
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)+")");
-
}
-
}
-
-
-
-
//check for obex service
-
RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(index-1);
-
UUID[] uuidSet = new UUID[1];
-
uuidSet[0]=new UUID("1105",true);
-
-
agent.searchServices(null,uuidSet,remoteDevice,bluetoothServiceDiscovery);
-
-
try {
-
synchronized(lock){
-
lock.wait();
-
}
-
}
-
e.printStackTrace();
-
}
-
-
if(connectionURL==null){
-
}
-
else{
-
}
-
}
-
-
-
/**
-
* Called when a bluetooth device is discovered.
-
* Used for device search.
-
*/
-
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
-
//add the device to the vector
-
if(!vecDevices.contains(btDevice)){
-
vecDevices.addElement(btDevice);
-
}
-
}
-
-
-
/**
-
* Called when a bluetooth service is discovered.
-
* Used for service search.
-
*/
-
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
-
if(servRecord!=null && servRecord.length>0){
-
connectionURL=servRecord[0].getConnectionURL(0,false);
-
}
-
synchronized(lock){
-
lock.notify();
-
}
-
}
-
-
-
/**
-
* Called when the service search is over.
-
*/
-
public void serviceSearchCompleted(int transID, int respCode) {
-
synchronized(lock){
-
lock.notify();
-
}
-
}
-
-
-
/**
-
* Called when the device search is over.
-
*/
-
public void inquiryCompleted(int discType) {
-
synchronized(lock){
-
lock.notify();
-
}
-
-
}//end method
-
-
-
}//end class

October 31st, 2007 at 2:57 am
[...] If you already knew the direct url to the server, this step can be skipped. This consist of searching for a service, and getting its connection URL. More details about this can be learned from the article about bluetooth service search [...]
December 5th, 2007 at 9:49 am
java.io.BufferedReader is not part of the MIDP/CLDC Standard, so
the above code will not work in a midlet development environment.
December 5th, 2007 at 3:53 pm
BufferedReader is used in the above code just to get the input values from the user. It is just for demonstration purpose. Just remove that part, and it will work in j2me environment
December 6th, 2007 at 10:40 am
This example fails if a uuidSet with more than one element is given. According to the standard, that ought to work.
Any ideas on this?
December 6th, 2007 at 11:01 am
Just found out that the search that is performed is an AND search. Thus, if you give several UUIDs the search is only succesful if the device in question delivers ALL uuids given.
This is unexpected behaviour.
December 6th, 2007 at 11:36 am
update
The above is not entirely true, sorry for posting too quickly.
What is true is that given an array of UUIDs only the first is returned as ServiceRecord, even though more are supported.
I am using BlueCove version 2.0.1 on widcomm (win xp).
@moderator: you may compress my last posts if you wish