JuulLabs / kable

Kotlin Asynchronous Bluetooth Low-Energy

Home Page:https://juullabs.github.io/kable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NullPointerException: characteristic.value must not be null

twyatt opened this issue · comments

I'm getting several reports of this crash:

Fatal Exception: java.lang.NullPointerException: characteristic.value must not be null
       at com.juul.kable.gatt.Callback.onCharacteristicRead(Callback.kt:144)
       at android.bluetooth.BluetoothGatt$1$6.run(BluetoothGatt.java:405)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:201)
       at android.os.Looper.loop(Looper.java:288)
       at android.os.HandlerThread.run(HandlerThread.java:67)

Originally posted by @miwright2 in #485 (comment)

@miwright2 is this isolated to specific versions of Android?

@twyatt -- this is the report from the last 30 days. It looks spread between Android 9, Android 11, and Android 12. The device types are below:
image

@miwright2 I have a suspicion of what is going wrong.

My guess is that during a BLE I/O operation there was an error result (and to my surprise) Android may be providing null for the data ByteArray in the event an error occurred.

Unfortunately the error status is logged after trying to create the object that is throwing the NPE (so even if reproduced it would be harder to verify my suspicion — unless you attach debugger and can validate that null data occurs in the event of a BLE GATT error status):

val event = OnCharacteristicRead(characteristic, value, GattStatus(status))
logger.debug {
message = "onCharacteristicRead"
detail(characteristic)
detail(event.status)
detail(value)
}

I'll think about a good way to address this and try to get you a SNAPSHOT soon to try.

@miwright2 what version of Kable are you currently on? Asking to know what version I should work off of for providing you a SNAPSHOT (so that it has the smallest number of changes from your actively used version).

@twyatt I am on the previous snapshot with the ANR fix. I believe that was based on 0.23. I could try moving to 0.25 if you think that's a good idea.

If you're able to bump up to 0.25.0 then I'll make the snapshot based on that version. It would be preferred simply because other fixes may have made it into the latest version.

Let's do that approach and base it off of 0.25. Thanks for the support!

I'm pretty confident I found the cause of the crash: when upgrading the Android compile SDK (#455) the Android BLE callbacks got new I/O callbacks (these new callbacks do not allow null for the data) whereas the old deprecated callbacks sometimes provide null for I/O data — the old callbacks were forwarding to the new callbacks (for backwards compatibility).

Also explains why you're not seeing the crash on Android 13 (it isn't using the old deprecated BLE I/O callback functions).

Once #541 is approved, I plan to merge and release 0.25.1 with this fix; if you need it sooner, it is available in the following snapshot:

repositories {
    maven("https://oss.sonatype.org/content/repositories/snapshots")
}

dependencies {
    implementation("com.juul.kable:core:0.25.0-issue-540-1-SNAPSHOT")
}

This should be fixed in 0.25.1.