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