From aa0d0c049727f7b3e3f619de00110eeb8cfc31fe Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Sun, 15 Apr 2018 21:14:10 +0200 Subject: [PATCH] Fix crash while granting BT permission Revert b3508ac1 and fix it proper. Fixes #244. --- .../gui/preferences/BluetoothPreferences.java | 81 ++++++++++--------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java index 6d40223cf..23840ec82 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java @@ -54,6 +54,7 @@ public class BluetoothPreferences extends PreferenceFragment { private PreferenceScreen btScanner; private BluetoothAdapter btAdapter = null; + private BluetoothAdapter.LeScanCallback leScanCallback = null; private Handler handler = null; private Map foundDevices = new HashMap<>(); @@ -86,23 +87,30 @@ public void run() { } }, progressUpdatePeriod); - // Close any existing connection during the scan - OpenScale.getInstance(getActivity()).disconnectFromBluetoothDevice(); - + // Abort discovery and scan if back is pressed or a scale is selected btScanner.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { - handler.removeCallbacksAndMessages(null); - btAdapter.cancelDiscovery(); + stopDiscoveryAndLeScan(); } }); + // Close any existing connection during the scan + OpenScale.getInstance(getActivity()).disconnectFromBluetoothDevice(); + + // Intent filter for the scanning process + IntentFilter filter = new IntentFilter(); + filter.addAction(BluetoothDevice.ACTION_FOUND); + filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); + filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + getActivity().registerReceiver(mReceiver, filter); + // Do classic bluetooth discovery first and BLE scan afterwards btAdapter.startDiscovery(); } private void startBleScan() { - final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { + leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { onDeviceFound(device); @@ -113,9 +121,7 @@ public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { handler.postDelayed(new Runnable() { @Override public void run() { - handler.removeCallbacksAndMessages(null); - btAdapter.stopLeScan(leScanCallback); - btScanner.getDialog().setOnDismissListener(null); + stopDiscoveryAndLeScan(); Preference scanning = btScanner.getPreference(0); scanning.setTitle(R.string.label_bluetooth_searching_finished); @@ -133,18 +139,28 @@ public void run() { } }, 10 * 1000); - // Also cancel scan if dialog is dismissed - btScanner.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - handler.removeCallbacksAndMessages(null); - btAdapter.stopLeScan(leScanCallback); - } - }); - btAdapter.startLeScan(leScanCallback); } + private void stopDiscoveryAndLeScan() { + if (handler != null) { + handler.removeCallbacksAndMessages(null); + handler = null; + + getActivity().unregisterReceiver(mReceiver); + btAdapter.cancelDiscovery(); + } + + if (leScanCallback != null) { + btAdapter.stopLeScan(leScanCallback); + leScanCallback = null; + } + + if (btScanner.getDialog() != null) { + btScanner.getDialog().setOnDismissListener(null); + } + } + private void onDeviceFound(BluetoothDevice device) { if (device.getName() == null || foundDevices.containsKey(device.getAddress())) { return; @@ -175,11 +191,6 @@ private void onDeviceFound(BluetoothDevice device) { private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(final Context context, Intent intent) { - // May be called before the dialog is shown or while it is being dismissed - if (btScanner.getDialog() == null || !btScanner.getDialog().isShowing()) { - return; - } - String action = intent.getAction(); if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { @@ -236,25 +247,22 @@ public boolean onPreferenceClick(Preference preference) { } @Override - public void onResume() { - super.onResume(); + public void onStart() { + super.onStart(); - // Intent filter for the scanning process - IntentFilter filter = new IntentFilter(); - filter.addAction(BluetoothDevice.ACTION_FOUND); - filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); - filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); - getActivity().registerReceiver(mReceiver, filter); + // Restart discovery after e.g. orientation change + if (btScanner.getDialog() != null) { + startBluetoothDiscovery(); + } } @Override - public void onPause() { - getActivity().unregisterReceiver(mReceiver); + public void onStop() { if (btScanner.getDialog() != null) { - btScanner.getDialog().dismiss(); + stopDiscoveryAndLeScan(); } - super.onPause(); + super.onStop(); } @Override @@ -300,9 +308,10 @@ public void onMyOwnRequestPermissionsResult(int requestCode, String permissions[ switch (requestCode) { case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - //startBluetoothDiscovery(); + startBluetoothDiscovery(); } else { Toast.makeText(getActivity(), R.string.permission_not_granted, Toast.LENGTH_SHORT).show(); + btScanner.getDialog().dismiss(); } break; }