How to pop up Enable Bluetooth prompt from Google

2019-05-06 23:21发布

问题:

Application I'm working on uses both Locations and BLE and if location or bluetooth are disabled I have to ask user to enable them.

Latest Google Play Services provides a standard way to do that using LocationSettingsRequest which checks requirements and raises standard popup if changes to settings are required. It works like a charm for location alone but once I add SetNeedBle (true) to LocationSettingsRequest I get a status SETTINGS_CHANGE_UNAVAILABLE.

The only my guess was I need to add AddApi (FitnessClass.BLE_API) call to a GoogleApiClientBuilder as it might be vital for BLE functionality, but then I got connection to Google Play Services failed with SIGN_IN_REQUIRED status which is confusing as I just need BLE part of Fitness service.

Does anyone know good example of LocationSettingsRequest usage to prompt user for both locations and bluetooth?

回答1:

You are very close. In the LocationSettingsRequest.Builder there is setNeedBle(boolean needBle) which will pop up a dialog box to ask for BLE. Don't use the Fitness API for BLE location.

Also ensure that the phone is BLE enable by adding into the manifest:

<manifest>
<uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="false" />
</manifest>

Then in you code :

if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    //has BLE
}

And from Google Play Service you can use the SettingApi which ask the system about available features. The guide contains a full example of how to use it.

https://developers.google.com/android/reference/com/google/android/gms/location/LocationSettingsRequest.Builder.html#setNeedBle(boolean)



回答2:

Look like it has been fixed in 8.1.0 so SetNeedBle (true) works as expected: