def scan_for_iBeacon_frames(duration_ms: int): """ Scan for iBeacon frames :param duration_ms: Time in milliseconds to scan for. """ # Configure scan for the entire window with ble.gap_scan(duration_ms=duration_ms, interval_us=2500, window_us=2500) as scan: iBeacon_count = 0 beacon_count = 0 while not scan.stopped(): for frame in scan.get(): beacon_count += 1 payload = frame['payload'] if iBeacon.is_proximity_beacon(payload): (uuid, power, major, minor) = iBeacon.parse_proximity_beacon(payload) # Calculate approximate distance calib_rssi = power - 256 rssi = frame['rssi'] db_ratio = float(calib_rssi - rssi) linear_ratio = 10.0**(db_ratio / 10.0) distance = linear_ratio**0.5 addr = binascii.hexlify(frame['address']).decode() uuid = binascii.hexlify(uuid).decode() print("") print("Got iBeacon from {}".format(addr)) print("distance={:0.2f} meters (approximate)".format( distance)) print("UUID={} major={} minor={}".format( uuid, major, minor)) print("RSSI={} calibrated_RSSI={} calibrated_power={}". format(rssi, calib_rssi, power)) iBeacon_count += 1 print("Found {} iBeacon frames out of a total {} beacon frames".format( iBeacon_count, beacon_count))
def _find_thunderboard(): scanner = ble.gap_scan(200, interval_us=2500, window_us=2500) for adv in scanner: if b"Thunder Sense" in adv['payload']: return adv['address'] return None
# SOFTWARE. from digi import ble def form_mac_address(addr: bytes) -> str: return ":".join('{:02x}'.format(b) for b in addr) # Turn on Bluetooth ble.active(True) print("Started Bluetooth with address of: {}".format(form_mac_address(ble.config("mac")))) scanner = None try: # Start a scan to run for 30 seconds scanner = ble.gap_scan(30000, interval_us=50000, window_us=50000) # Loop through all advertisements until the scan has stopped. for adv in scanner: print(adv) finally: if scanner is not None: scanner.stop() # Scan with a context manager this time. with ble.gap_scan(30000, interval_us=50000, window_us=50000) as scanner: # Loop through the available advertisements, blocking if there are none. Exits when the scan stops. for adv in scanner: print(adv) # Leaving the context would automatically end the scan by an implicit scanner.stop()
def find_advertiser(substring): scanner = ble.gap_scan(100, interval_us=2500, window_us=2500) for adv in scanner: if substring in adv['payload']: return adv['address'] return None