Skip to content

Commit

Permalink
BLESession: Add _getDevName() helper function
Browse files Browse the repository at this point in the history
GitHub issue #31 reported that Intelino Smart Train is not connected
with pyscrlink. It also reported that getValueText(0x9) call for the
device instead of getValueText(0x8) avoids the failure. This implies
that the Intelino Smart Train does not report AdType 0x8 "Shortened
local name" but it reports AdType 0x9 "Complete local name".

To get local name of the BLE devices regardless AdType support status
of those devices, introduce a helper function _getDevName(). It gets the
AdType 0x9 "Complete local name" first. If it is not available, get
AdType 0x8 "Shortened local name". Use this helper function at all
places to get the device name.

Signed-off-by: Shin'ichiro Kawasaki <[email protected]>
  • Loading branch information
kawasaki committed Dec 31, 2021
1 parent 3cf61a1 commit 48e460b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pyscrlink/scratch_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ class BLESession(Session):
scan_lock = threading.RLock()
scan_started = False

@staticmethod
def _getDevName(dev):
"""
Get AdType 0x09 (Completed local name). If it is not available,
get AdType 0x08 (Shortened local name).
"""
return dev.getValueText(0x9) or dev.getValueText(0x8)

class BLEThread(threading.Thread):
"""
Separated thread to control notifications to Scratch.
Expand All @@ -381,7 +389,7 @@ def run(self):
for d in devices:
params = { 'rssi': d.rssi }
params['peripheralId'] = devices.index(d)
params['name'] = d.getValueText(0x9) or d.getValueText(0x8)
params['name'] = BLESession._getDevName(d)
self.session.notify('didDiscoverPeripheral', params)
time.sleep(1)
elif self.session.status == self.session.CONNECTED:
Expand Down Expand Up @@ -504,7 +512,7 @@ def matches(self, dev, filters):
return True
if 'namePrefix' in f:
# 0x08: Shortened Local Name
deviceName = dev.getValueText(0x08)
deviceName = self._getDevName(dev)
if not deviceName:
continue
logger.debug(f"Name of \"{deviceName}\" begins with: \"{f['namePrefix']}\"?")
Expand Down Expand Up @@ -620,7 +628,7 @@ def handle_request(self, method, params):
elif self.status == self.DISCOVERY and method == 'connect':
logger.debug("connecting to the BLE device")
self.device = BLESession.found_devices[params['peripheralId']]
self.deviceName = self.device.getValueText(0x9) or self.device.getValueText(0x8)
self.deviceName = self._getDevName(self.device)
try:
self.perip = Peripheral(self.device)
logger.info(f"connected to the BLE peripheral: {self.deviceName}")
Expand Down

0 comments on commit 48e460b

Please sign in to comment.