From 84d1e957677a8c18ec395a2e8419d00b86119bae Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 10 May 2015 23:14:32 +0200 Subject: [PATCH] Fix removal of async runnable to stop pairing after 60s (#3) --- .../gadgetbridge/discovery/DiscoveryActivity.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/discovery/DiscoveryActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/discovery/DiscoveryActivity.java index 045e0794c..b1f260512 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/discovery/DiscoveryActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/discovery/DiscoveryActivity.java @@ -10,6 +10,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; +import android.os.Message; import android.os.Parcelable; import android.util.Log; import android.view.View; @@ -287,17 +288,23 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli private void startBTLEDiscovery() { Log.i(TAG, "Starting BTLE Discovery"); handler.removeMessages(0, stopRunnable); - handler.postDelayed(stopRunnable, SCAN_DURATION); + handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION); adapter.startLeScan(leScanCallback); } private void startBTDiscovery() { Log.i(TAG, "Starting BT Discovery"); handler.removeMessages(0, stopRunnable); - handler.postDelayed(stopRunnable, SCAN_DURATION); + handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION); adapter.startDiscovery(); } + private Message getPostMessage(Runnable runnable) { + Message m = Message.obtain(handler, runnable); + m.obj = runnable; + return m; + } + @Override public void onItemClick(AdapterView parent, View view, int position, long id) { DeviceCandidate deviceCandidate = deviceCandidates.get(position); @@ -322,7 +329,7 @@ public class DiscoveryActivity extends Activity implements AdapterView.OnItemCli bondingAddress = btDevice.getAddress(); } } catch (Exception e) { - e.printStackTrace(); + Log.e(TAG, "Error pairing device: " + deviceCandidate.getMacAddress()); } } }