Thursday, June 28, 2012

JSR-82 : Java Bluetooth

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.


  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Vector;
  5.  
  6. import javax.bluetooth.DeviceClass;
  7. import javax.bluetooth.DiscoveryAgent;
  8. import javax.bluetooth.DiscoveryListener;
  9. import javax.bluetooth.LocalDevice;
  10. import javax.bluetooth.RemoteDevice;
  11. import javax.bluetooth.ServiceRecord;
  12. import javax.bluetooth.UUID;
  13.  
  14. /**
  15. *
  16. * Class that discovers all bluetooth devices in the neighbourhood,
  17. *
  18. * Connects to the chosen device and checks for the presence of OBEX push service in it.
  19. * and displays their name and bluetooth address.
  20. *
  21. *
  22. */
  23. public class BluetoothServiceDiscovery implements DiscoveryListener{
  24.    
  25.     //object used for waiting
  26.     private static Object lock=new Object();
  27.        
  28.     //vector containing the devices discovered
  29.     private static Vector vecDevices=new Vector();
  30.    
  31.     private static String connectionURL=null;
  32.  
  33.    
  34.     /**
  35.      * Entry point.
  36.      */
  37.     public static void main(String[] args) throws IOException {
  38.        
  39.         BluetoothServiceDiscovery bluetoothServiceDiscovery=new BluetoothServiceDiscovery();
  40.        
  41.         //display local device address and name
  42.         LocalDevice localDevice = LocalDevice.getLocalDevice();
  43.         System.out.println("Address: "+localDevice.getBluetoothAddress());
  44.         System.out.println("Name: "+localDevice.getFriendlyName());
  45.        
  46.         //find devices
  47.         DiscoveryAgent agent = localDevice.getDiscoveryAgent();
  48.       
  49.         System.out.println("Starting device inquiry...");
  50.         agent.startInquiry(DiscoveryAgent.GIAC, bluetoothServiceDiscovery);
  51.        
  52.         try {
  53.             synchronized(lock){
  54.                 lock.wait();
  55.             }
  56.         }
  57.         catch (InterruptedException e) {
  58.             e.printStackTrace();
  59.         }
  60.        
  61.        
  62.         System.out.println("Device Inquiry Completed. ");
  63.        
  64.         //print all devices in vecDevices
  65.         int deviceCount=vecDevices.size();
  66.        
  67.         if(deviceCount <= 0){
  68.             System.out.println("No Devices Found .");
  69.         }
  70.         else{
  71.             //print bluetooth device addresses and names in the format [ No. address (name) ]
  72.             System.out.println("Bluetooth Devices: ");
  73.             for (int i = 0; i <deviceCount; i++) {
  74.                 RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
  75.                 System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(true)+")");
  76.             }
  77.         }
  78.        
  79.         System.out.print("Choose the device to search for Obex Push service : ");
  80.         BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
  81.        
  82.         String chosenIndex=bReader.readLine();
  83.         int index=Integer.parseInt(chosenIndex.trim());
  84.        
  85.         //check for obex service
  86.         RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(index-1);
  87.         UUID[] uuidSet = new UUID[1];
  88.         uuidSet[0]=new UUID("1105",true);
  89.        
  90.         System.out.println("\nSearching for service...");
  91.         agent.searchServices(null,uuidSet,remoteDevice,bluetoothServiceDiscovery);
  92.        
  93.         try {
  94.             synchronized(lock){
  95.                 lock.wait();
  96.             }
  97.         }
  98.         catch (InterruptedException e) {
  99.             e.printStackTrace();
  100.         }
  101.        
  102.         if(connectionURL==null){
  103.             System.out.println("Device does not support Object Push.");
  104.         }
  105.         else{
  106.             System.out.println("Device supports Object Push.");
  107.         }
  108.     }
  109.    
  110.    
  111.     /**
  112.      * Called when a bluetooth device is discovered.
  113.      * Used for device search.
  114.      */
  115.     public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  116.         //add the device to the vector
  117.         if(!vecDevices.contains(btDevice)){
  118.             vecDevices.addElement(btDevice);
  119.         }
  120.     }
  121.  
  122.    
  123.     /**
  124.      * Called when a bluetooth service is discovered.
  125.      * Used for service search.
  126.      */
  127.     public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  128.         if(servRecord!=null && servRecord.length>0){
  129.             connectionURL=servRecord[0].getConnectionURL(0,false);
  130.         }
  131.         synchronized(lock){
  132.             lock.notify();
  133.         }
  134.     }
  135.  
  136.    
  137.     /**
  138.      * Called when the service search is over.
  139.      */
  140.     public void serviceSearchCompleted(int transID, int respCode) {
  141.         synchronized(lock){
  142.             lock.notify();
  143.         }
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Called when the device search is over.
  149.      */
  150.     public void inquiryCompleted(int discType) {
  151.         synchronized(lock){
  152.             lock.notify();
  153.         }
  154.        
  155.     }//end method
  156.    
  157.    
  158. }//end class 
     

No comments:

Post a Comment