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 random 22import logging 23 24from bumble.device import Device, Connection 25from bumble.transport import open_transport_or_link 26from bumble.gatt import Service, Characteristic 27 28 29# ----------------------------------------------------------------------------- 30class Listener(Device.Listener, Connection.Listener): 31 def __init__(self, device): 32 self.device = device 33 34 def on_connection(self, connection): 35 print(f'=== Connected to {connection}') 36 connection.listener = self 37 38 def on_disconnection(self, reason): 39 print(f'### Disconnected, reason={reason}') 40 41 def on_characteristic_subscription( 42 self, connection, characteristic, notify_enabled, indicate_enabled 43 ): 44 print( 45 f'$$$ Characteristic subscription for handle {characteristic.handle} ' 46 f'from {connection}: ' 47 f'notify {"enabled" if notify_enabled else "disabled"}, ' 48 f'indicate {"enabled" if indicate_enabled else "disabled"}' 49 ) 50 51 52# ----------------------------------------------------------------------------- 53# Alternative way to listen for subscriptions 54# ----------------------------------------------------------------------------- 55def on_my_characteristic_subscription(peer, enabled): 56 print(f'### My characteristic from {peer}: {"enabled" if enabled else "disabled"}') 57 58 59# ----------------------------------------------------------------------------- 60async def main(): 61 if len(sys.argv) < 3: 62 print('Usage: run_notifier.py <device-config> <transport-spec>') 63 print('example: run_notifier.py device1.json usb:0') 64 return 65 66 print('<<< connecting to HCI...') 67 async with await open_transport_or_link(sys.argv[2]) as (hci_source, hci_sink): 68 print('<<< connected') 69 70 # Create a device to manage the host 71 device = Device.from_config_file_with_hci(sys.argv[1], hci_source, hci_sink) 72 device.listener = Listener(device) 73 74 # Add a few entries to the device's GATT server 75 characteristic1 = Characteristic( 76 '486F64C6-4B5F-4B3B-8AFF-EDE134A8446A', 77 Characteristic.READ | Characteristic.NOTIFY, 78 Characteristic.READABLE, 79 bytes([0x40]), 80 ) 81 characteristic2 = Characteristic( 82 '8EBDEBAE-0017-418E-8D3B-3A3809492165', 83 Characteristic.READ | Characteristic.INDICATE, 84 Characteristic.READABLE, 85 bytes([0x41]), 86 ) 87 characteristic3 = Characteristic( 88 '8EBDEBAE-0017-418E-8D3B-3A3809492165', 89 Characteristic.READ | Characteristic.NOTIFY | Characteristic.INDICATE, 90 Characteristic.READABLE, 91 bytes([0x42]), 92 ) 93 characteristic3.on('subscription', on_my_characteristic_subscription) 94 custom_service = Service( 95 '50DB505C-8AC4-4738-8448-3B1D9CC09CC5', 96 [characteristic1, characteristic2, characteristic3], 97 ) 98 device.add_services([custom_service]) 99 100 # Debug print 101 for attribute in device.gatt_server.attributes: 102 print(attribute) 103 104 # Get things going 105 await device.power_on() 106 107 # Connect to a peer 108 if len(sys.argv) > 3: 109 target_address = sys.argv[3] 110 print(f'=== Connecting to {target_address}...') 111 await device.connect(target_address) 112 else: 113 await device.start_advertising(auto_restart=True) 114 115 while True: 116 await asyncio.sleep(3.0) 117 characteristic1.value = bytes([random.randint(0, 255)]) 118 await device.notify_subscribers(characteristic1) 119 characteristic2.value = bytes([random.randint(0, 255)]) 120 await device.indicate_subscribers(characteristic2) 121 characteristic3.value = bytes([random.randint(0, 255)]) 122 await device.notify_subscribers(characteristic3) 123 await device.indicate_subscribers(characteristic3) 124 125 126# ----------------------------------------------------------------------------- 127logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) 128asyncio.run(main()) 129