Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Fix long GATT read operations #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -142,15 +142,15 @@ public void run() {
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset,
BluetoothGattCharacteristic characteristic) {
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
byte [] value = characteristictem.getValue();
Log.d(TAG, "Device tried to read characteristic: " + characteristic.getUuid());
Log.d(TAG, "Value: " + Arrays.toString(characteristic.getValue()));
Log.d(TAG, "Value: " + Arrays.toString(value));
if (offset != 0) {
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset,
/* value (optional) */ null);
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
Arrays.copyOfRange(value, offset, value.length));
return;
}
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,
offset, characteristic.getValue());
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}

@Override
Expand Down Expand Up @@ -178,15 +178,15 @@ public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,
public void onDescriptorReadRequest(BluetoothDevice device, int requestId,
int offset, BluetoothGattDescriptor descriptor) {
super.onDescriptorReadRequest(device, requestId, offset, descriptor);
byte [] value = descriptor.getValue();
Log.d(TAG, "Device tried to read descriptor: " + descriptor.getUuid());
Log.d(TAG, "Value: " + Arrays.toString(descriptor.getValue()));
Log.d(TAG, "Value: " + Arrays.toString(value));
if (offset != 0) {
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset,
/* value (optional) */ null);
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
Copy link
Contributor

@g-ortuno g-ortuno Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of questions:

  1. Since we are using value.length chances are we are sending more bytes than necessary, could we fix that? If not then please comment why we use the length.
  2. I'm somewhat surprised of how we are using the API. I would have guessed that offset would indicate the Android API to only send the data starting from offset. The fact that we 1. send offset back and 2. only send part of the value is making me really suspicious. Could you confirm that simply removing this if statement isn't enough to fix the issue? If it's not enough then I think we can just make that the default behavior:
Log.d(TAG, "Value: " + Arrays.toString(value));
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
      // TODO: Comment why we use value.length
      Arrays.copyOfRange(value, offset, value.length)); 

Arrays.copyOfRange(value, offset, value.length));
return;
}
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
descriptor.getValue());
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}

@Override
Expand Down