ble_heart_rate_simpletest.py
1 """ 2 Read heart rate data from a heart rate peripheral using the standard BLE 3 Heart Rate 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_heart_rate import HeartRateService 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 hr_connection = None 17 # Start with a fresh connection. 18 if ble.connected: 19 for connection in ble.connections: 20 if HeartRateService in connection: 21 connection.disconnect() 22 break 23 24 while True: 25 print("Scanning...") 26 for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): 27 if HeartRateService in adv.services: 28 print("found a HeartRateService advertisement") 29 hr_connection = ble.connect(adv) 30 print("Connected") 31 break 32 33 # Stop scanning whether or not we are connected. 34 ble.stop_scan() 35 print("Stopped scan") 36 37 if hr_connection and hr_connection.connected: 38 print("Fetch connection") 39 if DeviceInfoService in hr_connection: 40 dis = hr_connection[DeviceInfoService] 41 try: 42 manufacturer = dis.manufacturer 43 except AttributeError: 44 manufacturer = "(Manufacturer Not specified)" 45 try: 46 model_number = dis.model_number 47 except AttributeError: 48 model_number = "(Model number not specified)" 49 print("Device:", manufacturer, model_number) 50 else: 51 print("No device information") 52 hr_service = hr_connection[HeartRateService] 53 print("Location:", hr_service.location) 54 while hr_connection.connected: 55 print(hr_service.measurement_values) 56 time.sleep(1)