BLE - decoding service UUID
I'm using an ESP32 to offer a service with a 128bit-UUID.
_HE_TB_UUID = UUID('04500100-39fd-49ec-b565-b5d6dc31b6ae') ... ...= self._ble.gatts_register_services(_SERVICES)
On the client side is a GPy with the 1.20 firmware. Here I use 'service.uuid()' from https://docs.pycom.io/firmwareapi/pycom/network/bluetooth/gattccservice/
to read the uuid of the service. I'm getting back this bytes object:
How can I decode that? The documentation only say '...but for 128-bit long UUIDs the value returned is a bytes object.' I've been through all permutations of struct.unpack but to no avail.
Thanks @poesel. We picked that up and wrapped it  into two corresponding routines for your convenience:
>>> from terkin.network.ble.util import BluetoothUuid >>> BluetoothUuid.from_bytes_le(BluetoothUuid.to_bytes_le('04500100-39fd-49ec-b565-b5d6dc31b6ae')) '04500100-39fd-49ec-b565-b5d6dc31b6ae'
You will also find some background information about why that happens within the comments. Thanks also to @jmarcelino for
FYI this is the function I came up to decode the UUID. Not nice but works. :)
def UUIDbytes2UUIDstring(uuid): ''' reverses the bytes of uuid and converts them to a properly formatted UUID string ''' from ubinascii import hexlify tmp = str(hexlify(bytes(reversed(uuid))))[2:34] return tmp[0:8]+'-'+tmp[8:12]+'-'+tmp[12:16]+'-'+tmp[16:20]+'-'+tmp[20:32]
The UUID library is not implemented in Micropython.
That's true. I used vanilla Python to demonstrate what @jcaron tried to point out here.
Hmm, sorry. I don't get it. Where do I find the UUID function?
The only one I can find is the one that reads it via bluetooth. The UUID library is not implemented in Micropython.
@andreas - Hallo Andreas! :)
Cool thanks, thats it.
@jcaron - compare it with a known UUID. This is for a non-standard service.
>>> UUID(bytes=b'\xae\xb61\xdc\xd6\xb5e\xb5\xecI\xfd9\x00\x01P\x04'[::-1]) UUID('04500100-39fd-49ec-b565-b5d6dc31b6ae')
@poesel hat do you actually want to do? Display the UUID in UUID format? Compare it with a known UUID? Something else?
Note that the bytes object has the bytes reversed if you compare that to the original UUID string. Other than that, it’s just a matter of formatting...