BLESession: handle device UUIDs as list

In the debug for the issue #30, it turned out that the bluepy API
ScanEntry.getValueText() for adtypes may return multiple UUIDs
concatenated with comma. Current code assumes that the API returns
single UUID, then multiple UUIDs cause the "Error: Non-hexadecimal digit
found" for the comma.

To fix it, replace ScanEntry.getValueText() call with getValue(), and
handle the result as a list of UUIDs. Also rename the helper function
_get_dev_uuid() to _get_dev_uuids() accordingly.

Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
This commit is contained in:
Shin'ichiro Kawasaki
2021-11-14 20:46:53 +09:00
parent 641b84a86e
commit 4558ea43df

View File

@@ -453,15 +453,14 @@ class BLESession(Session):
def __del__(self): def __del__(self):
self.close() self.close()
def _get_dev_uuid(self, dev): def _get_dev_uuids(self, dev):
for adtype in self.SERVICE_CLASS_UUID_ADTYPES: for adtype in self.SERVICE_CLASS_UUID_ADTYPES:
service_class_uuid = dev.getValueText(adtype) service_class_uuids = dev.getValue(adtype)
if service_class_uuid: if service_class_uuids:
for u in service_class_uuids:
a = self.SERVICE_CLASS_UUID_ADTYPES[adtype] a = self.SERVICE_CLASS_UUID_ADTYPES[adtype]
logger.debug(f"service class uuid for {a}/{adtype}: {service_class_uuid}") logger.debug(f"service class uuid for {a}/{adtype}: {u}")
uuid = UUID(service_class_uuid) return service_class_uuids
logger.debug(f"uuid: {uuid}")
return uuid
return None return None
def matches(self, dev, filters): def matches(self, dev, filters):
@@ -475,12 +474,13 @@ class BLESession(Session):
logger.debug(f"service to check: {s}") logger.debug(f"service to check: {s}")
given_uuid = s given_uuid = s
logger.debug(f"given UUID: {given_uuid} hash={UUID(given_uuid).__hash__()}") logger.debug(f"given UUID: {given_uuid} hash={UUID(given_uuid).__hash__()}")
dev_uuid = self._get_dev_uuid(dev) dev_uuids = self._get_dev_uuids(dev)
if not dev_uuid: if not dev_uuids:
continue continue
logger.debug(f"dev UUID: {dev_uuid} hash={dev_uuid.__hash__()}") for u in dev_uuids:
logger.debug(given_uuid == dev_uuid) logger.debug(f"dev UUID: {u} hash={u.__hash__()}")
if given_uuid == dev_uuid: logger.debug(given_uuid == u)
if given_uuid == u:
logger.debug("match...") logger.debug("match...")
return True return True
if 'namePrefix' in f: if 'namePrefix' in f: