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
14
15from adafruit_ble_cycling_speed_and_cadence import CyclingSpeedAndCadenceService
16
17# Initialize the BLE radio
18ble = adafruit_ble.BLERadio()
19
20
21while True:
22    print("Scanning...")
23    # Save advertisements, indexed by address
24    advs = {}
25    for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
26        if CyclingSpeedAndCadenceService in adv.services:
27            print("found a CyclingSpeedAndCadenceService advertisement")
28            # Save advertisement. Overwrite duplicates from same address (device).
29            advs[adv.address] = adv
30
31    ble.stop_scan()
32    print("Stopped scanning")
33    if not advs:
34        # Nothing found. Go back and keep looking.
35        continue
36
37    # Connect to all available CSC sensors.
38    cyc_connections = []
39    for adv in advs.values():
40        cyc_connections.append(ble.connect(adv))
41        print("Connected", len(cyc_connections))
42
43    # Print out info about each sensors.
44    for conn in cyc_connections:
45        if conn.connected:
46            if DeviceInfoService in conn:
47                dis = conn[DeviceInfoService]
48                try:
49                    manufacturer = dis.manufacturer
50                except AttributeError:
51                    manufacturer = "(Manufacturer Not specified)"
52                print("Device:", manufacturer)
53            else:
54                print("No device information")
55
56    print("Waiting for data... (could be 10-20 seconds or more)")
57    # Get CSC Service from each sensor.
58    cyc_services = []
59    for conn in cyc_connections:
60        cyc_services.append(conn[CyclingSpeedAndCadenceService])
61
62    # Read data from each sensor once a second.
63    # Stop if we lose connection to all sensors.
64    while True:
65        still_connected = False
66        for conn, svc in zip(cyc_connections, cyc_services):
67            if conn.connected:
68                still_connected = True
69                print(svc.measurement_values)
70
71        if not still_connected:
72            break
73        time.sleep(1)