Reputation: 940
I'm new to BLE development on Android, and I'm looking at the API docs and don't see a way to cancel a read/write characteristic/descriptor operation that has been "queued" to the remote device. How can I achieve that?
Specifically, after calling the following, how to I cancel the write after a timeout (handled elsewhere using an AsyncTask)?
private void writeCharacteristic(BluetoothGatt gatt) {
Log.i(TAG, "Writing to " + mCharacteristic);
characteristic.setValue(mPayload);
gatt.writeCharacteristic(mCharacteristic);
}
Upvotes: 0
Views: 975
Reputation: 18452
You can't. A Write Request is sent over to the remote device and it answers with a Write Response. When the Write Response is received, the onCharacteristicWrite callback is called. There is no "cancellation" specified in the BLE protocol. Instead a 30 second timeout is specified. If the remote device does not send a Write Response within 30 seconds, the link is dropped. This is implemented for you in Android's Bluetooth stack. Since there may also only be one outstanding request at a time with the GATT protocol, there is no way to "retry" the operation.
Upvotes: 3
Reputation: 2505
I would advice against handling operations this way as it is not guaranteed that any Bluetooth LE operation will succeed or fail in X amount of time. Factors such as distance, interference etc will all affect your operation.
You will get a definite result of your operation on your BluetoothGattCallback methods. For example:
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (status==BluetoothGatt.SUCCESS)
{
//operation completed successfully
}
else
{
//operation failed
}
}
All write/read operations for characteristics and descriptors deliver a result like that.
Upvotes: 0