weliem / blessed-android

BLESSED, a Bluetooth Low Energy (BLE) library for Android
MIT License
557 stars 119 forks source link

BluetoothPeripheralManager is incorrectly chopping long value #129

Closed ceduliocezar closed 2 years ago

ceduliocezar commented 2 years ago

TLDR; BluetoothPeripheralManager seems to be incorrectly chopping long values when BluetoothDevice.TRANSPORT_BREDR is used as transport. If BluetoothDevice.TRANSPORT_LE is used the long read works fine .

I ran several tests locally with two android phones and an iphone(central role). My observation is that when BluetoothDevice.TRANSPORT_BREDR is used the OS stack does not issue subsequent calls incrementing the offset. Both Android OS and iOS have this behavior.

The problem seems to be on BluetoothPeripheralManager.

Scenario

Steps to reproduce the problem:

val characteristic = BluetoothGattCharacteristic(CHARACTERISTIC_ID, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED)

characteristic.value = "Lorem ipsum dolor sit amet, consectetur adipiscing elit".toByteArray() 

Legal Imprint (Mandatory for legal reasons, sorry for that) Cedulio Silva cedulio.silva@daimler.com, MBition GmbH, imprint

ceduliocezar commented 2 years ago

With all that being said, my understanding is that should take care of chopping the beginning of the array based on the offset value and return the rest of the bytes. The end of the array is chopped on OS level.

I'm working on the fix and I will create a PR as soon I write the tests.

ceduliocezar commented 2 years ago

PR created https://github.com/weliem/blessed-android/pull/130

weliem commented 2 years ago

Merged PR and released version 2.1.4

I wonder if the same issue also occurs when doing long reads on descriptors.....because that uses the same implementation.

ceduliocezar commented 2 years ago

@weliem I did some testing reading long descriptor values and indeed it has the same problem when BREDR is used.

weliem commented 2 years ago

Ok, applied the same fix to descriptors in release 2.1.5.

Can you test and confirm it now also works for descriptors?