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

Don't synchronize on Boolean object

Apparently this synchronizes on two different objects depending on if
the boolean is true or false as the boxed values are global instances.
This commit is contained in:
Erik Johansson
2018-04-18 20:55:39 +02:00
parent 0965f67a55
commit f276fd6106

View File

@@ -55,7 +55,8 @@ public abstract class BluetoothCommunication {
private Queue<BluetoothGattDescriptor> descriptorRequestQueue;
private Queue<BluetoothGattCharacteristic> characteristicRequestQueue;
private Boolean openRequest;
private boolean openRequest;
private final Object lock = new Object();
public BluetoothCommunication(Context context)
{
@@ -192,9 +193,10 @@ public abstract class BluetoothCommunication {
* @param btMachineState the machine state that should be set.
*/
protected void setBtMachineState(BT_MACHINE_STATE btMachineState) {
this.btMachineState = btMachineState;
handleRequests();
synchronized (lock) {
this.btMachineState = btMachineState;
handleRequests();
}
}
/**
@@ -209,7 +211,7 @@ public abstract class BluetoothCommunication {
.getCharacteristic(characteristic);
gattCharacteristic.setValue(bytes);
synchronized (openRequest) {
synchronized (lock) {
characteristicRequestQueue.add(gattCharacteristic);
handleRequests();
}
@@ -244,7 +246,7 @@ public abstract class BluetoothCommunication {
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
synchronized (openRequest) {
synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor);
handleRequests();
}
@@ -264,7 +266,7 @@ public abstract class BluetoothCommunication {
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
synchronized (openRequest) {
synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor);
handleRequests();
}
@@ -284,7 +286,7 @@ public abstract class BluetoothCommunication {
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
synchronized (openRequest) {
synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor);
handleRequests();
}
@@ -347,9 +349,13 @@ public abstract class BluetoothCommunication {
return;
}
if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE && doCleanup) {
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
nextMachineStateStep();
if (doCleanup) {
synchronized (lock) {
if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) {
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
nextMachineStateStep();
}
}
}
bluetoothGatt.disconnect();
@@ -384,7 +390,7 @@ public abstract class BluetoothCommunication {
}
private void handleRequests() {
synchronized (openRequest) {
synchronized (lock) {
// check for pending request
if (openRequest) {
return; // yes, do nothing
@@ -436,14 +442,16 @@ public abstract class BluetoothCommunication {
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
cmdStepNr = 0;
initStepNr = 0;
cleanupStepNr = 0;
synchronized (lock) {
cmdStepNr = 0;
initStepNr = 0;
cleanupStepNr = 0;
// Clear from possible previous setups
characteristicRequestQueue = new LinkedList<>();
descriptorRequestQueue = new LinkedList<>();
openRequest = false;
// Clear from possible previous setups
characteristicRequestQueue = new LinkedList<>();
descriptorRequestQueue = new LinkedList<>();
openRequest = false;
}
try {
// Sleeping a while after discovering services fixes connection problems.
@@ -463,7 +471,7 @@ public abstract class BluetoothCommunication {
public void onDescriptorWrite(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor,
int status) {
synchronized (openRequest) {
synchronized (lock) {
openRequest = false;
handleRequests();
}
@@ -473,7 +481,7 @@ public abstract class BluetoothCommunication {
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
synchronized (openRequest) {
synchronized (lock) {
openRequest = false;
handleRequests();
}
@@ -483,8 +491,8 @@ public abstract class BluetoothCommunication {
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
onBluetoothDataRead(gatt, characteristic, status);
synchronized (openRequest) {
synchronized (lock) {
onBluetoothDataRead(gatt, characteristic, status);
openRequest = false;
handleRequests();
}
@@ -493,7 +501,9 @@ public abstract class BluetoothCommunication {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
onBluetoothDataChange(gatt, characteristic);
synchronized (lock) {
onBluetoothDataChange(gatt, characteristic);
}
}
}
}