15
15
import android .bluetooth .BluetoothGattService ;
16
16
import android .bluetooth .BluetoothManager ;
17
17
import android .bluetooth .BluetoothProfile ;
18
+ import android .bluetooth .le .BluetoothLeScanner ;
18
19
import android .bluetooth .le .ScanCallback ;
19
20
import android .bluetooth .le .ScanFilter ;
20
21
import android .bluetooth .le .ScanResult ;
@@ -591,16 +592,15 @@ private void startScan(MethodCall call, Result result) {
591
592
Protos .ScanSettings settings ;
592
593
try {
593
594
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 );
602
603
}
603
- result .success (null );
604
604
}
605
605
606
606
private void stopScan () {
@@ -643,7 +643,9 @@ public void onScanFailed(int errorCode) {
643
643
}
644
644
645
645
@ 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?" );
647
649
int scanMode = proto .getAndroidScanMode ();
648
650
int count = proto .getServiceUuidsCount ();
649
651
List <ScanFilter > filters = new ArrayList <>(count );
@@ -653,12 +655,13 @@ private void startScan21(Protos.ScanSettings proto) {
653
655
filters .add (f );
654
656
}
655
657
ScanSettings settings = new ScanSettings .Builder ().setScanMode (scanMode ).build ();
656
- mBluetoothAdapter . getBluetoothLeScanner () .startScan (filters , settings , getScanCallback21 ());
658
+ scanner .startScan (filters , settings , getScanCallback21 ());
657
659
}
658
660
659
661
@ TargetApi (21 )
660
662
private void stopScan21 () {
661
- mBluetoothAdapter .getBluetoothLeScanner ().stopScan (getScanCallback21 ());
663
+ BluetoothLeScanner scanner = mBluetoothAdapter .getBluetoothLeScanner ();
664
+ if (scanner != null ) scanner .stopScan (getScanCallback21 ());
662
665
}
663
666
664
667
private BluetoothAdapter .LeScanCallback scanCallback18 ;
@@ -679,13 +682,14 @@ public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi,
679
682
return scanCallback18 ;
680
683
}
681
684
682
- private void startScan18 (Protos .ScanSettings proto ) {
685
+ private void startScan18 (Protos .ScanSettings proto ) throws IllegalStateException {
683
686
List <String > serviceUuids = proto .getServiceUuidsList ();
684
687
UUID [] uuids = new UUID [serviceUuids .size ()];
685
688
for (int i = 0 ; i < serviceUuids .size (); i ++) {
686
689
uuids [0 ] = UUID .fromString (serviceUuids .get (0 ));
687
690
}
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?" );
689
693
}
690
694
691
695
private void stopScan18 () {
0 commit comments