nrf5/examples: Adding example on how to use the ubluepy Scanner object in order to scan for a device name and find the address of the device. This can subsequently be used to perform a Central role connect() using the Peripheral object.
This commit is contained in:
parent
33b1028b50
commit
9568e07159
|
@ -0,0 +1,38 @@
|
|||
from ubluepy import Scanner
|
||||
|
||||
def bytes_to_str(bytes):
|
||||
string = ""
|
||||
for b in bytes:
|
||||
string += chr(b)
|
||||
return string
|
||||
|
||||
def get_device_names(scan_entries):
|
||||
dev_names = []
|
||||
for e in scan_entries:
|
||||
scan = e.getScanData()
|
||||
if scan:
|
||||
for s in scan:
|
||||
if s[0] == 9:
|
||||
dev_names.append((e, bytes_to_str(s[2])))
|
||||
return dev_names
|
||||
|
||||
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]
|
||||
|
||||
# >>> 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
|
Loading…
Reference in New Issue