Skip to content

Commit

Permalink
Don't start or stop scans if bluetooth state is off to prevent crashe…
Browse files Browse the repository at this point in the history
…s on HTC devis
  • Loading branch information
davidgyoung committed Nov 16, 2016
1 parent e855723 commit 2df2286
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public void run() {

@Override
protected void startScan() {
if (!isBluetoothOn()) {
LogManager.d(TAG, "Not starting scan because bluetooth is off");
return;
}
List<ScanFilter> filters = new ArrayList<ScanFilter>();
ScanSettings settings;

Expand Down Expand Up @@ -205,6 +209,10 @@ public void run() {
}

private void postStopLeScan() {
if (!isBluetoothOn()){
LogManager.d(TAG, "Not stopping scan because bluetooth is off");
return;
}
final BluetoothLeScanner scanner = getScanner();
if (scanner == null) {
return;
Expand All @@ -230,6 +238,20 @@ public void run() {
});
}

private boolean isBluetoothOn() {
try {
BluetoothAdapter bluetoothAdapter = getBluetoothAdapter();
if (bluetoothAdapter != null) {
return (bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
}
LogManager.w(TAG, "Cannot get bluetooth adapter");
}
catch (SecurityException e) {
LogManager.w(TAG, "SecurityException checking if bluetooth is on");
}
return false;
}

private BluetoothLeScanner getScanner() {
try {
if (mScanner == null) {
Expand Down

0 comments on commit 2df2286

Please sign in to comment.