JSR-82 Sample : Bluetooth Service Search

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
private static Object lock=new Object();

//vector containing the devices discovered
private static Vector vecDevices=new Vector();

private static String connectionURL=null;

* Entry point.
public static void main(String[] args) throws IOException {

BluetoothServiceDiscovery bluetoothServiceDiscovery=new BluetoothServiceDiscovery();

//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println(“Address: “+localDevice.getBluetoothAddress());
System.out.println(“Name: “+localDevice.getFriendlyName());

//find devices
DiscoveryAgent agent = localDevice.getDiscoveryAgent();

System.out.println(“Starting device inquiry…”);
agent.startInquiry(DiscoveryAgent.GIAC, bluetoothServiceDiscovery);

try {
catch (InterruptedException e) {

System.out.println(“Device Inquiry Completed. “);

//print all devices in vecDevices
int deviceCount=vecDevices.size();

if(deviceCount <= 0){ System.out.println("No Devices Found ."); } else{ //print bluetooth device addresses and names in the format [ No. address (name) ] System.out.println("Bluetooth Devices: "); for (int i = 0; i < deviceCount; i++) { RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i); System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(true)+")"); } } System.out.print("Choose the device to search for Obex Push service : "); BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in)); String chosenIndex=bReader.readLine(); int index=Integer.parseInt(chosenIndex.trim()); //check for obex service RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(index-1); UUID[] uuidSet = new UUID[1]; uuidSet[0]=new UUID("1105",true); System.out.println("\nSearching for service..."); agent.searchServices(null,uuidSet,remoteDevice,bluetoothServiceDiscovery); try { synchronized(lock){ lock.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } if(connectionURL==null){ System.out.println("Device does not support Object Push."); } else{ System.out.println("Device supports Object Push."); } } /** * 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){

* Called when the service search is over.
public void serviceSearchCompleted(int transID, int respCode) {

* Called when the device search is over.
public void inquiryCompleted(int discType) {

}//end method

}//end class


AddThis Social Bookmark Button AddThis Feed Button

17 Responses to “JSR-82 Sample : Bluetooth Service Search”

  1. JSR-82 : Java Bluetooth » JSR-82 Sample : SPP Server and Client » Articles, Tutorials and Discussion Forums about Java Bluetooth Programming and JSR-82 Bluetooth API Says:

    [...] 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 [...]

  2. Anonymous Says:

    java.io.BufferedReader is not part of the MIDP/CLDC Standard, so
    the above code will not work in a midlet development environment.

  3. Bruse Says:

    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

  4. Victor Klos Says:

    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?

  5. Victor Klos Says:

    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.

  6. Victor Klos Says:


    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

  7. Lukasz Says:

    If you are using BlueCove, try to use remoteDevice.getFriendlyName(false) The function remoteDevice.getFriendlyName(true) is not implemented on
    windows mobile version of bluecove.

  8. sub0 Says:

    Thank you for your code which is very helpful.
    But every time i execute it i always have the same response code in serviceSearchCompleted() whitch is respCode = 6 (SERVICE_SEARCH_DEVICE_NOT_REACHABLE).
    can you tell me where is the error ?
    PS : i work with avetana stack under Mandriva Linux

  9. kartik Says:

    I used it in mobile application but it is giving me exception at the following line of execution.


  10. kartik Says:

    The exceptoion is Null pointer Exception. It might be because the first parameter which we are passing is null. Can some one make me out from this ? I am in big trouble. MY email id is kartik@saltriver.com

  11. kartik Says:

    It seems no one is taking interest in this.

  12. cookdav Says:

    Nice code and very helpful!
    [It's working for me on Vista
    using the BlueCove-2.1.0 jar.]

  13. Tanmay Says:

    hello…i want to transfer data via bluetooth by java from one computer to another but i dont know how to execute it.Please help me!!

  14. Himanshu Arora Says:

    hey ppl m workin on 1 blue tooth application … so if u hav any doubt regarding Connection ..post it here or mail me …

  15. Bluetooth headset Says:

    Do i need software to install this on my mobile to make the bluetooth work? I have read about similar coding on a bluetooth blog

  16. Aviator168 Says:

    After discovery. How do you make a bluetooth headset start transmitting and receiving audio?

  17. karthick Says:

    code works well with netbeans and bluecove2.1.0

Leave a Comment