From a4a27d2f7ea32b82739010e68b501b52ec74b7b5 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Thu, 14 Jun 2018 21:56:52 +0200 Subject: [PATCH] Stop LE scan before connecting to discovered device See #278 --- .../bluetooth/BluetoothCommunication.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 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 5a9495fc..4e29ec6f 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 @@ -409,6 +409,7 @@ public abstract class BluetoothCommunication { return; } synchronized (lock) { + stopLeScan(); connectGatt(device); } } @@ -455,15 +456,20 @@ public abstract class BluetoothCommunication { } } + private void stopLeScan() { + if (leScanCallback != null) { + Timber.d("Stopping LE scan"); + btAdapter.stopLeScan(leScanCallback); + leScanCallback = null; + } + } + /** * Disconnect from a Bluetooth device */ public void disconnect(boolean doCleanup) { synchronized (lock) { - if (leScanCallback != null) { - btAdapter.stopLeScan(leScanCallback); - leScanCallback = null; - } + stopLeScan(); if (bluetoothGatt == null) { return; @@ -563,10 +569,7 @@ public abstract class BluetoothCommunication { if (newState == BluetoothProfile.STATE_CONNECTED) { synchronized (lock) { - if (leScanCallback != null) { - btAdapter.stopLeScan(leScanCallback); - leScanCallback = null; - } + stopLeScan(); } connectionEstablished = true;