circuitpython/ports/nrf/boards/feather52/examples/ble_scan.py

27 lines
779 B
Python
Raw Normal View History

2018-01-15 10:04:30 -05:00
from ubluepy import Scanner, constants
2018-01-15 10:40:17 -05:00
def display_scan_results(scan_entries):
2018-01-15 10:04:30 -05:00
for e in scan_entries:
2018-01-15 10:40:17 -05:00
print("ADDR: ", e.addr())
print("TYPE: ", e.addr_type())
print("RSSI: ", e.rssi())
# Parse the contents of the advertising packet
2018-01-15 10:04:30 -05:00
scan = e.getScanData()
if scan:
for s in scan:
2018-01-15 10:40:17 -05:00
# Convert byte array to hex format string
hex = ' '.join('0x%02X' % b for b in s[2])
# Display enum value and hex string together
print('\t{}: {}'.format(s[1], hex))
# Line break between record sets
2018-01-15 10:04:30 -05:00
print("")
2018-01-15 10:40:17 -05:00
# Scan 1s for advertising devices in range
2018-01-15 10:04:30 -05:00
s = Scanner()
2018-01-15 10:40:17 -05:00
scan_res = s.scan(1000)
# Display the scan results
display_scan_results(scan_res)