From 161731547222e1251f77face92501612d688e27d Mon Sep 17 00:00:00 2001 From: oliexdev Date: Sun, 10 Feb 2019 21:31:34 +0100 Subject: [PATCH] adding a delay of 500 ms between the end of the BLE scan and the beginning of a connection --- .../bluetooth/BluetoothCommunication.java | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java index 14ad49b1..3af6adcc 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java @@ -517,28 +517,35 @@ public abstract class BluetoothCommunication { Timber.d("Stop Le san"); scanSubscription.dispose(); scanSubscription = null; + } - Timber.d("Try to connect to BLE device " + macAddress); + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + Timber.d("Try to connect to BLE device " + macAddress); - connectionObservable = bleDevice - .establishConnection(false) - .delay(BT_DELAY, TimeUnit.MILLISECONDS) - .takeUntil(disconnectTriggerSubject) - .doOnError(throwable -> setBtStatus(BT_STATUS_CODE.BT_CONNECTION_RETRYING)) - .observeOn(AndroidSchedulers.mainThread()) - .compose(ReplayingShare.instance()); + connectionObservable = bleDevice + .establishConnection(false) + .delay(BT_DELAY, TimeUnit.MILLISECONDS) + .takeUntil(disconnectTriggerSubject) + .doOnError(throwable -> setBtStatus(BT_STATUS_CODE.BT_CONNECTION_RETRYING)) + .observeOn(AndroidSchedulers.mainThread()) + .compose(ReplayingShare.instance()); - if (isConnected()) { - disconnect(); - } else { - initStepNr = -1; - cmdStepNr = -1; - cleanupStepNr = -1; + if (isConnected()) { + disconnect(); + } else { + initStepNr = -1; + cmdStepNr = -1; + cleanupStepNr = -1; - setBtMonitoringOn(); - setBtMachineState(BT_MACHINE_STATE.BT_INIT_STATE); - } + setBtMonitoringOn(); + setBtMachineState(BT_MACHINE_STATE.BT_INIT_STATE); + } + } + }, 500); } private void setBtMonitoringOn() {