Skip to content

Commit 4c6978c

Browse files
committed
NPE when getBluetoothLeScanner() is called when Adapter is turned off, wrapped in IllegalStateException. Fixes #50
1 parent a23d0d9 commit 4c6978c

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

android/src/main/java/com/pauldemarco/flutterblue/FlutterBluePlugin.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.bluetooth.BluetoothGattService;
1616
import android.bluetooth.BluetoothManager;
1717
import android.bluetooth.BluetoothProfile;
18+
import android.bluetooth.le.BluetoothLeScanner;
1819
import android.bluetooth.le.ScanCallback;
1920
import android.bluetooth.le.ScanFilter;
2021
import android.bluetooth.le.ScanResult;
@@ -591,16 +592,15 @@ private void startScan(MethodCall call, Result result) {
591592
Protos.ScanSettings settings;
592593
try {
593594
settings = Protos.ScanSettings.newBuilder().mergeFrom(data).build();
594-
} catch (InvalidProtocolBufferException e) {
595-
result.error("RuntimeException", e.getMessage(), e);
596-
return;
597-
}
598-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
599-
startScan21(settings);
600-
} else {
601-
startScan18(settings);
595+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
596+
startScan21(settings);
597+
} else {
598+
startScan18(settings);
599+
}
600+
result.success(null);
601+
} catch (Exception e) {
602+
result.error("startScan", e.getMessage(), e);
602603
}
603-
result.success(null);
604604
}
605605

606606
private void stopScan() {
@@ -643,7 +643,9 @@ public void onScanFailed(int errorCode) {
643643
}
644644

645645
@TargetApi(21)
646-
private void startScan21(Protos.ScanSettings proto) {
646+
private void startScan21(Protos.ScanSettings proto) throws IllegalStateException {
647+
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
648+
if(scanner == null) throw new IllegalStateException("getBluetoothLeScanner() is null. Is the Adapter on?");
647649
int scanMode = proto.getAndroidScanMode();
648650
int count = proto.getServiceUuidsCount();
649651
List<ScanFilter> filters = new ArrayList<>(count);
@@ -653,12 +655,13 @@ private void startScan21(Protos.ScanSettings proto) {
653655
filters.add(f);
654656
}
655657
ScanSettings settings = new ScanSettings.Builder().setScanMode(scanMode).build();
656-
mBluetoothAdapter.getBluetoothLeScanner().startScan(filters, settings, getScanCallback21());
658+
scanner.startScan(filters, settings, getScanCallback21());
657659
}
658660

659661
@TargetApi(21)
660662
private void stopScan21() {
661-
mBluetoothAdapter.getBluetoothLeScanner().stopScan(getScanCallback21());
663+
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
664+
if(scanner != null) scanner.stopScan(getScanCallback21());
662665
}
663666

664667
private BluetoothAdapter.LeScanCallback scanCallback18;
@@ -679,13 +682,14 @@ public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi,
679682
return scanCallback18;
680683
}
681684

682-
private void startScan18(Protos.ScanSettings proto) {
685+
private void startScan18(Protos.ScanSettings proto) throws IllegalStateException {
683686
List<String> serviceUuids = proto.getServiceUuidsList();
684687
UUID[] uuids = new UUID[serviceUuids.size()];
685688
for(int i = 0; i < serviceUuids.size(); i++) {
686689
uuids[0] = UUID.fromString(serviceUuids.get(0));
687690
}
688-
mBluetoothAdapter.startLeScan(uuids, getScanCallback18());
691+
boolean success = mBluetoothAdapter.startLeScan(uuids, getScanCallback18());
692+
if(!success) throw new IllegalStateException("getBluetoothLeScanner() is null. Is the Adapter on?");
689693
}
690694

691695
private void stopScan18() {

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ class _FlutterBlueAppState extends State<FlutterBlueApp> {
196196
}
197197

198198
_buildScanningButton() {
199-
if (isConnected || state != BluetoothState.on) {
199+
/*if (isConnected || state != BluetoothState.on) {
200200
return null;
201-
}
201+
}*/
202202
if (isScanning) {
203203
return new FloatingActionButton(
204204
child: new Icon(Icons.stop),

0 commit comments

Comments
 (0)