From caa344ecbbd5d1aac9fc8421c128f615b318f1c3 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Sun, 25 Jun 2023 21:23:00 +0900 Subject: [PATCH] scratch_link.py: Support COMPLETE_LOCAL_NAME for 'namePrefix' filter Current implementation of match() does filter matching for 'namePrefix' keyword only for SHORT_LOCAL_NAME of the device. When the device does not provide SHORT_LOCAL_NAME but provides COMPLETE_LOCAL_NAME, match() for 'namePrefix' fails. For example, MicroBit More v2 [1] does not provide the SHORT_LOCAL_NAME. To support 'namePrefix' filter match with COMPLETE_LOCAL_NAME, add support for it. While at it, replace the SHORT_LOCAL_NAME identifier constant with the definition in btle.ScanEntry. [1] https://lab.yengawa.com/project/microbit-more-v2/ Signed-off-by: Shin'ichiro Kawasaki --- pyscrlink/scratch_link.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pyscrlink/scratch_link.py b/pyscrlink/scratch_link.py index 9b2d7f2..ac7dd66 100755 --- a/pyscrlink/scratch_link.py +++ b/pyscrlink/scratch_link.py @@ -18,7 +18,7 @@ import traceback import argparse # for BLESession (e.g. BBC micro:bit) -from bluepy.btle import Scanner, UUID, Peripheral, DefaultDelegate +from bluepy.btle import Scanner, UUID, Peripheral, DefaultDelegate, ScanEntry from bluepy.btle import BTLEDisconnectError, BTLEManagementError from pyscrlink import bluepy_helper_cap @@ -292,15 +292,19 @@ class BLESession(Session): 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") + logger.debug(f"given namePrefix: {f['namePrefix']}") + deviceName = dev.getValueText(ScanEntry.SHORT_LOCAL_NAME) + if deviceName: + logger.debug(f"SHORT_LOCAL_NAME: {deviceName}") + if deviceName.startswith(f['namePrefix']): + logger.debug(f"match...") + return True + deviceName = dev.getValueText(ScanEntry.COMPLETE_LOCAL_NAME) + if deviceName: + logger.debug(f"COMPLETE_LOCAL_NAME: {deviceName}") + if deviceName.startswith(f['namePrefix']): + logger.debug(f"match...") + return True if 'name' in f or 'manufactureData' in f: logger.error("name/manufactureData filters not implemented") # TODO: implement other filters defined: