Advertisement
Guest User

Untitled

a guest
Apr 28th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package moppydesk.outputs;
  2.  
  3. import javax.bluetooth.*;
  4. import java.io.IOException;
  5.  
  6. public class BluetoothScan {
  7.     private boolean scanFinished = false;
  8.     private RemoteDevice hc05device;
  9.     private String hc05Url;
  10.  
  11.     public String scan() throws BluetoothStateException {
  12.  
  13.         LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, new DiscoveryListener() {
  14.             @Override
  15.             public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  16.                 try {
  17.                     String name = btDevice.getFriendlyName(false);
  18.                     System.out.format("%s (%s)\n", name, btDevice.getBluetoothAddress());
  19.                     if (name.contains("HT")) {//!!!!!!!Replace this with a part of your Bluetooth name e.g. HT-05
  20.                         hc05device = btDevice;
  21.                     }
  22.                 } catch (IOException e) {
  23.                     e.printStackTrace();
  24.                 }
  25.             }
  26.  
  27.             @Override
  28.             public void inquiryCompleted(int discType) {
  29.                 scanFinished = true;
  30.             }
  31.  
  32.             @Override
  33.             public void serviceSearchCompleted(int transID, int respCode) {
  34.             }
  35.  
  36.             @Override
  37.             public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  38.             }
  39.         });
  40.         while (!scanFinished) {
  41.             try {
  42.                 Thread.sleep(100);
  43.             } catch (InterruptedException e) {
  44.                 e.printStackTrace();
  45.             }
  46.         }
  47.  
  48.         //search for services:
  49.         UUID uuid = new UUID(0x1101); //scan for btspp://... services (as HC-05 offers it)
  50.         UUID[] searchUuidSet = new UUID[]{uuid};
  51.         int[] attrIDs = new int[]{
  52.                 0x0100 // service name
  53.         };
  54.  
  55.         scanFinished = false;
  56.         LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet,
  57.                 hc05device, new DiscoveryListener() {
  58.                     @Override
  59.                     public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  60.                     }
  61.  
  62.                     @Override
  63.                     public void inquiryCompleted(int discType) {
  64.                     }
  65.  
  66.                     @Override
  67.                     public void serviceSearchCompleted(int transID, int respCode) {
  68.                         scanFinished = true;
  69.                     }
  70.  
  71.                     @Override
  72.                     public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  73.                         for (int i = 0; i < servRecord.length; i++) {
  74.                             hc05Url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
  75.                             if (hc05Url != null) {
  76.                                 break; //take the first one
  77.                             }
  78.                         }
  79.                     }
  80.                 });
  81.  
  82.         while (!scanFinished) {
  83.             try {
  84.                 Thread.sleep(100);
  85.             } catch (InterruptedException e) {
  86.                 e.printStackTrace();
  87.             }
  88.         }
  89.         System.out.println(hc05Url);
  90.         return hc05Url;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement