android device.getUuids returns null

9.5k views Asked by At

I'm trying to connect to an Arduino Uno via an android app using Bluetooth Low Energy (BLE).
I'm developing on Android Studio, testing with a Samsung Galaxy S4, and with an Android version 5.0.1
I followed this link: http://www.truiton.com/2015/04/android-bluetooth-low-energy-ble-example/
I'm scanning devices and when I found one, I would like to get it's UUID before connecting to it, to make sure that it's the right type of device:

mScanCallback = new ScanCallback() {
        @Override
        @TargetApi(21)
        public void onScanResult(int callbackType, ScanResult result) {
            BluetoothDevice btDevice = result.getDevice();
            ParcelUuid[] uuids = btDevice.getUuids(); //<-- this is always null!! :(

            Log.d(TAG, ""+btDevice.fetchUuidsWithSdp()); //<-- prints true.
            Log.d(TAG, "result : " + result.toString()); //<-- prints a bunch of relevant info that contains the UUID I want to check.
            Log.d(TAG, "uuids : " + uuids); //<-- prints null.
            /*
            for (ParcelUuid u : uuids) {
                //Compare with the UUID of my device, and connect if ok.
            }
            */
        }

However, btDevice.getUuids(); is always returning null with no error...
How can I get the UUID of the scanned device?
A brute force method would be to use regexp with the result.toString() to grab what I want but there must be a better way isn't it?
Thanks

3

There are 3 answers

1
Marcos Vasconcelos On

From Java DOC:

public ParcelUuid[] getUuids ()

Added in API level 15 Returns the supported features (UUIDs) of the remote device.

This method does not start a service discovery procedure to retrieve the UUIDs from the remote device. Instead, the local cached copy of the service UUIDs are returned.

Use fetchUuidsWithSdp() if fresh UUIDs are desired.

Requires BLUETOOTH.

Returns the supported features (UUIDs) of the remote device, or null on error

Your ScanResult probably is a error

1
Jefriiyer S On
 BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
 // scan for devices
 scanner.startScan(new ScanCallback() {
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 @Override
 public void onScanResult(int callbackType, ScanResult result) 
   {
      List<ParcelUuid> uuids = result.getScanRecord().getServiceUuids();
   }
}

this is worked for me in Android 6.0.1

0
Gabriel Gardin On

After hours of searching I find out that getUuid() is not the way to go if you want to retrieve the uuid of an iBeacon.

If that is the case, you need to get it directly from the Scan result result object.

I managed to work by using the answer provided by ADEBAYO OSIPITAN. BLE obtain uuid encoded in advertising packet

Here is his code snipet:

//For APIs greater than 21, Returns Device UUID
public String getUUID(ScanResult result){
String UUIDx = UUID
        .nameUUIDFromBytes(result.getScanRecord().getBytes()).toString();
    return UUIDx;
}