openmv/scripts/examples/50-Arduino-Boards/Nano-33-BLE-Sense/10-Bluetooth/ble_scan.py
iabdalkader 98a29e0870 scripts/examples: Update examples.
- Add examples index.
- Remove RP2040's Bluetooth examples.
- Resort examples.
2023-10-29 21:21:55 +01:00

44 lines
1.0 KiB
Python

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
import time
from ubluepy import Scanner, constants
def bytes_to_str(bytes):
string = ""
for b in bytes:
string += chr(b)
return string
def get_device_names(scan_entries):
dev_names = []
print(len(scan_entries))
for e in scan_entries:
scan = e.getScanData()
for s in scan:
print(s)
if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME:
dev_names.append((e, bytes_to_str(s[2])))
return dev_names
def find_device_by_name(name):
s = Scanner()
scan_res = s.scan(1000)
device_names = get_device_names(scan_res)
for dev in device_names:
if name == dev[1]:
return dev[0]
while True:
res = find_device_by_name("micr")
if res:
print("address:", res.addr())
print("address type:", res.addr_type())
print("rssi:", res.rssi())
time.sleep_ms(500)