@@ -271,17 +271,10 @@ public List<BeaconParser> getBeaconParsers() {
271
271
*/
272
272
@ TargetApi (18 )
273
273
public boolean checkAvailability () throws BleNotAvailableException {
274
- if (android . os . Build . VERSION . SDK_INT < 18 ) {
274
+ if (! isBleAvailable () ) {
275
275
throw new BleNotAvailableException ("Bluetooth LE not supported by this device" );
276
276
}
277
- if (!mContext .getPackageManager ().hasSystemFeature (PackageManager .FEATURE_BLUETOOTH_LE )) {
278
- throw new BleNotAvailableException ("Bluetooth LE not supported by this device" );
279
- } else {
280
- if (((BluetoothManager ) mContext .getSystemService (Context .BLUETOOTH_SERVICE )).getAdapter ().isEnabled ()) {
281
- return true ;
282
- }
283
- }
284
- return false ;
277
+ return ((BluetoothManager ) mContext .getSystemService (Context .BLUETOOTH_SERVICE )).getAdapter ().isEnabled ();
285
278
}
286
279
287
280
/**
@@ -292,8 +285,12 @@ public boolean checkAvailability() throws BleNotAvailableException {
292
285
* @param consumer the <code>Activity</code> or <code>Service</code> that will receive the callback when the service is ready.
293
286
*/
294
287
public void bind (BeaconConsumer consumer ) {
295
- if (android .os .Build .VERSION .SDK_INT < 18 ) {
296
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
288
+ if (!isBleAvailable ()) {
289
+ LogManager .w (TAG , "Method invocation will be ignored." );
290
+ return ;
291
+ }
292
+ if (!mContext .getPackageManager ().hasSystemFeature (PackageManager .FEATURE_BLUETOOTH_LE )) {
293
+ LogManager .w (TAG , "This device does not support bluetooth LE. Will not start beacon scanning." );
297
294
return ;
298
295
}
299
296
synchronized (consumers ) {
@@ -318,8 +315,8 @@ public void bind(BeaconConsumer consumer) {
318
315
* @param consumer the <code>Activity</code> or <code>Service</code> that no longer needs to use the service.
319
316
*/
320
317
public void unbind (BeaconConsumer consumer ) {
321
- if (android . os . Build . VERSION . SDK_INT < 18 ) {
322
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
318
+ if (! isBleAvailable () ) {
319
+ LogManager .w (TAG , "Method invocation will be ignored. " );
323
320
return ;
324
321
}
325
322
synchronized (consumers ) {
@@ -391,8 +388,9 @@ public boolean isAnyConsumerBound() {
391
388
* @see #setBackgroundBetweenScanPeriod(long p)
392
389
*/
393
390
public void setBackgroundMode (boolean backgroundMode ) {
394
- if (android .os .Build .VERSION .SDK_INT < 18 ) {
395
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
391
+ if (!isBleAvailable ()) {
392
+ LogManager .w (TAG , "Method invocation will be ignored." );
393
+ return ;
396
394
}
397
395
mBackgroundModeUninitialized = false ;
398
396
if (backgroundMode != mBackgroundMode ) {
@@ -608,8 +606,8 @@ public void requestStateForRegion(Region region) {
608
606
*/
609
607
@ TargetApi (18 )
610
608
public void startRangingBeaconsInRegion (Region region ) throws RemoteException {
611
- if (android . os . Build . VERSION . SDK_INT < 18 ) {
612
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
609
+ if (! isBleAvailable () ) {
610
+ LogManager .w (TAG , "Method invocation will be ignored. " );
613
611
return ;
614
612
}
615
613
if (serviceMessenger == null ) {
@@ -636,8 +634,8 @@ public void startRangingBeaconsInRegion(Region region) throws RemoteException {
636
634
*/
637
635
@ TargetApi (18 )
638
636
public void stopRangingBeaconsInRegion (Region region ) throws RemoteException {
639
- if (android . os . Build . VERSION . SDK_INT < 18 ) {
640
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
637
+ if (! isBleAvailable () ) {
638
+ LogManager .w (TAG , "Method invocation will be ignored. " );
641
639
return ;
642
640
}
643
641
if (serviceMessenger == null ) {
@@ -671,8 +669,8 @@ public void stopRangingBeaconsInRegion(Region region) throws RemoteException {
671
669
*/
672
670
@ TargetApi (18 )
673
671
public void startMonitoringBeaconsInRegion (Region region ) throws RemoteException {
674
- if (android . os . Build . VERSION . SDK_INT < 18 ) {
675
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
672
+ if (! isBleAvailable () ) {
673
+ LogManager .w (TAG , "Method invocation will be ignored. " );
676
674
return ;
677
675
}
678
676
if (serviceMessenger == null ) {
@@ -699,8 +697,8 @@ public void startMonitoringBeaconsInRegion(Region region) throws RemoteException
699
697
*/
700
698
@ TargetApi (18 )
701
699
public void stopMonitoringBeaconsInRegion (Region region ) throws RemoteException {
702
- if (android . os . Build . VERSION . SDK_INT < 18 ) {
703
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
700
+ if (! isBleAvailable () ) {
701
+ LogManager .w (TAG , "Method invocation will be ignored. " );
704
702
return ;
705
703
}
706
704
if (serviceMessenger == null ) {
@@ -721,8 +719,8 @@ public void stopMonitoringBeaconsInRegion(Region region) throws RemoteException
721
719
*/
722
720
@ TargetApi (18 )
723
721
public void updateScanPeriods () throws RemoteException {
724
- if (android . os . Build . VERSION . SDK_INT < 18 ) {
725
- LogManager .w (TAG , "Not supported prior to API 18. Method invocation will be ignored" );
722
+ if (! isBleAvailable () ) {
723
+ LogManager .w (TAG , "Method invocation will be ignored. " );
726
724
return ;
727
725
}
728
726
if (serviceMessenger == null ) {
@@ -895,6 +893,18 @@ public void setNonBeaconLeScanCallback(NonBeaconLeScanCallback callback) {
895
893
mNonBeaconLeScanCallback = callback ;
896
894
}
897
895
896
+ private boolean isBleAvailable () {
897
+ boolean available = false ;
898
+ if (android .os .Build .VERSION .SDK_INT < android .os .Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
899
+ LogManager .w (TAG , "Bluetooth LE not supported prior to API 18." );
900
+ } else if (!mContext .getPackageManager ().hasSystemFeature (PackageManager .FEATURE_BLUETOOTH_LE )) {
901
+ LogManager .w (TAG , "This device does not support bluetooth LE." );
902
+ } else {
903
+ available = true ;
904
+ }
905
+ return available ;
906
+ }
907
+
898
908
private long getScanPeriod () {
899
909
if (mBackgroundMode ) {
900
910
return backgroundScanPeriod ;
0 commit comments