Java  

Example

: Scanning bluetooth devices


Code
  1. package bluetoothexample;
  2. import java.io.IOException;
  3. import java.util.Vector;
  4. import javax.bluetooth.*;
  5. /**
  6. *
  7. * Class that discovers all bluetooth devices in the neighbourhood,
  8. *
  9. */
  10. public class BluetoothExample implements DiscoveryListener
  11. {
  12. private static Object lock=new Object();//object used for waiting
  13. private static Vector vecDevices=new Vector();//vector containing the devices discovered
  14. private static String connectionURL=null;
  15. public static void main(String[] args) throws IOException
  16. {
  17. BluetoothExample be=new BluetoothExample();
  18. //display local device address and name
  19. LocalDevice localDevice = LocalDevice.getLocalDevice();
  20. System.out.println("Address: "+localDevice.getBluetoothAddress());
  21. System.out.println("Name: "+localDevice.getFriendlyName());
  22. //find devices
  23. DiscoveryAgent agent = localDevice.getDiscoveryAgent();
  24. /////////////////////////////////////////////////////
  25. //System.out.println("Starting device inquiry...");//
  26. /////////////////////////////////////////////////////
  27. agent.startInquiry(DiscoveryAgent.GIAC, be);
  28. try
  29. {
  30. synchronized(lock)
  31. {
  32. lock.wait();
  33. }
  34. }
  35. e.printStackTrace();
  36. }
  37. /////////////////////////////////////////////////////
  38. //System.out.println("Device Inquiry Completed. ");//
  39. /////////////////////////////////////////////////////
  40. //print all devices in vecDevices
  41. int deviceCount=vecDevices.size();
  42. if(deviceCount <= 0){
  43. System.out.println("No Devices Found .");
  44. }
  45. else
  46. {
  47. System.out.println("Bluetooth Devices: ");
  48. for (int i = 0; i )
  49. {
  50. try
  51. {
  52. RemoteDevice remoteDevice = (RemoteDevice) vecDevices.elementAt(i);
  53. System.out.println((i + 1) + ". " + remoteDevice.getBluetoothAddress() + " (" + remoteDevice.getFriendlyName(true) + ")");
  54. } catch (IOException iOException) {
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * Called when a bluetooth device is discovered.
  61. * Used for device search.
  62. */
  63. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
  64. {
  65. //add the device to the vector
  66. if(!vecDevices.contains(btDevice))
  67. {
  68. vecDevices.addElement(btDevice);
  69. }
  70. }
  71. /**
  72. * Called when a bluetooth service is discovered.
  73. * Used for service search.
  74. */
  75. public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
  76. {
  77. if(servRecord!=null && servRecord.length>0)
  78. {
  79. connectionURL=servRecord[0].getConnectionURL(0,false);
  80. }
  81. synchronized(lock)
  82. {
  83. lock.notify();
  84. }
  85. }
  86. /**
  87. * Called when the service search is over.
  88. */
  89. public void serviceSearchCompleted(int transID, int respCode)
  90. {
  91. synchronized(lock)
  92. {
  93. lock.notify();
  94. }
  95. }
  96. /**
  97. * Called when the device search is over.
  98. */
  99. public void inquiryCompleted(int discType)
  100. {
  101. synchronized(lock)
  102. {
  103. lock.notify();
  104. }
  105. }//end method
  106. }//end class


Returns
    BlueCove version 2.1.0 on bluez
    Address: 00805A20EB9C
    Name: hormiga-0
    Bluetooth Devices:
    1. 001E3BA57B16 (Libelula)
    2. 0025C7339785 (Mosquito)
    3. 0017E5D82BB0 (Tarantula)
    4. 001F5DBFF001 (Cucaracha)
    BlueCove stack shutdown completed
    


Invocation:

java -jar "BluetoothExample.jar"


Require

Require bluecove lib, this library is inside of lib folder.


© Libelium Comunicaciones Distribuidas S.L.

| Terms of use