Search code examples
javaandroidbluetoothgatt

Bluetooth GATT UUID


I'm trying to build an android app that uses the BluetoothLE. In tutorials they use a 128bit UUID but I only have the 16 bit UUID. I have to create a new 128 bit UUID using the service UUID and the Bluetooth Base.

Example :

  • Alert Notification Service UUID (16bit) => 0x1811
  • Bluetooth Base UUID (128bit) => 00000000-0000-1000-8000-00805F9B34FB

By combining those two UUIDs, we receive...

  • Alert Notification Service UUID (128bit) => 00001811-0000-1000-8000-00805F9B34FB

Is there a proper way to do this?


Solution

  • I use this:

    public class BLEUtils {
    
        public static final long BT_UUID_LOWER_BITS = 0x800000805F9B34FBl;
        public static final long BT_UUID_UPPER_BITS = 0x1000l;
        public static final long BT_CCCD_SHORT_UUID = 0x2902l;
        public static final UUID BT_CCCD_UUID = get16BitBTUUID(BT_CCCD_SHORT_UUID);
    
        public static UUID get16BitBTUUID(long uuid) {
            return new UUID(BT_UUID_UPPER_BITS + (uuid << 32), BT_UUID_LOWER_BITS);
        }
    }
    

    Your example looks sane. Is it recognized properly by other devices?

    Edit: The reverse operation requested in the comment would be:

    public static long getShortUuid(UUID uuid) {
        long result = uuid.getMostSignificantBits();
        result = result - BT_UUID_UPPER_BITS;
        result = result >> 32;
        return result;
    }
    

    I didn't test it though.