×
Namespaces

Variants
Actions
Revision as of 08:38, 15 August 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to search for Bluetooth devices and services

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to connect to and find out the address of devices with specific Bluetooth services using the Java ME Bluetooth API (JSR-82).

Article Metadata
Article
Created: SergioEstevao (25 Nov 2007)
Last edited: hamishwillee (15 Aug 2013)

In the following sample we are going to see how to implement this code using a class called BtManager. Let's start creating the class and some variables to store our results.

import java.util.Vector;
// bluetooth classes
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;
 
public class BTManager implements DiscoveryListener {
// used to store devices we found
public Vector btDevicesFound;
// used to store service information for each device
public Vector btServicesFound;
 
public BTManager() {
btDevicesFound = new Vector();
btServicesFound = new Vector();
}
 
public int find(UUID[] aServices){
// search for devices
findDevices();
// search for services in the devices found
findServices(aServices);
return btDevicesFound.size();
}

As you can see we are going to implement the DiscoveryListener interface. This interface provides all the callbacks for our Bluetooth calls. Let's start for searching for devices:

public int findDevices() {
try {
// cleans previous elements
btDevicesFound.removeAllElements();
// resets status variable
isBTSearchComplete = false;
LocalDevice local = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = local.getDiscoveryAgent();
// start discovery of new devices
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
while ((!isBTSearchComplete)) {
//waits for a fixed time, to avoid long search
synchronized (this) {
this.wait(BTManager.BLUETOOTH_TIMEOUT);
}
// check if search is completed
if (!isBTSearchComplete) {
// search no yet completed so let's cancel it
discoveryAgent.cancelInquiry(this);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// returns the number of devices found
return btDevicesFound.size();
}
 
public void deviceDiscovered(RemoteDevice remoteDevice,
DeviceClass deviceClass) {
btDevicesFound.addElement(remoteDevice);
}
 
public void inquiryCompleted(int param) {
isBTSearchComplete = true;
// notifies and wake main thread that device search is completed
synchronized (this) {
this.notify();
}
}

Now that we have a list of devices, let's search for the services we want:

public void findServices(UUID[] aServices) {
// cleans previous elements
btServicesFound.removeAllElements();
try {
LocalDevice local = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = local.getDiscoveryAgent();
// discover services
if (btDevicesFound.size() > 0) {
for (int i = 0; i < btDevicesFound.size(); i++) {
isBTSearchComplete = false;
// adds a null element in case we don't found service
btServicesFound.addElement(null);
int transID = discoveryAgent.searchServices(null, aServices,
(RemoteDevice) (btDevicesFound.elementAt(i)), this);
// wait for service discovery ends
synchronized (this) {
this.wait(BTManager.BLUETOOTH_TIMEOUT);
}
if (!isBTSearchComplete) {
discoveryAgent.cancelServiceSearch(transID);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
 
public void servicesDiscovered(int param, ServiceRecord[] serviceRecord) {
int index = btServicesFound.size() - 1;
for (int i = 0; i < serviceRecord.length; i++) {
btServicesFound.setElementAt(serviceRecord[i], index);
}
}
 
public void serviceSearchCompleted(int transID, int respCode) {
isBTSearchComplete = true;
// notifies and wake mains thread that service search is completed
synchronized (this) {
this.notify();
}
}

After we got all the data stored in our Vectors, we just need to add two methods to retrieve information, one to retrieve the Bluetooth Url and another Bluetooth user friendly name, after all Bluetooth URLs, don't have a lot of meaning to the end users of our application.

  public String getDeviceName(int deviceID) {
try {
return ((RemoteDevice) btDevicesFound.elementAt(deviceID))
.getFriendlyName(false);
} catch (Exception e) {
e.printStackTrace();
}
return "Error";
}
 
public String getServiceURL(int deviceID) {
try {
return ((ServiceRecord) btServicesFound.elementAt(deviceID))
.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
} catch (Exception e) {
e.printStackTrace();
}
return "Error";
}

And with those last methods we completed our Bluetooth finder class. You can check the full source code where I provide a example of how to use it to search for Bluetooth GPS.

Downloads

This page was last modified on 15 August 2013, at 08:38.
116 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×