1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 07:21:40 +02:00

Try to discover medisana scale before connecting

In an attempt to see if this fixes the problems reported in #278.
This commit is contained in:
Erik Johansson
2018-05-31 21:46:24 +02:00
parent 71f7cae682
commit 2f6b6df3a4
2 changed files with 29 additions and 5 deletions

View File

@@ -96,6 +96,10 @@ public abstract class BluetoothCommunication {
return bluetoothGatt.getServices();
}
protected boolean discoverDeviceBeforeConnecting() {
return false;
}
/**
* Register a callback Bluetooth handler that notify any BT_STATUS_CODE changes for GUI/CORE.
*
@@ -380,7 +384,7 @@ public abstract class BluetoothCommunication {
*
* @param hwAddress the Bluetooth address to connect to
*/
public void connect(String hwAddress) {
public void connect(final String hwAddress) {
Timber.i("Connecting to [%s] (driver: %s)", hwAddress, driverName());
Timber.d("BT is%s enabled, state=%d, scan mode=%d, is%s discovering",
@@ -401,6 +405,8 @@ public abstract class BluetoothCommunication {
// Some good tips to improve BLE connections:
// https://android.jlelse.eu/lessons-for-first-time-android-bluetooth-le-developers-i-learned-the-hard-way-fee07646624
final boolean doDiscoveryFirst = discoverDeviceBeforeConnecting();
// Running an LE scan during connect improves connectivity on some phones
// (e.g. Sony Xperia Z5 compact, Android 7.1.1).
btAdapter.cancelDiscovery();
@@ -411,6 +417,13 @@ public abstract class BluetoothCommunication {
leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Timber.d("Found LE device %s [%s]", device.getName(), device.getAddress());
if (!doDiscoveryFirst || !device.getAddress().equals(hwAddress)) {
return;
}
synchronized (lock) {
connectGatt(device);
}
}
};
btAdapter.startLeScan(leScanCallback);
@@ -423,7 +436,12 @@ public abstract class BluetoothCommunication {
// Don't do any cleanup if disconnected before fully connected
btMachineState = BT_MACHINE_STATE.BT_CLEANUP_STATE;
BluetoothDevice device = btAdapter.getRemoteDevice(hwAddress);
if (!doDiscoveryFirst || leScanCallback == null) {
connectGatt(btAdapter.getRemoteDevice(hwAddress));
}
}
private void connectGatt(BluetoothDevice device) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
bluetoothGatt = device.connectGatt(
context, false, gattCallback, BluetoothDevice.TRANSPORT_LE);
@@ -438,14 +456,15 @@ public abstract class BluetoothCommunication {
*/
public void disconnect(boolean doCleanup) {
synchronized (lock) {
if (bluetoothGatt == null) {
return;
}
if (leScanCallback != null) {
btAdapter.stopLeScan(leScanCallback);
leScanCallback = null;
}
if (bluetoothGatt == null) {
return;
}
Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : "");
if (doCleanup) {

View File

@@ -44,6 +44,11 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication {
btScaleMeasurement = new ScaleMeasurement();
}
@Override
protected boolean discoverDeviceBeforeConnecting() {
return true;
}
@Override
public String driverName() {
return "Medisana BS44x";