organize and improve examples

This commit is contained in:
Kavish Devar
2024-09-28 15:25:09 +05:30
parent cd7a8e4b46
commit 33051ec551
9 changed files with 118 additions and 94 deletions

View File

@@ -10,6 +10,7 @@ class NotificationListener:
BATTERY_UPDATED = 0x01
ANC_UPDATED = 0x02
EAR_DETECTION_UPDATED = 0x03
UNKNOWN = 0x00
def __init__(self, socket: BluetoothSocket, callback: callable):
self.socket = socket
@@ -25,11 +26,14 @@ class NotificationListener:
break
if self.BatteryNotification.isBatteryData(data):
self.BatteryNotification.setBattery(data)
self.callback(self.BATTERY_UPDATED)
self.callback(self.BATTERY_UPDATED, data)
pass
if self.EarDetectionNotification.isEarDetectionData(data):
self.EarDetectionNotification.setEarDetection(data)
self.callback(self.EAR_DETECTION_UPDATED)
self.callback(self.EAR_DETECTION_UPDATED, data)
else:
self.callback(self.UNKNOWN, data)
pass
pass
pass

View File

@@ -11,7 +11,7 @@ class Notifications:
BATTERY_UPDATED = NotificationListener.BATTERY_UPDATED
ANC_UPDATED = NotificationListener.ANC_UPDATED
EAR_DETECTION_UPDATED = NotificationListener.EAR_DETECTION_UPDATED
UNKNOWN = NotificationListener.UNKNOWN
def __init__(self, socket: bluetooth.BluetoothSocket, callback: callable):
self.socket = socket
self.notificationListener = NotificationListener(self.socket, callback)

View File

@@ -35,7 +35,7 @@ class Connection:
return False
return True
def notification_callback(self, notification_type: int):
def notification_callback(self, notification_type: int, data: bytes):
import logging
if notification_type == Notifications.BATTERY_UPDATED:
logging = logging.getLogger("Battery Status")
@@ -46,6 +46,11 @@ class Connection:
logging = logging.getLogger("In-Ear Status")
logging.debug(f'{self.notificationListener.EarDetectionNotification.getEarDetection()}')
pass
elif notification_type == Notifications.UNKNOWN:
logging = logging.getLogger("Unknown Notification")
hex_data = ' '.join(f'{byte:02x}' for byte in data)
logging.debug(f'{hex_data}')
pass
pass
def disconnect(self):