1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 23:41:45 +02:00

Delay descriptor/characteristic value set until write

If value is set when queued, two writes to the same descriptor during
a request will only write the second value.
This commit is contained in:
Erik Johansson
2018-04-30 00:14:46 +02:00
parent 07a1ebe6fe
commit 85e738c8b2

View File

@@ -62,8 +62,18 @@ public abstract class BluetoothCommunication {
private int cleanupStepNr; private int cleanupStepNr;
private BT_MACHINE_STATE btMachineState; private BT_MACHINE_STATE btMachineState;
private Queue<BluetoothGattDescriptor> descriptorRequestQueue; private class GattObjectValue <GattObject> {
private Queue<BluetoothGattCharacteristic> characteristicRequestQueue; public final GattObject gattObject;
public final byte[] value;
public GattObjectValue(GattObject gattObject, byte[] value) {
this.gattObject = gattObject;
this.value = value;
}
}
private Queue<GattObjectValue<BluetoothGattDescriptor>> descriptorRequestQueue;
private Queue<GattObjectValue<BluetoothGattCharacteristic>> characteristicRequestQueue;
private boolean openRequest; private boolean openRequest;
private final Object lock = new Object(); private final Object lock = new Object();
@@ -225,12 +235,11 @@ public abstract class BluetoothCommunication {
* @param bytes the bytes that should be write * @param bytes the bytes that should be write
*/ */
protected void writeBytes(UUID service, UUID characteristic, byte[] bytes) { protected void writeBytes(UUID service, UUID characteristic, byte[] bytes) {
BluetoothGattCharacteristic gattCharacteristic = bluetoothGatt.getService(service)
.getCharacteristic(characteristic);
gattCharacteristic.setValue(bytes);
synchronized (lock) { synchronized (lock) {
characteristicRequestQueue.add(gattCharacteristic); characteristicRequestQueue.add(
new GattObjectValue<>(
bluetoothGatt.getService(service).getCharacteristic(characteristic),
bytes));
handleRequests(); handleRequests();
} }
} }
@@ -266,15 +275,15 @@ public abstract class BluetoothCommunication {
protected void setIndicationOn(UUID service, UUID characteristic, UUID descriptor) { protected void setIndicationOn(UUID service, UUID characteristic, UUID descriptor) {
Timber.d("Set indication on for %s", characteristic); Timber.d("Set indication on for %s", characteristic);
BluetoothGattCharacteristic gattCharacteristic = bluetoothGatt.getService(service) BluetoothGattCharacteristic gattCharacteristic =
.getCharacteristic(characteristic); bluetoothGatt.getService(service).getCharacteristic(characteristic);
bluetoothGatt.setCharacteristicNotification(gattCharacteristic, true); bluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
synchronized (lock) { synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor); descriptorRequestQueue.add(
new GattObjectValue<>(
gattCharacteristic.getDescriptor(descriptor),
BluetoothGattDescriptor.ENABLE_INDICATION_VALUE));
handleRequests(); handleRequests();
} }
} }
@@ -288,15 +297,15 @@ public abstract class BluetoothCommunication {
protected void setNotificationOn(UUID service, UUID characteristic, UUID descriptor) { protected void setNotificationOn(UUID service, UUID characteristic, UUID descriptor) {
Timber.d("Set notification on for %s", characteristic); Timber.d("Set notification on for %s", characteristic);
BluetoothGattCharacteristic gattCharacteristic = bluetoothGatt.getService(service) BluetoothGattCharacteristic gattCharacteristic =
.getCharacteristic(characteristic); bluetoothGatt.getService(service).getCharacteristic(characteristic);
bluetoothGatt.setCharacteristicNotification(gattCharacteristic, true); bluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
synchronized (lock) { synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor); descriptorRequestQueue.add(
new GattObjectValue<>(
gattCharacteristic.getDescriptor(descriptor),
BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE));
handleRequests(); handleRequests();
} }
} }
@@ -310,15 +319,15 @@ public abstract class BluetoothCommunication {
protected void setNotificationOff(UUID service, UUID characteristic, UUID descriptor) { protected void setNotificationOff(UUID service, UUID characteristic, UUID descriptor) {
Timber.d("Set notification off for %s", characteristic); Timber.d("Set notification off for %s", characteristic);
BluetoothGattCharacteristic gattCharacteristic = bluetoothGatt.getService(service) BluetoothGattCharacteristic gattCharacteristic =
.getCharacteristic(characteristic); bluetoothGatt.getService(service).getCharacteristic(characteristic);
bluetoothGatt.setCharacteristicNotification(gattCharacteristic, false); bluetoothGatt.setCharacteristicNotification(gattCharacteristic, false);
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
synchronized (lock) { synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor); descriptorRequestQueue.add(
new GattObjectValue<>(
gattCharacteristic.getDescriptor(descriptor),
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE));
handleRequests(); handleRequests();
} }
} }
@@ -469,26 +478,30 @@ public abstract class BluetoothCommunication {
} }
// handle descriptor requests first // handle descriptor requests first
BluetoothGattDescriptor descriptorRequest = descriptorRequestQueue.poll(); GattObjectValue<BluetoothGattDescriptor> descriptor = descriptorRequestQueue.poll();
if (descriptorRequest != null) { if (descriptor != null) {
descriptor.gattObject.setValue(descriptor.value);
Timber.d("Write descriptor %s: %s", Timber.d("Write descriptor %s: %s",
descriptorRequest.getUuid(), byteInHex(descriptorRequest.getValue())); descriptor.gattObject.getUuid(), byteInHex(descriptor.gattObject.getValue()));
if (!bluetoothGatt.writeDescriptor(descriptorRequest)) { if (!bluetoothGatt.writeDescriptor(descriptor.gattObject)) {
Timber.e("Failed to initiate write of descriptor %s", Timber.e("Failed to initiate write of descriptor %s",
descriptorRequest.getUuid()); descriptor.gattObject.getUuid());
} }
openRequest = true; openRequest = true;
return; return;
} }
// handle characteristics requests second // handle characteristics requests second
BluetoothGattCharacteristic characteristicRequest = characteristicRequestQueue.poll(); GattObjectValue<BluetoothGattCharacteristic> characteristic = characteristicRequestQueue.poll();
if (characteristicRequest != null) { if (characteristic != null) {
characteristic.gattObject.setValue(characteristic.value);
Timber.d("Write characteristic %s: %s", Timber.d("Write characteristic %s: %s",
characteristicRequest.getUuid(), byteInHex(characteristicRequest.getValue())); characteristic.gattObject.getUuid(), byteInHex(characteristic.gattObject.getValue()));
if (!bluetoothGatt.writeCharacteristic(characteristicRequest)) { if (!bluetoothGatt.writeCharacteristic(characteristic.gattObject)) {
Timber.e("Failed to initiate write of characteristic %s", Timber.e("Failed to initiate write of characteristic %s",
characteristicRequest.getUuid()); characteristic.gattObject.getUuid());
} }
openRequest = true; openRequest = true;
return; return;
@@ -611,6 +624,5 @@ public abstract class BluetoothCommunication {
handleRequests(); handleRequests();
} }
} }
} }
} }