• 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# -----------------------------------------------------------------------------
18from bumble.device import Device
19from bumble.transport.common import PacketParser
20
21
22# -----------------------------------------------------------------------------
23class ScannerListener(Device.Listener):
24    def on_advertisement(self, advertisement):
25        address_type_string = ('P', 'R', 'PI', 'RI')[advertisement.address.address_type]
26        print(
27            f'>>> {advertisement.address} [{address_type_string}]: RSSI={advertisement.rssi}, {advertisement.ad_data}'
28        )
29
30
31class HciSource:
32    def __init__(self, host_source):
33        self.parser = PacketParser()
34        host_source.delegate = self
35
36    def set_packet_sink(self, sink):
37        self.parser.set_packet_sink(sink)
38
39    # host source delegation
40    def data_received(self, data):
41        print('*** DATA from JS:', data)
42        buffer = bytes(data.to_py())
43        self.parser.feed_data(buffer)
44
45
46# class HciSink:
47#     def __init__(self, host_sink):
48#         self.host_sink = host_sink
49
50#     def on_packet(self, packet):
51#         print(f'>>> PACKET from Python: {packet}')
52#         self.host_sink.on_packet(packet)
53
54
55# -----------------------------------------------------------------------------
56async def main(host_source, host_sink):
57    print('### Starting Scanner')
58    hci_source = HciSource(host_source)
59    hci_sink = host_sink
60    device = Device.with_hci('Bumble', 'F0:F1:F2:F3:F4:F5', hci_source, hci_sink)
61    device.listener = ScannerListener()
62    await device.power_on()
63    await device.start_scanning()
64
65    print('### Scanner started')
66