15

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?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Ron
  • 22,128
  • 31
  • 108
  • 206

2 Answers2

18

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.

Stefano S.
  • 242
  • 2
  • 3
  • 1
    I'm using SDK16 and it doesn't work for me. The `uuids` are `null`. Furthermore I'd like to have just my Bluetooth uuid. Do you know how to get this one? – Ron Oct 29 '13 at 08:08
  • There is an uuid for each Bluetooth service exposed by the device; that's why you see multiple uuids for the smartphone. I performed the tests using SDK on Nexus S with Android 4.1.2 and Galaxy Nexus with Android 4.3. Which device did you use? – Stefano S. Oct 29 '13 at 09:34
  • Galaxy S2... do I need to have Bluetooth active to retrieve my uuid? – Ron Oct 29 '13 at 11:44
  • 1
    Bluetooth has to be on. I tested on a Galaxy S2 with Android 4.1.2 and it worked, showing 12 UUIDs. – Stefano S. Oct 29 '13 at 13:10
  • 2
    Ok, thats great so far - thanks :) But how do I get THE uuid representing my phone? Or do I need all of them for identifying my devide? – Ron Nov 04 '13 at 15:08
  • AFAIK, the UUID identifies a service (OBEX, printing, serial port and so on), while the device type is identified by the Device Class (Major and Minor), which is part of the data you get during the discovery process. A Bluetooth UUID consist of a base (00000000-0000-1000-8000-00805F9B34FB) and an identifier (the first block) for any different service (for instance, 0x1101 for the serial port). However, I can't understand what is the purpose: why do you need to know the class of your device? – Stefano S. Nov 08 '13 at 21:40
  • can you explain which instance use for Bluetooth adapter remote device connectivity? – Majid Golshadi Jan 15 '14 at 12:05
  • 1
    @Ron, have you got what you wanted? I also want to ask. Is it okay if i define my own UUID for two different android devices to communicate each other? I mean, wouldn't it be conflict if I use the same UUID for two different device? – gumuruh Jul 07 '14 at 07:24
  • "java.lang.reflect.InvocationTargetException" when executing (ParcelUuid[]) getUuidsMethod.invoke(adapter, null). I'm on Galaxy Note2 with Android 4.2 – LiangWang May 18 '15 at 11:20
  • From Android 4.0.x upwards, you can use public API, there is no need to use reflection. – Stefano S. May 25 '15 at 09:27
  • `java.lang.NoSuchMethodException: parameter type is null` - "getUuids" – user924 Aug 09 '19 at 12:23
2

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.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • java.lang.NoSuchMethodException: parameter type is null - "getUuids" – user924 Aug 09 '19 at 12:24
  • How can I know which uuid listed belongs to the device I want to pair? I can discover a device with device.getAddress() that returns something like: 24:A0:C4:BF:5C:F2 on the other hand using your code I get licas of 3 UUID each like this: 0000110a-0000-1000-8000-00805f9b34fb only differences in the first block. I want to call createInsecureRfcommSocketToServiceRecord(UUID) son can pair Android to a ESP32 device. – LucasRT Oct 15 '19 at 22:53