Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bluetooth: Get UUIDs of discovered devices

Tags:

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.

like image 471
tellob Avatar asked Feb 11 '13 12:02

tellob


People also ask

How do I find my Android Bluetooth UUID?

If you want to get the UUID of a Bluetooth device in Android, there are a few different ways to do it. One way is to use the BluetoothAdapter. getRemoteDevice(String address) method. This will return a BluetoothDevice object, which has a getUuids() method that will return an array of UUIDs.

How do I get a list of Bluetooth devices on my Android phone?

By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.

What is service UUID in Bluetooth?

The UUID is used for uniquely identifying information. It identifies a particular service provided by a Bluetooth device. The standard defines a basic BASE_UUID: 00000000-0000-1000-8000-00805F9B34FB . Devices such as healthcare sensors can provide a service, substituting the first eight digits with a predefined code.


2 Answers

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().

like image 162
Eddie Avatar answered Sep 19 '22 20:09

Eddie


NOTE: This solution applies to CLASSIC bluetooth and not BLE. For BLE check how to send manufacturer specific Data in advertiser on the peripheral side

The problem with fetching Uuids is that you have only one bluetooth adapter, and we cannot have parallel api calls which uses adapter for its purpose.

As Eddie pointed out, wait for BluetoothAdapter.ACTION_DISCOVERY_FINISHED and then call fetchUuidsWithSdp().

Still this cannot guarantee uuids to be fetched for all devices. In addition to this one must wait for each subsequent call to fetchuuidsWithSdp() to complete, and then give a call to this method for another device.

See the code below --

ArrayList<BluetoothDevice> mDeviceList = new ArrayList<BluetoothDevice>();  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {         public void onReceive(Context context, Intent intent) {         String action = intent.getAction();          if (BluetoothDevice.ACTION_FOUND.equals(action)) {             BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);             mDeviceList.add(device);         } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {             // discovery has finished, give a call to fetchUuidsWithSdp on first device in list.             if (!mDeviceList.isEmpty()) {                 BluetoothDevice device = mDeviceList.remove(0);                 boolean result = device.fetchUuidsWithSdp();             }         } else if (BluetoothDevice.ACTION_UUID.equals(action)) {             // This is when we can be assured that fetchUuidsWithSdp has completed.             // So get the uuids and call fetchUuidsWithSdp on another device in list              BluetoothDevice deviceExtra = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);             Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);             System.out.println("DeviceExtra address - " + deviceExtra.getAddress());             if (uuidExtra != null) {                 for (Parcelable p : uuidExtra) {                     System.out.println("uuidExtra - " + p);                 }             } else {                 System.out.println("uuidExtra is still null");             }             if (!mDeviceList.isEmpty()) {                 BluetoothDevice device = mDeviceList.remove(0);                 boolean result = device.fetchUuidsWithSdp();             }         }     } } 

UPDATE: Latest android versions (mm & above) would result in triggering a pairing process with each device

like image 29
Arunkumar Avatar answered Sep 16 '22 20:09

Arunkumar