New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bluetooth disconnects when calling writeCharacteristic on Android 13 #420
Comments
I found a native stack trace in the logs:
|
Are you using |
I am using |
Accepting data in chunks needs to be supported on the device side. I don't know what protocol are you using. If you have influence over the peripheral side, you may modify the firmware to support it. Usually, either the first write command contains a full length, or each packet has a flag "start/continuation/end/full" based on which it can either buffer, or sent to processing. Anyway, sending 540 bytes will not succeed even using Long Write, and I wonder how it used to work. |
In Android 12 logs I see the following lines, so it seems like Android 12 is allowing long writes over 512 bytes, but in Android 13 it's limited for some reason. Either way, it doesn't seem like something that can be fixed in this library.
|
According to the Bluetooth specification the maximum length of a characteristic is 512 bytes. Previously, Android did not enforce this limit but that has now been corrected. |
I had the same issue and resolved it once I limited the MTU to the size of 512 (or 509, given that 3 bytes are reserved). Thank you @weliem! Where can I find the specifications docs that you mentioned? |
We have made it work using |
Hi @philips77
Do you know if the first |
Hi, Smallest MTU is 23, so at least 20 bytes will always be sent.
I am aware of only 2 ways, that I described here: #420 (comment) |
These 2 statements appears to conflict with each other. If I send a string of 4 bytes is it guaranteed that it gets sent as a single request/packet? |
Yes, unless you're using some custom For example: This will send a single packet of 4 bytes: writeCharacteristic(c, byteArrayOf(1, 2, 3, 4), BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
.done { device, data -> /* ... */ }
.enqueue() This will send also a single packet, as 4 < MTU-3: writeCharacteristic(c, byteArrayOf(1, 2, 3, 4), BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
.split() // Uses default MTU splitter
.done { device, data -> /* ... */ }
.enqueue() This will send 4 packages, each of 1 byte: writeCharacteristic(c, byteArrayOf(1, 2, 3, 4), BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
.split(DataSplitter { message, index, _ -> byteArrayOf(message[index]) }))
.done { device, data -> /* ... */ }
.enqueue() |
This will send only MTU-3 bytes (by default 23-3=20) in a single packet, ignoring the rest: val array = ByteArray(1000) { it.toByte() }
writeCharacteristic(c, array, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
.done { device, data -> /* ... */ }
.enqueue() This, as it is using write with response, will send around 512 bytes or will crash: val array = ByteArray(1000) { it.toByte() }
writeCharacteristic(c, array, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
.done { device, data -> /* ... */ }
.enqueue() |
Thank you @philips77, you were super kind in explaining us all of this :) 🤝 |
Thanks a lot @philips77, that was very helpful. |
When I call BleManager.writeCharacteristic where the data has 543 bytes in the array, the call succeeds on Android 12. However, on Android 13 the bluetooth disconnects with the following logs:
There are a few earlier calls to writeCharacteristic with 1-2 bytes that succeed, it's only the call with the larger amount of data that fails due to the bluetooth disconnecting.
For reference, I'm targeting API 32 and using the following library versions:
Has anyone else run into this issue on Android 13?
The text was updated successfully, but these errors were encountered: