JSR-82 Sample : Device Discovery

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

  1. import java.io.IOException;
  2. import java.util.Vector;
  3.  
  4. import javax.bluetooth.DeviceClass;
  5. import javax.bluetooth.DiscoveryAgent;
  6. import javax.bluetooth.DiscoveryListener;
  7. import javax.bluetooth.LocalDevice;
  8. import javax.bluetooth.RemoteDevice;
  9. import javax.bluetooth.ServiceRecord;
  10.  
  11.  
  12. /**
  13. * Class that discovers all bluetooth devices in the neighbourhood
  14. * and displays their name and bluetooth address.
  15. */
  16. public class BluetoothDeviceDiscovery implements DiscoveryListener{
  17.    
  18.    
  19.     //object used for waiting
  20.     private static Object lock=new Object();
  21.    
  22.     //vector containing the devices discovered
  23.     private static Vector vecDevices=new Vector();
  24.    
  25.    
  26.     //main method of the application
  27.     public static void main(String[] args) throws IOException {
  28.        
  29.         //create an instance of this class
  30.         BluetoothDeviceDiscovery bluetoothDeviceDiscovery=new BluetoothDeviceDiscovery();
  31.        
  32.         //display local device address and name
  33.         LocalDevice localDevice = LocalDevice.getLocalDevice();
  34.         System.out.println("Address: "+localDevice.getBluetoothAddress());
  35.         System.out.println("Name: "+localDevice.getFriendlyName());
  36.        
  37.         //find devices
  38.         DiscoveryAgent agent = localDevice.getDiscoveryAgent();
  39.       
  40.         System.out.println("Starting device inquiry...");
  41.         agent.startInquiry(DiscoveryAgent.GIAC, bluetoothDeviceDiscovery);
  42.        
  43.         try {
  44.             synchronized(lock){
  45.                 lock.wait();
  46.             }
  47.         }
  48.         catch (InterruptedException e) {
  49.             e.printStackTrace();
  50.         }
  51.        
  52.        
  53.         System.out.println("Device Inquiry Completed. ");
  54.        
  55.         //print all devices in vecDevices
  56.         int deviceCount=vecDevices.size();
  57.        
  58.         if(deviceCount <= 0){
  59.             System.out.println("No Devices Found .");
  60.         }
  61.         else{
  62.             //print bluetooth device addresses and names in the format [ No. address (name) ]
  63.             System.out.println("Bluetooth Devices: ");
  64.             for (int i = 0; i <deviceCount; i++) {
  65.                 RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
  66.                 System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(true)+")");
  67.             }
  68.         }
  69.        
  70.        
  71.     }//end main
  72.  
  73.     //methods of DiscoveryListener
  74.    
  75.     /**
  76.      * This call back method will be called for each discovered bluetooth devices.
  77.      */
  78.     public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  79.         System.out.println("Device discovered: "+btDevice.getBluetoothAddress());
  80.         //add the device to the vector
  81.         if(!vecDevices.contains(btDevice)){
  82.             vecDevices.addElement(btDevice);
  83.         }
  84.     }
  85.  
  86.     //no need to implement this method since services are not being discovered
  87.     public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  88.     }
  89.  
  90.     //no need to implement this method since services are not being discovered
  91.     public void serviceSearchCompleted(int transID, int respCode) {
  92.     }
  93.  
  94.    
  95.     /**
  96.      * This callback method will be called when the device discovery is
  97.      * completed.
  98.      */
  99.     public void inquiryCompleted(int discType) {
  100.         synchronized(lock){
  101.             lock.notify();
  102.         }
  103.        
  104.         switch (discType) {
  105.             case DiscoveryListener.INQUIRY_COMPLETED :
  106.                 System.out.println("INQUIRY_COMPLETED");
  107.                 break;
  108.                
  109.             case DiscoveryListener.INQUIRY_TERMINATED :
  110.                 System.out.println("INQUIRY_TERMINATED");
  111.                 break;
  112.                
  113.             case DiscoveryListener.INQUIRY_ERROR :
  114.                 System.out.println("INQUIRY_ERROR");
  115.                 break;
  116.  
  117.             default :
  118.                 System.out.println("Unknown Response Code");
  119.                 break;
  120.         }
  121.     }//end method
  122. }//end class

AddThis Social Bookmark Button AddThis Feed Button

2 Responses to “JSR-82 Sample : Device Discovery”

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

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

  2. anon Says:

    Thanks for a good short well written tutorial - you have helped no end!

Leave a Comment