Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Get Bluetooth UUID for this device

I was browing Stack and the internet for a simple solution to get the UUID of the device I'm currently using. I stumbled over posts like this but none of them seemed to help me.

The doc tells me about this getUuids() function but when going through the doc for Android Bluetooth I end up having a BluetoothAdapter but I need a BluetoothDevice to execute this function.

So I need to know the following:

1) Is the function returning really the device UUID? Because the name saids plural (getUuids)

2) How do I get an instance of this BluetoothDevice?

Thanks!

like image 945
Ron Avatar asked Oct 28 '13 09:10

Ron


People also ask

How do I find the UUID of a Bluetooth device Android?

Set<BluetoothDevice> devices = BluetoothAdapter. getDefaultAdapter(). getBondedDevices(); BluetoothDevice glass = null; for (BluetoothDevice d : devices { ParcelUuid[] uuids = d. getUuids(); for (ParcelUuid p : uuids) { System.

What is UUID in Ble Android?

UUID. Universally unique identifier, 128-bit number used to identify services, characteristics and descriptors.

What is UUID used for in Bluetooth?

In short, you can use UUID as a secret password for sharing files between any two Bluetooth devices.

How do I identify a unique Bluetooth device?

To find an active Bluetooth device, first make sure you have Bluetooth enabled on your smartphone. Next, download Wunderfind for your iPhone or Android device and launch the app. Immediately, you'll see a list of Bluetooth devices that your smartphone has detected using its built-in Bluetooth radio.


2 Answers

Using reflection you can invoke the hidden method getUuids() on the BluetoothAdater:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);

ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null);

for (ParcelUuid uuid: uuids) {
    Log.d(TAG, "UUID: " + uuid.getUuid().toString());
}

This is the result on a Nexus S:

UUID: 00001000-0000-1000-8000-00805f9b34fb
UUID: 00001001-0000-1000-8000-00805f9b34fb
UUID: 00001200-0000-1000-8000-00805f9b34fb
UUID: 0000110a-0000-1000-8000-00805f9b34fb
UUID: 0000110c-0000-1000-8000-00805f9b34fb
UUID: 00001112-0000-1000-8000-00805f9b34fb
UUID: 00001105-0000-1000-8000-00805f9b34fb
UUID: 0000111f-0000-1000-8000-00805f9b34fb
UUID: 0000112f-0000-1000-8000-00805f9b34fb
UUID: 00001116-0000-1000-8000-00805f9b34fb

where, for instance, 0000111f-0000-1000-8000-00805f9b34fb is for HandsfreeAudioGatewayServiceClass and 00001105-0000-1000-8000-00805f9b34fb is for OBEXObjectPushServiceClass. Actual availability of this method may depend on device and firmware version.

like image 109
Stefano S. Avatar answered Oct 18 '22 18:10

Stefano S.


To achieve this you must define the Bluetooth permission:

<uses-permission android:name="android.permission.BLUETOOTH"/>

Then you can invoke the method getUuids() using reflection:

    try {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);
    ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null);

         if(uuids != null) {
             for (ParcelUuid uuid : uuids) {
                 Log.d(TAG, "UUID: " + uuid.getUuid().toString());
             }
         }else{
             Log.d(TAG, "Uuids not found, be sure to enable Bluetooth!");
         }

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

You must enable bluetooth to get Uuids.

like image 23
Jorgesys Avatar answered Oct 18 '22 20:10

Jorgesys