Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setNeedBle from SettingsApi not working on API23

I'm using the com.google.android.gms.location.SettingsApi to enable Bluetooth and location in my Android app. It brings up a dialog prompting the user to enable it when it's disabled so that my app can scan for nearby Bluetooth devices. I followed the instructions in the reference guide (https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi). It's pretty straightforward: first you connect to the GoogleApiClient and then you make a LcoationSettingsRequest.builder:

 LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
     .addLocationRequest(mLocationRequestHighAccuracy)
     .setNeedBle(true)

In my case I added setNeedBle(true) since I need to turn Bluetooth on as well. You then call the checkLocationSettings from the SettingsApi and add a callback to the result. This brings up a dialog prompting the user to turn on Bluetooth and Location and you get a callback with whether the user chose to do this or not.

This works fine on a device with API22 and Google Play Services version 8.4.89 but does not on the same device with API23 and GPS version 8.4.89 as well. It is only the setNeedBle(true) part that is not working on API23: when Bluetooth is off I do not get a dialog (but the dialog for enabling the location works fine). Since they both have the same version of GPS installed it have something to do with API22 vs API23. Does anyone know whether this is a bug or how I can resolve this problem?

I found a similar question (How to pop up Enable Bluetooth prompt from Google Play Services?) about this but there it is suggested that is fixed in GPS version 8.1 but I'm running GPS v8.4. Also, it's a slightly different problem since I'm not getting a SETTINGS_CHANGE_UNAVAILABLE status.

like image 313
Nimyz Avatar asked Nov 08 '22 19:11

Nimyz


1 Answers

The location service in API 23 has a Scanning setting panel.

location

There are toggles for Wifi and Bluetooth scanning to improve location accuracy.

scanning

By toggling it on, location service can use BLE scanning even if Bluetooth is not enabled. In this case, setNeedBle(true) will not prompt user to open Bluetooth since location service is already able to use BLE.

But I have no idea how to make the BLE scanning in location service available in our own app, maybe it is not possible.

I suggest to check whether Bluetooth is enabled after request location service, and show Bluetooth enabling prompt if it is not enabled.

like image 57
Jeffrey Chen Avatar answered Nov 14 '22 23:11

Jeffrey Chen