1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-22 08:13:43 +02:00

Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate
2018-04-18 20:56:47 +02:00
14 changed files with 79 additions and 66 deletions

View File

@@ -146,7 +146,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
@@ -245,7 +245,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
@@ -274,7 +274,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
// Force disconnect // Force disconnect

View File

@@ -55,7 +55,8 @@ public abstract class BluetoothCommunication {
private Queue<BluetoothGattDescriptor> descriptorRequestQueue; private Queue<BluetoothGattDescriptor> descriptorRequestQueue;
private Queue<BluetoothGattCharacteristic> characteristicRequestQueue; private Queue<BluetoothGattCharacteristic> characteristicRequestQueue;
private Boolean openRequest; private boolean openRequest;
private final Object lock = new Object();
public BluetoothCommunication(Context context) public BluetoothCommunication(Context context)
{ {
@@ -126,7 +127,7 @@ public abstract class BluetoothCommunication {
* @param stateNr the current step number * @param stateNr the current step number
* @return false if no next step is available otherwise true * @return false if no next step is available otherwise true
*/ */
abstract boolean nextInitCmd(int stateNr); abstract protected boolean nextInitCmd(int stateNr);
/** /**
* State machine for the normal/command process of the Bluetooth device. * State machine for the normal/command process of the Bluetooth device.
@@ -136,7 +137,7 @@ public abstract class BluetoothCommunication {
* @param stateNr the current step number * @param stateNr the current step number
* @return false if no next step is available otherwise true * @return false if no next step is available otherwise true
*/ */
abstract boolean nextBluetoothCmd(int stateNr); abstract protected boolean nextBluetoothCmd(int stateNr);
/** /**
* Set the next command number of the current state. * Set the next command number of the current state.
@@ -165,7 +166,7 @@ public abstract class BluetoothCommunication {
* @param stateNr the current step number * @param stateNr the current step number
* @return false if no next step is available otherwise true * @return false if no next step is available otherwise true
*/ */
abstract boolean nextCleanUpCmd(int stateNr); abstract protected boolean nextCleanUpCmd(int stateNr);
/** /**
* Method is triggered if a Bluetooth data is read from a device. * Method is triggered if a Bluetooth data is read from a device.
@@ -192,9 +193,10 @@ public abstract class BluetoothCommunication {
* @param btMachineState the machine state that should be set. * @param btMachineState the machine state that should be set.
*/ */
protected void setBtMachineState(BT_MACHINE_STATE btMachineState) { protected void setBtMachineState(BT_MACHINE_STATE btMachineState) {
this.btMachineState = btMachineState; synchronized (lock) {
this.btMachineState = btMachineState;
handleRequests(); handleRequests();
}
} }
/** /**
@@ -209,7 +211,7 @@ public abstract class BluetoothCommunication {
.getCharacteristic(characteristic); .getCharacteristic(characteristic);
gattCharacteristic.setValue(bytes); gattCharacteristic.setValue(bytes);
synchronized (openRequest) { synchronized (lock) {
characteristicRequestQueue.add(gattCharacteristic); characteristicRequestQueue.add(gattCharacteristic);
handleRequests(); handleRequests();
} }
@@ -244,7 +246,7 @@ public abstract class BluetoothCommunication {
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor); BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
synchronized (openRequest) { synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor); descriptorRequestQueue.add(gattDescriptor);
handleRequests(); handleRequests();
} }
@@ -264,7 +266,7 @@ public abstract class BluetoothCommunication {
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor); BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
synchronized (openRequest) { synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor); descriptorRequestQueue.add(gattDescriptor);
handleRequests(); handleRequests();
} }
@@ -284,7 +286,7 @@ public abstract class BluetoothCommunication {
BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor); BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor);
gattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); gattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
synchronized (openRequest) { synchronized (lock) {
descriptorRequestQueue.add(gattDescriptor); descriptorRequestQueue.add(gattDescriptor);
handleRequests(); handleRequests();
} }
@@ -332,6 +334,9 @@ public abstract class BluetoothCommunication {
public void connect(String hwAddress) { public void connect(String hwAddress) {
btAdapter.cancelDiscovery(); btAdapter.cancelDiscovery();
// Don't do any cleanup if disconnected before fully connected
btMachineState = BT_MACHINE_STATE.BT_CLEANUP_STATE;
BluetoothDevice device = btAdapter.getRemoteDevice(hwAddress); BluetoothDevice device = btAdapter.getRemoteDevice(hwAddress);
bluetoothGatt = device.connectGatt(context, false, gattCallback); bluetoothGatt = device.connectGatt(context, false, gattCallback);
} }
@@ -344,9 +349,13 @@ public abstract class BluetoothCommunication {
return; return;
} }
if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE && doCleanup) { if (doCleanup) {
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE); synchronized (lock) {
nextMachineStateStep(); if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) {
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
nextMachineStateStep();
}
}
} }
bluetoothGatt.disconnect(); bluetoothGatt.disconnect();
@@ -381,7 +390,7 @@ public abstract class BluetoothCommunication {
} }
private void handleRequests() { private void handleRequests() {
synchronized (openRequest) { synchronized (lock) {
// check for pending request // check for pending request
if (openRequest) { if (openRequest) {
return; // yes, do nothing return; // yes, do nothing
@@ -433,14 +442,16 @@ public abstract class BluetoothCommunication {
@Override @Override
public void onServicesDiscovered(final BluetoothGatt gatt, int status) { public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
cmdStepNr = 0; synchronized (lock) {
initStepNr = 0; cmdStepNr = 0;
cleanupStepNr = 0; initStepNr = 0;
cleanupStepNr = 0;
// Clear from possible previous setups // Clear from possible previous setups
characteristicRequestQueue = new LinkedList<>(); characteristicRequestQueue = new LinkedList<>();
descriptorRequestQueue = new LinkedList<>(); descriptorRequestQueue = new LinkedList<>();
openRequest = false; openRequest = false;
}
try { try {
// Sleeping a while after discovering services fixes connection problems. // Sleeping a while after discovering services fixes connection problems.
@@ -460,7 +471,7 @@ public abstract class BluetoothCommunication {
public void onDescriptorWrite(BluetoothGatt gatt, public void onDescriptorWrite(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor, BluetoothGattDescriptor descriptor,
int status) { int status) {
synchronized (openRequest) { synchronized (lock) {
openRequest = false; openRequest = false;
handleRequests(); handleRequests();
} }
@@ -470,7 +481,7 @@ public abstract class BluetoothCommunication {
public void onCharacteristicWrite(BluetoothGatt gatt, public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, BluetoothGattCharacteristic characteristic,
int status) { int status) {
synchronized (openRequest) { synchronized (lock) {
openRequest = false; openRequest = false;
handleRequests(); handleRequests();
} }
@@ -480,8 +491,8 @@ public abstract class BluetoothCommunication {
public void onCharacteristicRead(BluetoothGatt gatt, public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, BluetoothGattCharacteristic characteristic,
int status) { int status) {
onBluetoothDataRead(gatt, characteristic, status); synchronized (lock) {
synchronized (openRequest) { onBluetoothDataRead(gatt, characteristic, status);
openRequest = false; openRequest = false;
handleRequests(); handleRequests();
} }
@@ -490,7 +501,9 @@ public abstract class BluetoothCommunication {
@Override @Override
public void onCharacteristicChanged(BluetoothGatt gatt, public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) { BluetoothGattCharacteristic characteristic) {
onBluetoothDataChange(gatt, characteristic); synchronized (lock) {
onBluetoothDataChange(gatt, characteristic);
}
} }
} }
} }

View File

@@ -47,17 +47,17 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }

View File

@@ -56,7 +56,7 @@ public class BluetoothDigooDGSO38H extends BluetoothCommunication {
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
//Tell device to send us weight measurements //Tell device to send us weight measurements
@@ -68,7 +68,7 @@ public class BluetoothDigooDGSO38H extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
default: default:
return false; return false;
@@ -76,7 +76,7 @@ public class BluetoothDigooDGSO38H extends BluetoothCommunication {
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
default: default:

View File

@@ -46,12 +46,12 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser(); final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
@@ -87,7 +87,7 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication {
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }

View File

@@ -43,7 +43,7 @@ public class BluetoothExingtechY1 extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, WEIGHT_MEASUREMENT_CONFIG); setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, WEIGHT_MEASUREMENT_CONFIG);
@@ -69,12 +69,12 @@ public class BluetoothExingtechY1 extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }

View File

@@ -41,7 +41,7 @@ public class BluetoothHesley extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, WEIGHT_MEASUREMENT_CONFIG); setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, WEIGHT_MEASUREMENT_CONFIG);
@@ -58,12 +58,12 @@ public class BluetoothHesley extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }

View File

@@ -54,19 +54,19 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
Log.w("openscale","ihealthHS3 - nextInitCmd - returning false"); Log.w("openscale","ihealthHS3 - nextInitCmd - returning false");
return false; return false;
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
Log.w("openscale","ihealthHS3 - nextBluetoothCmd - returning false"); Log.w("openscale","ihealthHS3 - nextBluetoothCmd - returning false");
return false; return false;
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
Log.w("openscale","ihealthHS3 - nextCleanUpCmd - returning false"); Log.w("openscale","ihealthHS3 - nextCleanUpCmd - returning false");
return false; return false;
} }

View File

@@ -84,7 +84,7 @@ public class BluetoothMGB extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
setNotificationOn(uuid_service, uuid_char_ctrl, uuid_desc_ctrl); setNotificationOn(uuid_service, uuid_char_ctrl, uuid_desc_ctrl);
@@ -125,13 +125,13 @@ public class BluetoothMGB extends BluetoothCommunication {
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }

View File

@@ -46,12 +46,12 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
// set indication on for feature characteristic // set indication on for feature characteristic
@@ -89,7 +89,7 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication {
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }

View File

@@ -98,7 +98,7 @@ public class BluetoothMiScale extends BluetoothCommunication {
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
// read device time // read device time
@@ -136,7 +136,7 @@ public class BluetoothMiScale extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
// set notification on for weight measurement history // set notification on for weight measurement history
@@ -173,7 +173,7 @@ public class BluetoothMiScale extends BluetoothCommunication {
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:

View File

@@ -85,7 +85,7 @@ public class BluetoothMiScale2 extends BluetoothCommunication {
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
// set scale units // set scale units
@@ -119,7 +119,7 @@ public class BluetoothMiScale2 extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
// configure scale to get only last measurements // configure scale to get only last measurements
@@ -148,7 +148,7 @@ public class BluetoothMiScale2 extends BluetoothCommunication {
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:

View File

@@ -45,7 +45,7 @@ public class BluetoothOneByone extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CONFIG); setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CONFIG);
@@ -69,12 +69,12 @@ public class BluetoothOneByone extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }

View File

@@ -52,7 +52,7 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication {
} }
@Override @Override
boolean nextInitCmd(int stateNr) { protected boolean nextInitCmd(int stateNr) {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
byte[] userId = Converters.toUnsignedInt16Be(getUniqueNumber()); byte[] userId = Converters.toUnsignedInt16Be(getUniqueNumber());
@@ -94,12 +94,12 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication {
} }
@Override @Override
boolean nextBluetoothCmd(int stateNr) { protected boolean nextBluetoothCmd(int stateNr) {
return false; return false;
} }
@Override @Override
boolean nextCleanUpCmd(int stateNr) { protected boolean nextCleanUpCmd(int stateNr) {
return false; return false;
} }