2016-02-22 88 views
3

我無法獲得前棒棒糖設備上的iBeacon的UUID,但下面我的代碼正在使用Marshmallow Nexus 5x。無法獲取前棒棒糖設備上的iBeacon的UUID

我不想使用像AltBeacon或相關的任何圖書館。

BluetoothManager bm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
BluetoothAdapter mBluetoothAdapter = bm.getAdapter(); 
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { 
    //Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
    //startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
} else { 
    BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner(); 
    // scan for devices 
    scanner.startScan(new ScanCallback() { 
     @Override 
     public void onScanResult(int callbackType, ScanResult result) { 
      // get the discovered device as you wish 
      // this will trigger each time a new device is found 
      BluetoothDevice device = result.getDevice(); 
      if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) { //int the device type DEVICE_TYPE_CLASSIC, DEVICE_TYPE_LE DEVICE_TYPE_DUAL. DEVICE_TYPE_UNKNOWN if it's not available 
       if (device.fetchUuidsWithSdp()) { 
        System.out.println(device.getName()); 
        List<ParcelUuid> uuids = result.getScanRecord().getServiceUuids(); 
        System.out.println(uuids); 
        System.out.println(getMajor(result.getScanRecord().getBytes())); 
        System.out.println(getMinor(result.getScanRecord().getBytes())); 
       } else { 
        System.out.println("failed"); 
       } 
      } 
     } 
    }); 
} 

回答

2
BluetoothManager bm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
      BluetoothAdapter mBluetoothAdapter = bm.getAdapter(); 
      if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { 
       //Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       //startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
      } else { 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
        BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner(); 
        // scan for devices 
        scanner.startScan(new ScanCallback() { 
         @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
         @Override 
         public void onScanResult(int callbackType, ScanResult result) { 
          // get the discovered device as you wish 
          // this will trigger each time a new device is found 
          BluetoothDevice device = result.getDevice(); 
          if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE)//int the device type DEVICE_TYPE_CLASSIC, DEVICE_TYPE_LE DEVICE_TYPE_DUAL. DEVICE_TYPE_UNKNOWN if it's not available 
          { 
           if (device.fetchUuidsWithSdp()) { 
            System.out.println(device.getName()); 
            List<ParcelUuid> uuids = result.getScanRecord().getServiceUuids(); 
            System.out.println(uuids); 
            System.out.println(getMajor(result.getScanRecord().getBytes())); 
            System.out.println(getMinor(result.getScanRecord().getBytes())); 

           } else { 
            System.out.println("failed"); 
           } 
          } 
         } 
        }); 
       } else { 
        // targetting kitkat or bellow 
        mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() { 
         @Override 
         public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { 
          // get the discovered device as you wish\ 

         } 
        }); 
       } 
      } 
2
public void onLeScan(BluetoothDevice device, int rssi, final byte[] scanRecord) { 
     int startByte = 2; 
     boolean patternFound = false; 
     while (startByte <= 5) { 
      if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon 
        ((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length 
       patternFound = true; 
       break; 
      } 
      startByte++; 
     } 

     if (patternFound) { 
      //Convert to hex String 
      byte[] uuidBytes = new byte[16]; 
      System.arraycopy(scanRecord, startByte+4, uuidBytes, 0, 16); 
      String hexString = bytesToHex(uuidBytes); 

      //Here is your UUID 
      String uuid = hexString.substring(0,8) + "-" + 
        hexString.substring(8,12) + "-" + 
        hexString.substring(12,16) + "-" + 
        hexString.substring(16,20) + "-" + 
        hexString.substring(20,32); 

      //Here is your Major value 
      int major = (scanRecord[startByte+20] & 0xff) * 0x100 + (scanRecord[startByte+21] & 0xff); 

      //Here is your Minor value 
      int minor = (scanRecord[startByte+22] & 0xff) * 0x100 + (scanRecord[startByte+23] & 0xff); 

      System.out.println(String.format("%s\n%s\n%s",uuid,major,minor)); 
      textView.setText(String.format("%s\n%s\n%s", uuid,major,minor)); 
     } 
    } 

    /** 
    * bytesToHex method 
    * Found on the internet 
    * http://stackoverflow.com/a/9855338 
    */ 
    static final char[] hexArray = "ABCDEF".toCharArray(); 
    private static String bytesToHex(byte[] bytes) { 
     char[] hexChars = new char[bytes.length * 2]; 
     for (int j = 0; j < bytes.length; j++) { 
      int v = bytes[j] & 0xFF; 
      hexChars[j * 2] = hexArray[v >>> 4]; 
      hexChars[j * 2 + 1] = hexArray[v & 0x0F]; 
     } 
     return new String(hexChars); 
    } 

呼叫這樣的方法和享受:) onLeScan(設備,result.getRssi(),result.getScanRecord()的getBytes());

相關問題