Skip to content

Commit 313262d

Browse files
authored
Merge pull request #492 from AltBeacon/do-not-scan-if-ble-not-supported
Don't start scanning service if BLE unsupported
2 parents c3aac21 + 6c0ddaf commit 313262d

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

src/main/java/org/altbeacon/beacon/BeaconManager.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,10 @@ public List<BeaconParser> getBeaconParsers() {
271271
*/
272272
@TargetApi(18)
273273
public boolean checkAvailability() throws BleNotAvailableException {
274-
if (android.os.Build.VERSION.SDK_INT < 18) {
274+
if (!isBleAvailable()) {
275275
throw new BleNotAvailableException("Bluetooth LE not supported by this device");
276276
}
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();
285278
}
286279

287280
/**
@@ -292,8 +285,12 @@ public boolean checkAvailability() throws BleNotAvailableException {
292285
* @param consumer the <code>Activity</code> or <code>Service</code> that will receive the callback when the service is ready.
293286
*/
294287
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.");
297294
return;
298295
}
299296
synchronized (consumers) {
@@ -318,8 +315,8 @@ public void bind(BeaconConsumer consumer) {
318315
* @param consumer the <code>Activity</code> or <code>Service</code> that no longer needs to use the service.
319316
*/
320317
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.");
323320
return;
324321
}
325322
synchronized (consumers) {
@@ -391,8 +388,9 @@ public boolean isAnyConsumerBound() {
391388
* @see #setBackgroundBetweenScanPeriod(long p)
392389
*/
393390
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;
396394
}
397395
mBackgroundModeUninitialized = false;
398396
if (backgroundMode != mBackgroundMode) {
@@ -608,8 +606,8 @@ public void requestStateForRegion(Region region) {
608606
*/
609607
@TargetApi(18)
610608
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.");
613611
return;
614612
}
615613
if (serviceMessenger == null) {
@@ -636,8 +634,8 @@ public void startRangingBeaconsInRegion(Region region) throws RemoteException {
636634
*/
637635
@TargetApi(18)
638636
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.");
641639
return;
642640
}
643641
if (serviceMessenger == null) {
@@ -671,8 +669,8 @@ public void stopRangingBeaconsInRegion(Region region) throws RemoteException {
671669
*/
672670
@TargetApi(18)
673671
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.");
676674
return;
677675
}
678676
if (serviceMessenger == null) {
@@ -699,8 +697,8 @@ public void startMonitoringBeaconsInRegion(Region region) throws RemoteException
699697
*/
700698
@TargetApi(18)
701699
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.");
704702
return;
705703
}
706704
if (serviceMessenger == null) {
@@ -721,8 +719,8 @@ public void stopMonitoringBeaconsInRegion(Region region) throws RemoteException
721719
*/
722720
@TargetApi(18)
723721
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.");
726724
return;
727725
}
728726
if (serviceMessenger == null) {
@@ -895,6 +893,18 @@ public void setNonBeaconLeScanCallback(NonBeaconLeScanCallback callback) {
895893
mNonBeaconLeScanCallback = callback;
896894
}
897895

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+
898908
private long getScanPeriod() {
899909
if (mBackgroundMode) {
900910
return backgroundScanPeriod;

0 commit comments

Comments
 (0)