BLESession.matches: Support namePrefix in matching filters

Support "namePrefix" filter which is required for intelino Smart Train.
This commit is contained in:
ErrorJan
2020-07-30 21:59:53 +02:00
committed by Shin'ichiro Kawasaki
parent 20e65e9f03
commit a6972bb9a8

View File

@@ -434,13 +434,13 @@ class BLESession(Session):
def matches(self, dev, filters):
"""
Check if the found BLE device mathces the filters Scracth specifies.
Check if the found BLE device matches the filters Scratch specifies.
"""
logger.debug(f"in matches {dev} {filters}")
for f in filters:
if 'services' in f:
for s in f['services']:
logger.debug(f"sevice to check: {s}")
logger.debug(f"service to check: {s}")
given_uuid = s
logger.debug(f"given: {given_uuid}")
dev_uuid = self._get_dev_uuid(dev)
@@ -451,6 +451,16 @@ class BLESession(Session):
if given_uuid == dev_uuid:
logger.debug("match...")
return True
if 'namePrefix' in f:
# 0x08: Shortened Local Name
deviceName = dev.getValueText(0x08)
if not deviceName:
continue
logger.debug(f"Name of \"{deviceName}\" begins with: \"{f['namePrefix']}\"?")
if(deviceName.startswith(f['namePrefix'])):
logger.debug("Yes")
return True
logger.debug("No")
if 'name' in f or 'manufactureData' in f:
logger.error("name/manufactureData filters not implemented")
# TODO: implement other filters defined:
@@ -498,7 +508,7 @@ class BLESession(Session):
self.status = self.DONE
if len(self.found_devices) == 0 and not err_msg:
err_msg = (f"BLE service not found: {params['filters']}. "
err_msg = (f"No BLE device found: {params['filters']}. "
"Check BLE device.")
res["error"] = { "message": err_msg }
logger.error(err_msg)