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 logging 20import sys 21import os 22from bumble.device import AdvertisingType, Device 23from bumble.hci import Address 24 25from bumble.transport import open_transport_or_link 26 27 28# ----------------------------------------------------------------------------- 29async def main(): 30 if len(sys.argv) < 3: 31 print( 32 'Usage: run_advertiser.py <config-file> <transport-spec> [type] [address]' 33 ) 34 print('example: run_advertiser.py device1.json usb:0') 35 return 36 37 if len(sys.argv) >= 4: 38 advertising_type = AdvertisingType(int(sys.argv[3])) 39 else: 40 advertising_type = AdvertisingType.UNDIRECTED_CONNECTABLE_SCANNABLE 41 42 if advertising_type.is_directed: 43 if len(sys.argv) < 5: 44 print('<address> required for directed advertising') 45 return 46 target = Address(sys.argv[4]) 47 else: 48 target = None 49 50 print('<<< connecting to HCI...') 51 async with await open_transport_or_link(sys.argv[2]) as (hci_source, hci_sink): 52 print('<<< connected') 53 54 device = Device.from_config_file_with_hci(sys.argv[1], hci_source, hci_sink) 55 await device.power_on() 56 await device.start_advertising(advertising_type=advertising_type, target=target) 57 await hci_source.wait_for_termination() 58 59 60# ----------------------------------------------------------------------------- 61logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) 62asyncio.run(main()) 63