circuitpython/ports/nrf/examples/ubluepy_scan.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
929 B
Python
Raw Normal View History

from ubluepy import Scanner, constants
2021-03-15 09:57:36 -04:00
def bytes_to_str(bytes):
string = ""
for b in bytes:
string += chr(b)
return string
2021-03-15 09:57:36 -04:00
def get_device_names(scan_entries):
dev_names = []
for e in scan_entries:
scan = e.getScanData()
if scan:
for s in scan:
2021-03-15 09:57:36 -04:00
if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME:
dev_names.append((e, bytes_to_str(s[2])))
return dev_names
2021-03-15 09:57:36 -04:00
def find_device_by_name(name):
s = Scanner()
scan_res = s.scan(100)
device_names = get_device_names(scan_res)
for dev in device_names:
if name == dev[1]:
return dev[0]
2021-03-15 09:57:36 -04:00
# >>> res = find_device_by_name("micr")
# >>> if res:
# ... print("address:", res.addr())
# ... print("address type:", res.addr_type())
# ... print("rssi:", res.rssi())
# ...
# ...
# ...
# address: c2:73:61:89:24:45
# address type: 1
# rssi: -26