• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# -----------------------------------------------------------------------------
18import asyncio
19import sys
20import os
21import logging
22from colors import color
23
24from bumble.core import ProtocolError
25from bumble.device import Device, Peer
26from bumble.gatt import show_services
27from bumble.transport import open_transport_or_link
28from bumble.utils import AsyncRunner
29
30
31# -----------------------------------------------------------------------------
32class Listener(Device.Listener):
33    def __init__(self, device):
34        self.device = device
35
36    @AsyncRunner.run_in_task()
37    async def on_connection(self, connection):
38        print(f'=== Connected to {connection}')
39
40        # Discover all services
41        print('=== Discovering services')
42        peer = Peer(connection)
43        await peer.discover_services()
44        for service in peer.services:
45            await service.discover_characteristics()
46            for characteristic in service.characteristics:
47                await characteristic.discover_descriptors()
48
49        print('=== Services discovered')
50        show_services(peer.services)
51
52        # Discover all attributes
53        print('=== Discovering attributes')
54        attributes = await peer.discover_attributes()
55        for attribute in attributes:
56            print(attribute)
57        print('=== Attributes discovered')
58
59        # Read all attributes
60        for attribute in attributes:
61            try:
62                value = await peer.read_value(attribute)
63                print(color(f'0x{attribute.handle:04X} = {value.hex()}', 'green'))
64            except ProtocolError as error:
65                print(color(f'cannot read {attribute.handle:04X}:', 'red'), error)
66            except TimeoutError:
67                print(color('read timeout'))
68
69
70# -----------------------------------------------------------------------------
71async def main():
72    if len(sys.argv) < 3:
73        print('Usage: run_gatt_client.py <device-config> <transport-spec> [<bluetooth-address>]')
74        print('example: run_gatt_client.py device1.json usb:0 E1:CA:72:48:C4:E8')
75        return
76
77    print('<<< connecting to HCI...')
78    async with await open_transport_or_link(sys.argv[2]) as (hci_source, hci_sink):
79        print('<<< connected')
80
81        # Create a device to manage the host, with a custom listener
82        device = Device.from_config_file_with_hci(sys.argv[1], hci_source, hci_sink)
83        device.listener = Listener(device)
84        await device.power_on()
85
86        # Connect to a peer
87        if len(sys.argv) > 3:
88            target_address = sys.argv[3]
89            print(f'=== Connecting to {target_address}...')
90            await device.connect(target_address)
91        else:
92            await device.start_advertising()
93
94        await asyncio.get_running_loop().create_future()
95
96# -----------------------------------------------------------------------------
97logging.basicConfig(level = os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
98asyncio.run(main())
99