mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 16:02:04 +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:
@@ -96,6 +96,10 @@ public abstract class BluetoothCommunication {
|
|||||||
return bluetoothGatt.getServices();
|
return bluetoothGatt.getServices();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean discoverDeviceBeforeConnecting() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a callback Bluetooth handler that notify any BT_STATUS_CODE changes for GUI/CORE.
|
* 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
|
* @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.i("Connecting to [%s] (driver: %s)", hwAddress, driverName());
|
||||||
|
|
||||||
Timber.d("BT is%s enabled, state=%d, scan mode=%d, is%s discovering",
|
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:
|
// 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
|
// 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
|
// Running an LE scan during connect improves connectivity on some phones
|
||||||
// (e.g. Sony Xperia Z5 compact, Android 7.1.1).
|
// (e.g. Sony Xperia Z5 compact, Android 7.1.1).
|
||||||
btAdapter.cancelDiscovery();
|
btAdapter.cancelDiscovery();
|
||||||
@@ -411,6 +417,13 @@ public abstract class BluetoothCommunication {
|
|||||||
leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
|
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);
|
btAdapter.startLeScan(leScanCallback);
|
||||||
@@ -423,7 +436,12 @@ public abstract class BluetoothCommunication {
|
|||||||
// Don't do any cleanup if disconnected before fully connected
|
// Don't do any cleanup if disconnected before fully connected
|
||||||
btMachineState = BT_MACHINE_STATE.BT_CLEANUP_STATE;
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
bluetoothGatt = device.connectGatt(
|
bluetoothGatt = device.connectGatt(
|
||||||
context, false, gattCallback, BluetoothDevice.TRANSPORT_LE);
|
context, false, gattCallback, BluetoothDevice.TRANSPORT_LE);
|
||||||
@@ -438,14 +456,15 @@ public abstract class BluetoothCommunication {
|
|||||||
*/
|
*/
|
||||||
public void disconnect(boolean doCleanup) {
|
public void disconnect(boolean doCleanup) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (bluetoothGatt == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (leScanCallback != null) {
|
if (leScanCallback != null) {
|
||||||
btAdapter.stopLeScan(leScanCallback);
|
btAdapter.stopLeScan(leScanCallback);
|
||||||
leScanCallback = null;
|
leScanCallback = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bluetoothGatt == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : "");
|
Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : "");
|
||||||
|
|
||||||
if (doCleanup) {
|
if (doCleanup) {
|
||||||
|
@@ -44,6 +44,11 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication {
|
|||||||
btScaleMeasurement = new ScaleMeasurement();
|
btScaleMeasurement = new ScaleMeasurement();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean discoverDeviceBeforeConnecting() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String driverName() {
|
public String driverName() {
|
||||||
return "Medisana BS44x";
|
return "Medisana BS44x";
|
||||||
|
Reference in New Issue
Block a user