Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 1 | # Copyright 2021-2022 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # ----------------------------------------------------------------------------- |
| 16 | # Imports |
| 17 | # ----------------------------------------------------------------------------- |
| 18 | import asyncio |
| 19 | import sys |
| 20 | import os |
| 21 | import logging |
uael | d21da78 | 2023-02-23 20:16:33 +0000 | [diff] [blame] | 22 | from bumble.colors import color |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 23 | from bumble.device import Device |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 24 | from bumble.transport import open_transport |
| 25 | from bumble.profiles.heart_rate_service import HeartRateServiceProxy |
| 26 | |
| 27 | |
| 28 | # ----------------------------------------------------------------------------- |
| 29 | async def main(): |
| 30 | if len(sys.argv) != 3: |
| 31 | print('Usage: heart_rate_client.py <transport-spec> <bluetooth-address>') |
| 32 | print('example: heart_rate_client.py usb:0 E1:CA:72:48:C4:E8') |
| 33 | return |
| 34 | |
| 35 | print('<<< connecting to HCI...') |
| 36 | async with await open_transport(sys.argv[1]) as (hci_source, hci_sink): |
| 37 | print('<<< connected') |
| 38 | |
| 39 | # Create and start a device |
| 40 | device = Device.with_hci('Bumble', 'F0:F1:F2:F3:F4:F5', hci_source, hci_sink) |
| 41 | await device.power_on() |
| 42 | |
| 43 | # Connect to the peer |
| 44 | target_address = sys.argv[2] |
| 45 | print(f'=== Connecting to {target_address}...') |
Jayson Messenger | 9cd1890 | 2022-07-26 10:34:37 -0400 | [diff] [blame] | 46 | async with device.connect_as_gatt(target_address) as peer: |
| 47 | print(f'=== Connected to {peer}') |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 48 | |
Jayson Messenger | 9cd1890 | 2022-07-26 10:34:37 -0400 | [diff] [blame] | 49 | heart_rate_service = peer.create_service_proxy(HeartRateServiceProxy) |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 50 | |
Jayson Messenger | 9cd1890 | 2022-07-26 10:34:37 -0400 | [diff] [blame] | 51 | # Check that the service was found |
| 52 | if not heart_rate_service: |
| 53 | print('!!! Service not found') |
| 54 | return |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 55 | |
Jayson Messenger | 9cd1890 | 2022-07-26 10:34:37 -0400 | [diff] [blame] | 56 | # Read the body sensor location |
| 57 | if heart_rate_service.body_sensor_location: |
| 58 | location = await heart_rate_service.body_sensor_location.read_value() |
| 59 | print(color('Sensor Location:', 'green'), location) |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 60 | |
Jayson Messenger | 9cd1890 | 2022-07-26 10:34:37 -0400 | [diff] [blame] | 61 | # Subscribe to the heart rate measurement |
| 62 | if heart_rate_service.heart_rate_measurement: |
| 63 | await heart_rate_service.heart_rate_measurement.subscribe( |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 64 | lambda value: print( |
| 65 | f'{color("Heart Rate Measurement:", "green")} {value}' |
| 66 | ) |
Jayson Messenger | 9cd1890 | 2022-07-26 10:34:37 -0400 | [diff] [blame] | 67 | ) |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 68 | |
Jayson Messenger | 9cd1890 | 2022-07-26 10:34:37 -0400 | [diff] [blame] | 69 | await peer.sustain() |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 73 | logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) |
Gilles Boccon-Gibod | 3040df3 | 2022-07-23 09:38:44 -0700 | [diff] [blame] | 74 | asyncio.run(main()) |