• 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
23
24from bumble.device import Device
25from bumble.transport import open_transport_or_link
26from bumble.core import BT_BR_EDR_TRANSPORT, BT_L2CAP_PROTOCOL_ID
27from bumble.sdp import (
28    Client as SDP_Client,
29    SDP_PUBLIC_BROWSE_ROOT,
30    SDP_ALL_ATTRIBUTES_RANGE,
31)
32
33
34# -----------------------------------------------------------------------------
35async def main():
36    if len(sys.argv) < 3:
37        print(
38            'Usage: run_classic_connect.py <device-config> <transport-spec> '
39            '<bluetooth-addresses..>'
40        )
41        print('example: run_classic_connect.py classic1.json usb:0 E1:CA:72:48:C4:E8')
42        return
43
44    print('<<< connecting to HCI...')
45    async with await open_transport_or_link(sys.argv[2]) as (hci_source, hci_sink):
46        print('<<< connected')
47
48        # Create a device
49        device = Device.from_config_file_with_hci(sys.argv[1], hci_source, hci_sink)
50        device.classic_enabled = True
51        await device.power_on()
52
53    async def connect(target_address):
54        print(f'=== Connecting to {target_address}...')
55        connection = await device.connect(target_address, transport=BT_BR_EDR_TRANSPORT)
56        print(f'=== Connected to {connection.peer_address}!')
57
58        # Connect to the SDP Server
59        sdp_client = SDP_Client(device)
60        await sdp_client.connect(connection)
61
62        # List all services in the root browse group
63        service_record_handles = await sdp_client.search_services(
64            [SDP_PUBLIC_BROWSE_ROOT]
65        )
66        print(color('\n==================================', 'blue'))
67        print(color('SERVICES:', 'yellow'), service_record_handles)
68
69        # For each service in the root browse group, get all its attributes
70        for service_record_handle in service_record_handles:
71            attributes = await sdp_client.get_attributes(
72                service_record_handle, [SDP_ALL_ATTRIBUTES_RANGE]
73            )
74            print(color(f'SERVICE {service_record_handle:04X} attributes:', 'yellow'))
75            for attribute in attributes:
76                print('  ', attribute.to_string(with_colors=True))
77
78        # Search for services with an L2CAP service attribute
79        search_result = await sdp_client.search_attributes(
80            [BT_L2CAP_PROTOCOL_ID], [SDP_ALL_ATTRIBUTES_RANGE]
81        )
82        print(color('\n==================================', 'blue'))
83        print(color('SEARCH RESULTS:', 'yellow'))
84        for attribute_list in search_result:
85            print(color('SERVICE:', 'green'))
86            print(
87                '  '
88                + '\n  '.join(
89                    [
90                        attribute.to_string(with_colors=True)
91                        for attribute in attribute_list
92                    ]
93                )
94            )
95
96        await sdp_client.disconnect()
97        await hci_source.wait_for_termination()
98
99    # Connect to a peer
100    target_addresses = sys.argv[3:]
101    await asyncio.wait(
102        [
103            asyncio.create_task(connect(target_address))
104            for target_address in target_addresses
105        ]
106    )
107
108
109# -----------------------------------------------------------------------------
110logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
111asyncio.run(main())
112