JuulLabs / kable

Kotlin Asynchronous Bluetooth Low-Energy
https://juullabs.github.io/kable
Apache License 2.0
788 stars 76 forks source link

peripheral.observe didUpdateNotificationStateForCharacteristic #653

Open DayDayUpAllInOne opened 5 months ago

DayDayUpAllInOne commented 5 months ago

When I use the code below,The kable library reports an error

actual suspend fun notify(device: BleDeviceEntity): Flow<ByteArray> {
    val notifyCharacteristic = characteristicOf(
      uuidFrom(bleUuid.serviceUuid).toString(),
      uuidFrom(bleUuid.notifyUuid).toString()
    )
    return device.peripheral.observe(notifyCharacteristic)
}

error message:

W/Kable/Delegate: 890FF1DD-33D3-AF4F-2CD8-7340E8C3B112 890FF1DD-33D3-AF4F-2CD8-7340E8C3B112 didUpdateNotificationStateForCharacteristic
  service: 8C858000-0302-41C5-B46E-CF057C562025
  characteristic: 8C858000-0302-4D46-BEC2-EE689169F628
  error: Error Domain=CBATTErrorDomain Code=10 "The attribute could not be found." UserInfo={NSLocalizedDescription=The attribute could not be found.}

error

How to solve this problem? Thanks!

twyatt commented 5 months ago

What version of Kable are you using?

DayDayUpAllInOne commented 5 months ago

What version of Kable are you using?

hi twyatt, the Kable version I use is 0.24.0, this problem occurs on iOS, Android is normal

DayDayUpAllInOne commented 5 months ago

I added the observationExceptionHandler method, and the program can receive the notification data from the Bluetooth device normally.

val peripheral = defaultScope.peripheral(it) {
    onServicesDiscovered {
        bleLogger.i("""BleClient/startScan/onServicesDiscovered:${it.peripheralName}""")
    }
    observationExceptionHandler { cause ->
        // Log failure instead of propagating associated `observe` flow.
        bleLogger.i("""BleClient/startScan/observationExceptionHandler:$cause""")
    }
}

Log:

Runner[1678:356710] W/Kable/Delegate: 890FF1DD-33D3-AF4F-2CD8-7340E8C3B112 890FF1DD-33D3-AF4F-2CD8-7340E8C3B112 didUpdateNotificationStateForCharacteristic
  service: 8C858000-0302-41C5-B46E-CF057C562025
  characteristic: 8C858000-0302-4D46-BEC2-EE689169F628
  error: Error Domain=CBATTErrorDomain Code=10 "The attribute could not be found." UserInfo={NSLocalizedDescription=The attribute could not be found.}
>>> info#ESIot/ble <<<BleClient/startScan/observationExceptionHandler:com.juul.kable.IOException: Error Domain=CBATTErrorDomain Code=10 "The attribute could not be found." UserInfo={NSLocalizedDescription=The attribute could not be found.}/1710152968286

Why does the program become normal after adding the observationExceptionHandler method?