Search code examples
androidandroid-thingsrxandroidble

Location Services disabled on Android Things


When attempting to run the BLE Indoor Positioning Demo App on Android Things (using the NXP i.MX7D Developer Kit), I get Bluetooth scanning error: Location Services disabled (code 4).

E/BluetoothClient: Bluetooth scanning error: Location Services disabled (code 4)
W/System.err: com.polidea.rxandroidble.exceptions.BleScanException: Location Services disabled (code 4)
W/System.err:     at com.polidea.rxandroidble.internal.scan.ScanPreconditionsVerifierApi18.verify(ScanPreconditionsVerifierApi18.java:31)
W/System.err:     at com.polidea.rxandroidble.internal.scan.ScanPreconditionsVerifierApi24.verify(ScanPreconditionsVerifierApi24.java:38)
W/System.err:     at com.polidea.rxandroidble.RxBleClientImpl$1.call(RxBleClientImpl.java:114)
W/System.err:     at com.polidea.rxandroidble.RxBleClientImpl$1.call(RxBleClientImpl.java:111)
W/System.err:     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
W/System.err:     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
W/System.err:     at rx.Observable.subscribe(Observable.java:10352)
W/System.err:     at rx.Observable.subscribe(Observable.java:10319)
W/System.err:     at rx.Observable.subscribe(Observable.java:10227)
W/System.err:     at com.nexenio.bleindoorpositioningdemo.bluetooth.BluetoothClient.startScanning(BluetoothClient.java:79)
W/System.err:     at com.nexenio.bleindoorpositioningdemo.HomeActivity.onResume(HomeActivity.java:108)
W/System.err:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)
W/System.err:     at android.app.Activity.performResume(Activity.java:7118)
W/System.err:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3556)
W/System.err:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862)
W/System.err:     at android.app.ActivityThread.-wrap11(Unknown Source:0)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:164)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

The app uses RxAndroidBle for dealing with Android's Bluetooth stack. It also declares these permission in the manifest:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

As of my understanding, Android Things should automatically grant these permissions at install time or after a reboot.

I don't know how I should enable the location services manually or programatically.


Solution

  • The issue is not with permissions. The RxAndroidBle library makes some assumptions about location services that aren't necessarily appropriate for Android Things (since those settings aren't actually controlled by users). The reason the library check is failing is because NETWORK_PROVIDER is not enabled and GPS_PROVIDER is only enabled if you have a GPS driver registered. You don't actually need either of those to do a BLE scan, however.

    Fortunately for you, it looks like the author of RxAndroidBle recently changed the code to also verify if the location mode settings were enabled. This check will pass on Android Things, so in your case you probably only need to update your library dependency to 1.5:

    dependencies {
        ...
        implementation "com.polidea.rxandroidble:rxandroidble:1.5.0"
    }