How to get the UUID of bluetooth on an Android device?

3

To establish a socket connection to my device via bluetooth I need to obtain the UUID identifier of the "server".

On the part of the client I can obtain the UUID's by means of the method getUuids () from BlueToothDevice

  

getUuids () Returns the supported characteristics (UUID) of the   remote device.

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
ParcelUuid[] uuids = device.getUuids();

But I want to get the UUID of the same device. Investigating there is not really an exposed method of the SDK for this purpose.

Is there an option?

    
asked by Elenasys 22.03.2018 в 17:17
source

2 answers

3

Obtaining the UUIDs designated to the device can be obtained through "reflection" using the same method getUuids () , for this we must have enabled BlueTooth on our device.

   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 no encontrados, asegura habilitar Bluetooth!");
         }

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

In this way you can get the UUID or related to the device with the following format:

0000XXXX-0000-X000-X8000-00X0XXXXXXXX

this to be used as an identifier:

private static final UUID deviceUUID = UUID.fromString("0000XXXX-0000-X000-X8000-00X0XXXXXXXX");

Do not forget to declare the permission within the file AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH"/>
    
answered by 22.03.2018 / 17:22
source
1

try:

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());
}
    
answered by 22.03.2018 в 18:31