mirror of
https://github.com/google/bumble.git
synced 2026-04-18 00:45:32 +00:00
Add subscribe and unsubscribe commands to console.py (#22)
Subscribe command will enable notify or indicate events from the characteristic, depending on supported characteristic properties, and print received values to the output window. Unsubscribe will stop notify or indicate events. Rename find_attribute() to find_characteristic() and return a characteristic for a set of UUIDS, a characteristic for an attribute handle, or None. Print read and received values has a hex string. Add an unsubscribe implementation to gatt_client.py. Reset the CCCD bits to 0x0000. Remove a matching subsciber, if one is provided. Otherwise remove all subscribers for a characteristic, since no more notify or indicates events will be comming. authored-by: Michael Mogenson <mogenson@google.com>
This commit is contained in:
@@ -110,6 +110,9 @@ class CharacteristicProxy(AttributeProxy):
|
||||
async def subscribe(self, subscriber=None):
|
||||
return await self.client.subscribe(self, subscriber)
|
||||
|
||||
async def unsubscribe(self, subscriber=None):
|
||||
return await self.client.unsubscribe(self, subscriber)
|
||||
|
||||
def __str__(self):
|
||||
return f'Characteristic(handle=0x{self.handle:04X}, uuid={self.uuid}, properties={Characteristic.properties_as_string(self.properties)})'
|
||||
|
||||
@@ -548,6 +551,30 @@ class Client:
|
||||
|
||||
await self.write_value(cccd, struct.pack('<H', bits), with_response=True)
|
||||
|
||||
async def unsubscribe(self, characteristic, subscriber=None):
|
||||
# If we haven't already discovered the descriptors for this characteristic, do it now
|
||||
if not characteristic.descriptors_discovered:
|
||||
await self.discover_descriptors(characteristic)
|
||||
|
||||
# Look for the CCCD descriptor
|
||||
cccd = characteristic.get_descriptor(GATT_CLIENT_CHARACTERISTIC_CONFIGURATION_DESCRIPTOR)
|
||||
if not cccd:
|
||||
logger.warning('unsubscribing from characteristic with no CCCD descriptor')
|
||||
return
|
||||
|
||||
if subscriber is not None:
|
||||
# Remove matching subscriber from subscriber sets
|
||||
for subscriber_set in (self.notification_subscribers, self.indication_subscribers):
|
||||
subscribers = subscriber_set.get(characteristic.handle, [])
|
||||
if subscriber in subscribers:
|
||||
subscribers.remove(subscriber)
|
||||
else:
|
||||
# Remove all subscribers for this attribute from the sets!
|
||||
self.notification_subscribers.pop(characteristic.handle, None)
|
||||
self.indication_subscribers.pop(characteristic.handle, None)
|
||||
|
||||
await self.write_value(cccd, b'\x00\x00', with_response=True)
|
||||
|
||||
async def read_value(self, attribute, no_long_read=False):
|
||||
'''
|
||||
See Vol 3, Part G - 4.8.1 Read Characteristic Value
|
||||
|
||||
Reference in New Issue
Block a user