Android Bluetooth: Get UUIDs of discovered devices

androidbluetooth

As I'm currently working on a little bluetooth library for Android, I'm trying to get all the service uuids of the devices I discovered in my surrounding.

When my broadcast receiver gets the BluetoothDevice.ACTION_FOUND intent, I'm extracting the device and call:

device.fetchUuidsWithSdp();

This will result BluetoothDevice.ACTION_UUID intents for each device found and I'm handling them with the same receiver:

BluetoothDevice d = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);

if(uuidExtra ==  null) {
    Log.e(TAG, "UUID = null");
}

if(d != null && uuidExtra != null)
    Log.d(TAG, d.getName() + ": " + uuidExtra.toString());

The thing is, that uuidExtra is always null.

How can i get all the UUIDs of the surrounding devices?

EDIT:

Im working on a Nexus 7. I tried code i found on the internet and this also gives me a NullPointerException: http://digitalhacksblog.blogspot.de/2012/05/android-example-bluetooth-discover-and.html

Thank you.

Best Answer

The documentation on this states...

Always contains the extra field BluetoothDevice.EXTRA_UUID

However, just like you, I have found this not to be true.

If you call fetchUuidsWithSdp() while device discovery is still taking place BluetoothDevice.EXTRA_UUID can be null.

You should wait until you receive BluetoothAdapter.ACTION_DISCOVERY_FINISHED before you make any calls to fetchUuidsWithSdp().