问题描述
您好,我希望有人可以帮助我进行蓝牙广告和扫描。这是一个学校项目。
我正在使用电路游乐场 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))
time.sleep(0.2)
cpb.pixels.fill((250,0))
cpb.pixels.fill((0,0))
# then the neopixels flash bright red three times,then turn off
receiver.stop_scan()
解决方法
目前看来,如果检测到任何 RSSI 大于 -80 dBm 的蓝牙广告,您的代码就会触发。那是你要的吗?通常,您会发现其他设备正在广播/广告您的通知服务。
通常,这样的服务不会只使用 RSSI 编号。广告数据还包括广播广告的 dBm。使用这两个 dBm 数字之间的差异可以粗略估计距离,如以下文章中所述:https://medium.com/personaldata-io/inferring-distance-from-bluetooth-signal-strength-a-deep-dive-fe7badc2bb6d 如果您的所有设备都相同并且以相同的 dBm 进行广播,那么计算距离就不那么重要了。
总而言之,我不会广播 emitter.address_bytes
,而是输入所有徽章都会广播的特定数字。然后,当您扫描该号码时,请查看 advertisement.ServiceData
。仅当是你的暴露通知时,计算是否离得太近。
为了进一步参考,这里是去年发布的 Google/Apple Exposure Notification Bluetooth® Specification 的链接。