• 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 bumble.colors import color
23from bumble.device import Device, Peer
24from bumble.profiles.device_information_service import DeviceInformationServiceProxy
25from bumble.transport import open_transport
26
27
28# -----------------------------------------------------------------------------
29async def main():
30    if len(sys.argv) != 3:
31        print(
32            'Usage: device_information_client.py <transport-spec> <bluetooth-address>'
33        )
34        print('example: device_information_client.py usb:0 E1:CA:72:48:C4:E8')
35        return
36
37    print('<<< connecting to HCI...')
38    async with await open_transport(sys.argv[1]) as (hci_source, hci_sink):
39        print('<<< connected')
40
41        # Create and start a device
42        device = Device.with_hci('Bumble', 'F0:F1:F2:F3:F4:F5', hci_source, hci_sink)
43        await device.power_on()
44
45        # Connect to the peer
46        target_address = sys.argv[2]
47        print(f'=== Connecting to {target_address}...')
48        connection = await device.connect(target_address)
49        print(f'=== Connected to {connection}')
50
51        # Discover the Device Information service
52        peer = Peer(connection)
53        print('=== Discovering Device Information Service')
54        device_information_service = await peer.discover_service_and_create_proxy(
55            DeviceInformationServiceProxy
56        )
57
58        # Check that the service was found
59        if device_information_service is None:
60            print('!!! Service not found')
61            return
62
63        # Read and print the fields
64        if device_information_service.manufacturer_name is not None:
65            print(
66                color('Manufacturer Name:       ', 'green'),
67                await device_information_service.manufacturer_name.read_value(),
68            )
69        if device_information_service.model_number is not None:
70            print(
71                color('Model Number:            ', 'green'),
72                await device_information_service.model_number.read_value(),
73            )
74        if device_information_service.serial_number is not None:
75            print(
76                color('Serial Number:           ', 'green'),
77                await device_information_service.serial_number.read_value(),
78            )
79        if device_information_service.hardware_revision is not None:
80            print(
81                color('Hardware Revision:       ', 'green'),
82                await device_information_service.hardware_revision.read_value(),
83            )
84        if device_information_service.firmware_revision is not None:
85            print(
86                color('Firmware Revision:       ', 'green'),
87                await device_information_service.firmware_revision.read_value(),
88            )
89        if device_information_service.software_revision is not None:
90            print(
91                color('Software Revision:       ', 'green'),
92                await device_information_service.software_revision.read_value(),
93            )
94        if device_information_service.system_id is not None:
95            print(
96                color('System ID:               ', 'green'),
97                await device_information_service.system_id.read_value(),
98            )
99        if (
100            device_information_service.ieee_regulatory_certification_data_list
101            is not None
102        ):
103            print(
104                color('Regulatory Certification:', 'green'),
105                (
106                    # pylint: disable-next=line-too-long
107                    await device_information_service.ieee_regulatory_certification_data_list.read_value()
108                ).hex(),
109            )
110
111
112# -----------------------------------------------------------------------------
113logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
114asyncio.run(main())
115