mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 07:51:46 +02:00
Fix crash while granting BT permission
Revert b3508ac1
and fix it proper. Fixes #244.
This commit is contained in:
@@ -54,6 +54,7 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
|
|
||||||
private PreferenceScreen btScanner;
|
private PreferenceScreen btScanner;
|
||||||
private BluetoothAdapter btAdapter = null;
|
private BluetoothAdapter btAdapter = null;
|
||||||
|
private BluetoothAdapter.LeScanCallback leScanCallback = null;
|
||||||
private Handler handler = null;
|
private Handler handler = null;
|
||||||
private Map<String, BluetoothDevice> foundDevices = new HashMap<>();
|
private Map<String, BluetoothDevice> foundDevices = new HashMap<>();
|
||||||
|
|
||||||
@@ -86,23 +87,30 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
}
|
}
|
||||||
}, progressUpdatePeriod);
|
}, progressUpdatePeriod);
|
||||||
|
|
||||||
// Close any existing connection during the scan
|
// Abort discovery and scan if back is pressed or a scale is selected
|
||||||
OpenScale.getInstance(getActivity()).disconnectFromBluetoothDevice();
|
|
||||||
|
|
||||||
btScanner.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
|
btScanner.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onDismiss(DialogInterface dialog) {
|
public void onDismiss(DialogInterface dialog) {
|
||||||
handler.removeCallbacksAndMessages(null);
|
stopDiscoveryAndLeScan();
|
||||||
btAdapter.cancelDiscovery();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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
|
// Do classic bluetooth discovery first and BLE scan afterwards
|
||||||
btAdapter.startDiscovery();
|
btAdapter.startDiscovery();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startBleScan() {
|
private void startBleScan() {
|
||||||
final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
|
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
|
||||||
onDeviceFound(device);
|
onDeviceFound(device);
|
||||||
@@ -113,9 +121,7 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
handler.postDelayed(new Runnable() {
|
handler.postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
handler.removeCallbacksAndMessages(null);
|
stopDiscoveryAndLeScan();
|
||||||
btAdapter.stopLeScan(leScanCallback);
|
|
||||||
btScanner.getDialog().setOnDismissListener(null);
|
|
||||||
|
|
||||||
Preference scanning = btScanner.getPreference(0);
|
Preference scanning = btScanner.getPreference(0);
|
||||||
scanning.setTitle(R.string.label_bluetooth_searching_finished);
|
scanning.setTitle(R.string.label_bluetooth_searching_finished);
|
||||||
@@ -133,18 +139,28 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
}
|
}
|
||||||
}, 10 * 1000);
|
}, 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);
|
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) {
|
private void onDeviceFound(BluetoothDevice device) {
|
||||||
if (device.getName() == null || foundDevices.containsKey(device.getAddress())) {
|
if (device.getName() == null || foundDevices.containsKey(device.getAddress())) {
|
||||||
return;
|
return;
|
||||||
@@ -175,11 +191,6 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
public void onReceive(final Context context, Intent intent) {
|
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();
|
String action = intent.getAction();
|
||||||
|
|
||||||
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
|
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
|
||||||
@@ -236,25 +247,22 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onStart() {
|
||||||
super.onResume();
|
super.onStart();
|
||||||
|
|
||||||
// Intent filter for the scanning process
|
// Restart discovery after e.g. orientation change
|
||||||
IntentFilter filter = new IntentFilter();
|
if (btScanner.getDialog() != null) {
|
||||||
filter.addAction(BluetoothDevice.ACTION_FOUND);
|
startBluetoothDiscovery();
|
||||||
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
|
}
|
||||||
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
|
||||||
getActivity().registerReceiver(mReceiver, filter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onStop() {
|
||||||
getActivity().unregisterReceiver(mReceiver);
|
|
||||||
if (btScanner.getDialog() != null) {
|
if (btScanner.getDialog() != null) {
|
||||||
btScanner.getDialog().dismiss();
|
stopDiscoveryAndLeScan();
|
||||||
}
|
}
|
||||||
|
|
||||||
super.onPause();
|
super.onStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -300,9 +308,10 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION: {
|
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION: {
|
||||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
//startBluetoothDiscovery();
|
startBluetoothDiscovery();
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(getActivity(), R.string.permission_not_granted, Toast.LENGTH_SHORT).show();
|
Toast.makeText(getActivity(), R.string.permission_not_granted, Toast.LENGTH_SHORT).show();
|
||||||
|
btScanner.getDialog().dismiss();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user