Skip to content

Instantly share code, notes, and snippets.

@ayman
Last active January 25, 2022 07:22
Show Gist options
  • Save ayman/eb06d8a4c7d35993ed39e4e22b6dd622 to your computer and use it in GitHub Desktop.
Save ayman/eb06d8a4c7d35993ed39e4e22b6dd622 to your computer and use it in GitHub Desktop.
Scan BLE packets from an Adafruit CLUE for a certain MAC address, get the advertisement data, repeat.
from adafruit_ble import BLERadio
# from adafruit_ble.advertising import Advertisement
# from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
found = set()
scan_responses = set()
running = True
count = 0
total_scans = 100
while running:
ble = BLERadio()
for advertisement in ble.start_scan():
# for advertisement in ble.start_scan(ProvideServicesAdvertisement, Advertisement):
addr = advertisement.address
if advertisement.scan_response and addr not in scan_responses:
scan_responses.add(addr)
elif not advertisement.scan_response and addr not in found:
found.add(addr)
else:
continue
address = []
for piece in addr.address_bytes:
z = str(hex(piece))[2:]
z = z if len(z) == 2 else "0" + z
address.append(z)
address.reverse()
address_string = ":".join(address)
if address_string == "aa:bb:cc:dd:ee:ff":
print(advertisement)
print(advertisement.data_dict)
break
# ble.stop_scan()
ble.stop_scan()
count += 1
found = set()
scan_responses = set()
running = False if count > total_scans else True
print("scan done")
@ayman
Copy link
Author

ayman commented Jan 25, 2022

This is a slight modification on the CircuitPython examples. Details scan can be made by flipping the commented blocks and for statement. You can comment out the reset of found = set() at the end of the loop if you don't want to rescan old devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment