Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

python 3.x - Bluetooth advertising and scan

Hello I'm hoping someone can help me with Bluetooth advertising & scanning. This is for a school stem project.

I'm working with a circuit playground Bluefruit development board and using circuit python. Trying to implement a social distancing badge - lights up when another user is too near. General idea is to set up an advertising beacon and then switch to scan.

I've had a go at code but would appreciate someone to review it. I'm not sure if the eddystoneuid works; when I'm scanning will it only scan for my specific uid for example.

Thank you

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()
question from:https://stackoverflow.com/questions/65877086/bluetooth-advertising-and-scan

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It appears that at the moment your code will trigger if detects any Bluetooth advertisement with an RSSI greater than -80 dBm. Is that what you want? Typically you would identify that the other device is broadcasting/advertising your notification service.

Typically a service like this would not use just the RSSI number. The advertisement data also includes the dBm that the advertisement was broadcast at. Using the difference between those two dBm numbers allows for a rough approximation of distance as explained in the following article: https://medium.com/personaldata-io/inferring-distance-from-bluetooth-signal-strength-a-deep-dive-fe7badc2bb6d If all of your devices are the same and broadcasting at the same dBm then calculating the distance is less important.

So in conclusion, instead of broadcasting emitter.address_bytes, I would put some specific number that all badges would broadcast. Then when you scan look in the advertisement.ServiceData for that number. Only if it is your exposure notification, calculate if it is too close.

For further reference, here is a link to the Google/Apple Exposure Notification Bluetooth? Specification that was released last year.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...