Added traps for security exceptions in BLEScanActivity - Fixes #100

This commit is contained in:
Graham Jones
2023-06-17 20:48:35 +01:00
parent ef95065a3a
commit 49cb28d431
2 changed files with 38 additions and 12 deletions

View File

@@ -244,10 +244,9 @@ public class BLEScanActivity extends ListActivity {
protected void onListItemClick(ListView l, View v, int position, long id) { protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return; if (device == null) return;
Log.v(TAG, "onListItemClick: Device=" + device.getName() + ", Addr=" + device.getAddress()); Log.v(TAG, "onListItemClick: Device Addr=" + device.getAddress());
if (mScanning) { if (mScanning) {
mBluetoothLeScanner.stopScan(mLeScanCallback); stopScan();
mScanning = false;
} }
Log.v(TAG, "Saving Device Details"); Log.v(TAG, "Saving Device Details");
SharedPreferences.Editor SPE = PreferenceManager SharedPreferences.Editor SPE = PreferenceManager
@@ -259,7 +258,7 @@ public class BLEScanActivity extends ListActivity {
SPE.commit(); SPE.commit();
Log.v(TAG, "Saved Device Name=" + device.getName() + " and Address=" + device.getAddress()); Log.v(TAG, "Saved Device Name=" + device.getName() + " and Address=" + device.getAddress());
} catch (Exception ex) { } catch (SecurityException ex) {
Log.e(TAG, "Error Saving Device Name and Address!"); Log.e(TAG, "Error Saving Device Name and Address!");
Toast toast = Toast.makeText(this, "Problem Saving Device Name and Address", Toast.LENGTH_SHORT); Toast toast = Toast.makeText(this, "Problem Saving Device Name and Address", Toast.LENGTH_SHORT);
toast.show(); toast.show();
@@ -288,6 +287,28 @@ public class BLEScanActivity extends ListActivity {
} }
} }
private void startScan() {
mScanning = true;
try {
mBluetoothLeScanner.startScan(mLeScanCallback);
} catch (SecurityException e) {
Log.e(TAG,"startScan - SecurityException while starting scan");
Toast toast = Toast.makeText(this, "ERROR Starting Scan - Security Exception", Toast.LENGTH_SHORT);
toast.show();
}
}
private void stopScan() {
mScanning = false;
try {
mBluetoothLeScanner.stopScan(mLeScanCallback);
} catch (SecurityException e) {
Log.e(TAG,"stopScan - SecurityException while stopping scan");
Toast toast = Toast.makeText(this, "ERROR Stopping Scan - Security Exception", Toast.LENGTH_SHORT);
toast.show();
}
}
private void scanLeDevice(final boolean enable) { private void scanLeDevice(final boolean enable) {
TextView tv; TextView tv;
@@ -297,8 +318,7 @@ public class BLEScanActivity extends ListActivity {
mHandler.postDelayed(new Runnable() { mHandler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
mScanning = false; stopScan();
mBluetoothLeScanner.stopScan(mLeScanCallback);
invalidateOptionsMenu(); invalidateOptionsMenu();
TextView tv = (TextView) (findViewById(R.id.ble_scan_status_tv)); TextView tv = (TextView) (findViewById(R.id.ble_scan_status_tv));
tv.setText("Stopped"); tv.setText("Stopped");
@@ -308,16 +328,14 @@ public class BLEScanActivity extends ListActivity {
} }
}, SCAN_PERIOD); }, SCAN_PERIOD);
mScanning = true; startScan();
mBluetoothLeScanner.startScan(mLeScanCallback);
tv = (TextView) (findViewById(R.id.ble_scan_status_tv)); tv = (TextView) (findViewById(R.id.ble_scan_status_tv));
tv.setText("Scanning"); tv.setText("Scanning");
Button b = (Button) findViewById(R.id.startScanButton); Button b = (Button) findViewById(R.id.startScanButton);
b.setEnabled(false); b.setEnabled(false);
} else { } else {
mScanning = false; stopScan();
mBluetoothLeScanner.stopScan(mLeScanCallback);
tv = (TextView) (findViewById(R.id.ble_scan_status_tv)); tv = (TextView) (findViewById(R.id.ble_scan_status_tv));
tv.setText("Stopped"); tv.setText("Stopped");
Button b = (Button) findViewById(R.id.startScanButton); Button b = (Button) findViewById(R.id.startScanButton);
@@ -339,7 +357,11 @@ public class BLEScanActivity extends ListActivity {
public void addDevice(BluetoothDevice device) { public void addDevice(BluetoothDevice device) {
if (!mLeDevices.contains(device)) { if (!mLeDevices.contains(device)) {
Log.v(TAG, "addDevice - " + device.getName()); try {
Log.v(TAG, "addDevice - " + device.getName());
} catch (SecurityException e) {
Log.e(TAG,"addDevice() - security exception getting device name");
}
mLeDevices.add(device); mLeDevices.add(device);
} }
} }
@@ -400,7 +422,11 @@ public class BLEScanActivity extends ListActivity {
@Override @Override
public void onScanResult(int callbackType, ScanResult result) { public void onScanResult(int callbackType, ScanResult result) {
//super.onScanResult(callbackType, result); //super.onScanResult(callbackType, result);
Log.v(TAG, "ScanCallback - " + result.getDevice().getName()); try {
Log.v(TAG, "ScanCallback - " + result.getDevice().getName());
} catch (SecurityException e) {
Log.e(TAG,"ScanCallback - security exception getting device name");
}
mLeDeviceListAdapter.addDevice(result.getDevice()); mLeDeviceListAdapter.addDevice(result.getDevice());
mLeDeviceListAdapter.notifyDataSetChanged(); mLeDeviceListAdapter.notifyDataSetChanged();
} }