From bd021b75defad922ed06c439d8673c7d928ab6d8 Mon Sep 17 00:00:00 2001 From: oliexdev Date: Sat, 2 Feb 2019 08:40:12 +0100 Subject: [PATCH] delay Bluetooth connection to 50ms and set Bluetooth debug level on --- .../main/java/com/health/openscale/core/OpenScale.java | 1 + .../openscale/core/bluetooth/BluetoothCommunication.java | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java index 49281856..52fb9d31 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java @@ -103,6 +103,7 @@ public class OpenScale { fragmentList = new ArrayList<>(); bleClient = RxBleClient.create(context); + RxBleClient.setLogLevel(RxBleLog.DEBUG); RxBleLog.setLogger((level, tag, msg) -> Timber.tag(tag).log(level, msg)); reopenDatabase(false); 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 94f9c7a7..fdc52d59 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 @@ -34,6 +34,7 @@ import java.net.SocketException; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.concurrent.TimeUnit; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; @@ -65,6 +66,7 @@ public abstract class BluetoothCommunication { } private final int BT_RETRY_TIMES_ON_ERROR = 3; + private final int BT_DELAY = 50; // MS protected Context context; @@ -285,6 +287,7 @@ public abstract class BluetoothCommunication { protected void writeBytes(UUID characteristic, byte[] bytes) { if (isConnected()) { final Disposable disposable = connectionObservable + .delay(BT_DELAY, TimeUnit.MILLISECONDS) .flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristic, bytes)) .observeOn(AndroidSchedulers.mainThread()) .retry(BT_RETRY_TIMES_ON_ERROR) @@ -311,6 +314,7 @@ public abstract class BluetoothCommunication { protected void readBytes(UUID characteristic) { if (isConnected()) { final Disposable disposable = connectionObservable + .delay(BT_DELAY, TimeUnit.MILLISECONDS) .firstOrError() .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(characteristic)) .observeOn(AndroidSchedulers.mainThread()) @@ -334,6 +338,7 @@ public abstract class BluetoothCommunication { protected void setIndicationOn(UUID characteristic) { if (isConnected()) { final Disposable disposable = connectionObservable + .delay(BT_DELAY, TimeUnit.MILLISECONDS) .flatMap(rxBleConnection -> rxBleConnection.setupIndication(characteristic)) .doOnNext(notificationObservable -> { Timber.d("Successful set indication on for %s", BluetoothGattUuid.prettyPrint(characteristic)); @@ -366,6 +371,7 @@ public abstract class BluetoothCommunication { protected void setNotificationOn(UUID characteristic) { if (isConnected()) { final Disposable disposable = connectionObservable + .delay(BT_DELAY, TimeUnit.MILLISECONDS) .flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristic)) .doOnNext(notificationObservable -> { Timber.d("Successful set notification on for %s", BluetoothGattUuid.prettyPrint(characteristic)); @@ -466,6 +472,7 @@ public abstract class BluetoothCommunication { connectionObservable = bleDevice .establishConnection(false) + .delay(BT_DELAY, TimeUnit.MILLISECONDS) .takeUntil(disconnectTriggerSubject) .doOnError(throwable -> setBtStatus(BT_STATUS_CODE.BT_CONNECTION_RETRYING)) .observeOn(AndroidSchedulers.mainThread()) @@ -475,6 +482,7 @@ public abstract class BluetoothCommunication { disconnect(); } else { final Disposable connectionDisposable = connectionObservable + .delay(BT_DELAY, TimeUnit.MILLISECONDS) .flatMapSingle(RxBleConnection::discoverServices) .observeOn(AndroidSchedulers.mainThread()) .retry(BT_RETRY_TIMES_ON_ERROR)