From 20e65e9f03e2ca9f909753fcc9fabb9928112173 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Sat, 11 Jul 2020 14:06:47 +0900 Subject: [PATCH] BLESession: Support more adtypes It was reported that Lego WeDo2 advertises its service class UUIDs with adtype 0x6 "incomplete 128 bit service class UUIDs". Add this adtype as well as some more adtypes 0x5, 0x4 and 0x2 defined in Bluetooth spec [1]. Also introduce _get_dev_uuid() helper function to simplify the code. [1] https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/ Signed-off-by: Shin'ichiro Kawasaki --- scratch_link.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/scratch_link.py b/scratch_link.py index eacafe6..b12d5a9 100755 --- a/scratch_link.py +++ b/scratch_link.py @@ -326,8 +326,14 @@ class BLESession(Session): CONNECTED = 3 DONE = 4 - ADTYPE_COMP_16B = 0x3 - ADTYPE_COMP_128B = 0x7 + SERVICE_CLASS_UUID_ADTYPES = { + 0x7: "adtype complete 128b", + 0x3: "adtype complete 16b", + 0x6: "adtype incomplete 128b", + 0x5: "adtype complete 32b", + 0x4: "adtype incomplete 32b", + 0x2: "adtype incomplete 16b", + } class BLEThread(threading.Thread): """ @@ -418,6 +424,14 @@ class BLESession(Session): def __del__(self): self.close() + def _get_dev_uuid(self, dev): + for adtype in self.SERVICE_CLASS_UUID_ADTYPES: + service_class_uuid = dev.getValueText(adtype) + if service_class_uuid: + logger.debug(self.SERVICE_CLASS_UUID_ADTYPES[adtype]) + return UUID(service_class_uuid) + return None + def matches(self, dev, filters): """ Check if the found BLE device mathces the filters Scracth specifies. @@ -429,14 +443,9 @@ class BLESession(Session): logger.debug(f"sevice to check: {s}") given_uuid = s logger.debug(f"given: {given_uuid}") - service_class_uuid = dev.getValueText(self.ADTYPE_COMP_128B) - logger.debug(f"adtype 128b: {service_class_uuid}") - if not service_class_uuid: - service_class_uuid = dev.getValueText(self.ADTYPE_COMP_16B) - logger.debug(f"adtype 16b: {service_class_uuid}") - if not service_class_uuid: - continue - dev_uuid = UUID(service_class_uuid) + dev_uuid = self._get_dev_uuid(dev) + if not dev_uuid: + continue logger.debug(f"dev: {dev_uuid}") logger.debug(given_uuid == dev_uuid) if given_uuid == dev_uuid: