Search code examples
androidbluetooth-lowenergyibeaconibeacon-androidandroid-ibeacon

RedBear Beacon. How to get UUID using Bluetooth scan?


I'm using some Red Bear Beacons (that in Android are charged as bluetooth devices) into an Android application and I want to get the UUID stored in the beacon, as they do in their native app. Using the samples they provide I have not ben able to get it.

What I have tried:

  1. First approach

     BluetoothManager mBluetoothManager = (BluetoothManager)
     getSystemService(Context.BLUETOOTH_SERVICE);
    
     mBluetoothAdapter = mBluetoothManager.getAdapter();
    
     mBluetoothAdapter.startLeScan(mLeScanCallback);
    
    
    BluetoothAdapter.LeScanCallback mLeScanCallback = new
         BluetoothAdapter.LeScanCallback() {   
                  @Override
                  public void onLeScan(final BluetoothDevice device, final int rssi,
                          final byte[] scanRecord) {
    
                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
    
                              if (mDevice.indexOf(device) == -1){
                                  mDevice.add(device);
    
                                  byte[] serviceUuidBytes = new byte[16];
                                  String serviceUuid = "";
                                  for (int i = 32, j = 0; i >= 17; i--, j++) {
                                      serviceUuidBytes[j] = scanRecord[i];
                                  }
                                  serviceUuid = bytesToHex(serviceUuidBytes);
    
                                  Log.i(TAG, "UUID is: " + serviceUuid);
                                    //This is the result 420903bf01004915e79610a7f5d060b0
                              }
                          }
                      });
                  }
              };
    
  2. Second approach

    BluetoothLeScanner mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    
    mBluetoothLeScanner.startScan(mScanCallback); 
    
    ScanCallback mScanCallback = new ScanCallback() {
    
          public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
              Log.i(TAG, "scan result" + result.toString());
                //this contains something like [...] mServiceUuids=[b0702980-a295-a8ab-f734-031a98a512de][....]
          };
      };
    

None of these results 420903bf01004915e79610a7f5d060b0 OR b0702980-a295-a8ab-f734-031a98a512de is what I'm looking for, that should be like this: (screenshot from RedBear BeaconToolapp ) enter image description here

After some tests with my beacons I could see that this UUID is somehow linked with the one found by my mBluetoothLeScanner -> b0702980-a295-a8ab-f734-031a98a512de that makes me think that is the same but encoded(displayed) in a different manner.

Has anyone experience with redbear beacons and could tell me how can I obtain the beacon's UUID (E2C56DB5-DFFB-48D2-B060-D0F5A71096E0) in my app?

OR

can anybody tell me if E2C56DB5-DFFB-48D2-B060-D0F5A71096E0 and b0702980-a295-a8ab-f734-031a98a512de represents the same thing encoded in a different manner and if yes, how can be converted ?

Hope I was clear enough, please help me; any help will be highly appreciated! Thanks.


Solution

  • Understand that a beacon Proximity UUID is not the same as a Bluetooth GATT Service UUID. This is why the second sample you show won´t work. What you need to do is parse out the Proximity UUID from the bytes of the advertisement, as you are attempting to do in your first approach. However in that approach, this code is problematic

    for (int i = 32, j = 0; i >= 17; i--, j++) {
      serviceUuidBytes[j] = scanRecord[i];
    }
    

    First of all, the bytes you are reading out of the scan are not in reverse order, so you should not be decrementing the i variable. You do need to know the offset of the Proximity UUID and other fields to do this properly. This is proprietary Apple information, but it is readily available by searching on Google for the iBeacon layout or "profile".

    Also, if you want to do more than simple processing like this, you might consider using a full featured library like my Android Beacon Library. While it only works with AltBeacons out of the box, it is trivial to configure with any proprietary beacon type if you know the beacon layout. Simply do a Google search for "setBeaconLayout".