您好我希望有人可以帮助我进行蓝牙广告和扫描。这是一个学校干项目。
我正在使用电路游乐场 Bluefruit 开发板并使用电路 python。尝试实施社交距离徽章 - 当另一个用户离得太近时会亮起。总体思路是设置一个广告信标,然后切换到扫描。
我已经尝试过代码,但希望有人对其进行审查。我不确定 eddystoneuid 是否有效;例如,当我扫描时,它只会扫描我的特定 uid。
谢谢
import time
from adafruit_circuitplayground.bluefruit import cpb
import adafruit_ble
from adafruit_ble_eddystone import uid
emitter = adafruit_ble.BLERadio()
advertisement = uid.EddystoneUID(emitter.address_bytes)
receiver = adafruit_ble.BLERadio()
while cpb.switch:
# only if the switch is to the leftt the code will work
# so there's an option to turn the board off if needed.
emitter.start_advertising(advertisement)
time.sleep(0.5)
emitter.stop_advertising()
# advertises its "address" almost every half second
for advertisement in receiver.start_scan():
# then the same board scans for advertisements after
rssi = advertisement.rssi
if advertisement.rssi > -80:
# if the rssi is stronger than -80 (around 2m, yet to be certain)
cpb.pixels.fill((250, 0, 0))
time.sleep(0.2)
cpb.pixels.fill((250, 0, 0))
time.sleep(0.2)
cpb.pixels.fill((250, 0, 0))
cpb.pixels.fill((0, 0, 0))
# then the neopixels flash bright red three times, then turn off
receiver.stop_scan()