From f2197c9ca9c6684a6df1d55270c362dcc2248586 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Sun, 7 Oct 2018 09:24:37 +0200 Subject: [PATCH] Do gatt connect on main thread --- .../core/bluetooth/BluetoothCommunication.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 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 a5360d46..8abe809a 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 @@ -476,15 +476,20 @@ public abstract class BluetoothCommunication { private void startLeScanForDevice(final String hwAddress) { leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override - public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { + public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { Timber.d("Found LE device %s [%s]", device.getName(), device.getAddress()); if (!device.getAddress().equals(hwAddress)) { return; } - synchronized (lock) { - stopLeScan(); - connectGatt(device); - } + handler.post(new Runnable() { + @Override + public void run() { + if (leScanCallback != null) { + stopLeScan(); + connectGatt(device); + } + } + }); } };