ble_cycling_speed_and_cadence_simpletest.py
1 """ 2 Read cycling speed and cadence data from a peripheral using the standard BLE 3 Cycling Speed and Cadence (CSC) Service. 4 """ 5 6 import time 7 8 import adafruit_ble 9 from adafruit_ble.advertising.standard import ProvideServicesAdvertisement 10 from adafruit_ble.services.standard.device_info import DeviceInfoService 11 from adafruit_ble_cycling_speed_and_cadence import CyclingSpeedAndCadenceService 12 13 # PyLint can't find BLERadio for some reason so special case it here. 14 ble = adafruit_ble.BLERadio() # pylint: disable=no-member 15 16 17 while True: 18 print("Scanning...") 19 # Save advertisements, indexed by address 20 advs = {} 21 for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): 22 if CyclingSpeedAndCadenceService in adv.services: 23 print("found a CyclingSpeedAndCadenceService advertisement") 24 # Save advertisement. Overwrite duplicates from same address (device). 25 advs[adv.address] = adv 26 27 ble.stop_scan() 28 print("Stopped scanning") 29 if not advs: 30 # Nothing found. Go back and keep looking. 31 continue 32 33 # Connect to all available CSC sensors. 34 cyc_connections = [] 35 for adv in advs.values(): 36 cyc_connections.append(ble.connect(adv)) 37 print("Connected", len(cyc_connections)) 38 39 # Print out info about each sensors. 40 for conn in cyc_connections: 41 if conn.connected: 42 if DeviceInfoService in conn: 43 dis = conn[DeviceInfoService] 44 try: 45 manufacturer = dis.manufacturer 46 except AttributeError: 47 manufacturer = "(Manufacturer Not specified)" 48 print("Device:", manufacturer) 49 else: 50 print("No device information") 51 52 print("Waiting for data... (could be 10-20 seconds or more)") 53 # Get CSC Service from each sensor. 54 cyc_services = [] 55 for conn in cyc_connections: 56 57 cyc_services.append(conn[CyclingSpeedAndCadenceService]) 58 59 # Read data from each sensor once a second. 60 # Stop if we lose connection to all sensors. 61 while True: 62 still_connected = False 63 for conn, svc in zip(cyc_connections, cyc_services): 64 if conn.connected: 65 still_connected = True 66 print(svc.measurement_values) 67 68 if not still_connected: 69 break 70 time.sleep(1)