@@ -92,6 +92,19 @@ final class OmiBleManager: NSObject {
9292 /// Queued scan request if Bluetooth wasn't ready when startScan was called.
9393 private var pendingScan : ( timeout : Int , serviceUuids : [ String ] ) ?
9494
95+ #if DEBUG
96+ private struct NotificationWindow {
97+ var count = 0
98+ var bytes = 0
99+ var maxValueLength = 0
100+ }
101+
102+ private var notificationWindowStartedAt = Date ( )
103+ private var notificationWindows : [ String : NotificationWindow ] = [ : ]
104+ private var pigeonNotificationsInFlight = 0
105+ private var peakPigeonNotificationsInFlight = 0
106+ #endif
107+
95108 // MARK: - Initialization
96109
97110 private override init ( ) {
@@ -930,6 +943,48 @@ final class OmiBleManager: NSObject {
930943 }
931944 operationSchedulers [ peripheralUuid ] ? . endSession ( )
932945 }
946+
947+ #if DEBUG
948+ private func recordNotificationMetric ( characteristicUuid : String , valueLength : Int ) {
949+ var window = notificationWindows [ characteristicUuid ] ?? NotificationWindow ( )
950+ window . count += 1
951+ window . bytes += valueLength
952+ window . maxValueLength = max ( window . maxValueLength , valueLength )
953+ notificationWindows [ characteristicUuid ] = window
954+
955+ let elapsed = Date ( ) . timeIntervalSince ( notificationWindowStartedAt )
956+ guard elapsed >= 5 else { return }
957+
958+ for ( characteristic , values ) in notificationWindows . sorted ( by : { $0 . key < $1 . key } ) {
959+ NSLog (
960+ " [OmiBleMetrics] window=%.3fs characteristic=%@ notifications=%d bytes=%d max_value=%d "
961+ + " notifications_per_sec=%.1f bytes_per_sec=%.1f pigeon_in_flight=%d pigeon_peak=%d " ,
962+ elapsed ,
963+ characteristic ,
964+ values . count ,
965+ values . bytes ,
966+ values . maxValueLength ,
967+ Double ( values . count ) / elapsed ,
968+ Double ( values . bytes ) / elapsed ,
969+ pigeonNotificationsInFlight ,
970+ peakPigeonNotificationsInFlight
971+ )
972+ }
973+ notificationWindows . removeAll ( keepingCapacity : true )
974+ notificationWindowStartedAt = Date ( )
975+ peakPigeonNotificationsInFlight = pigeonNotificationsInFlight
976+ }
977+
978+ private func recordPigeonNotificationSent ( ) {
979+ pigeonNotificationsInFlight += 1
980+ peakPigeonNotificationsInFlight = max (
981+ peakPigeonNotificationsInFlight , pigeonNotificationsInFlight )
982+ }
983+
984+ private func recordPigeonNotificationCompleted ( ) {
985+ pigeonNotificationsInFlight = max ( 0 , pigeonNotificationsInFlight - 1 )
986+ }
987+ #endif
933988}
934989
935990// MARK: - CBCentralManagerDelegate
@@ -1227,40 +1282,55 @@ extension OmiBleManager: CBPeripheralDelegate {
12271282 // Handle notification
12281283 guard let data = characteristic . value , ! data . isEmpty else { return }
12291284
1285+ #if DEBUG
1286+ recordNotificationMetric ( characteristicUuid : charUuid , valueLength : data . count )
1287+ #endif
1288+
12301289 if characteristic . uuid == OmiBleManager . batteryLevelCharUuid , let firstByte = data . first {
12311290 persistBatteryReading ( uuid : uuid , level : Int ( firstByte ) )
12321291 }
12331292
1234- // Limitless Transcribe Later: while batch mode targets this pendant's RX
1235- // characteristic, the flash-drain engine consumes the packet natively.
1236- if LimitlessFlashDrainEngine . shared . handle (
1237- peripheralUuid : uuid ,
1238- serviceUuid : serviceUuid ,
1239- characteristicUuid : charUuid ,
1240- value : data
1241- ) {
1242- return
1243- }
1244-
1245- // Batch (offline) mode: store audio natively and skip the Dart forward so the
1246- // Flutter engine stays idle. Returns true only for the configured audio
1247- // characteristic while batch mode is on; everything else falls through.
1248- if OmiBatchAudioWriter . shared . handle (
1249- peripheralUuid : uuid ,
1250- serviceUuid : serviceUuid ,
1251- characteristicUuid : charUuid ,
1252- value : data
1253- ) {
1254- return
1293+ // Route by fixed protocol identity before consulting native capture policy.
1294+ // Ring sync can deliver hundreds of notifications per second; asking both
1295+ // batch handlers to read UserDefaults before rejecting every storage packet
1296+ // needlessly occupies CoreBluetooth's main-queue callback.
1297+ switch BleNotificationRouter . route ( serviceUuid : serviceUuid , characteristicUuid : charUuid ) {
1298+ case . limitlessFlash :
1299+ if LimitlessFlashDrainEngine . shared . handle (
1300+ peripheralUuid : uuid ,
1301+ serviceUuid : serviceUuid ,
1302+ characteristicUuid : charUuid ,
1303+ value : data
1304+ ) {
1305+ return
1306+ }
1307+ case . batchAudio :
1308+ if OmiBatchAudioWriter . shared . handle (
1309+ peripheralUuid : uuid ,
1310+ serviceUuid : serviceUuid ,
1311+ characteristicUuid : charUuid ,
1312+ value : data
1313+ ) {
1314+ return
1315+ }
1316+ case . dart :
1317+ break
12551318 }
12561319
12571320 let typedData = FlutterStandardTypedData ( bytes : data )
1321+ #if DEBUG
1322+ recordPigeonNotificationSent ( )
1323+ #endif
12581324 flutterApi ? . onCharacteristicValueUpdated (
12591325 peripheralUuid : uuid ,
12601326 serviceUuid : serviceUuid ,
12611327 characteristicUuid : charUuid ,
12621328 value : typedData
1263- ) { _ in }
1329+ ) { [ weak self ] _ in
1330+ #if DEBUG
1331+ self ? . recordPigeonNotificationCompleted ( )
1332+ #endif
1333+ }
12641334 }
12651335
12661336 func peripheral (
0 commit comments