Simple test

Ensure your device works with this simple test.

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