如何从 Google Play Services 弹出启用蓝牙提示?

How to pop up Enable Bluetooth prompt from Google Play Services?

我正在开发的应用程序同时使用位置和 BLE,如果位置或蓝牙被禁用,我必须要求用户启用它们。

最新 Google Play 服务提供了一种使用 LocationSettingsRequest 执行此操作的标准方法,它会检查要求并在需要更改设置时引发标准弹出窗口。它就像位置的魅力一样,但是一旦我将 SetNeedBle (true) 添加到 LocationSettingsRequest 我得到状态 SETTINGS_CHANGE_UNAVAILABLE.

我唯一的猜测是我需要添加 AddApi (FitnessClass.BLE_API)GoogleApiClientBuilder 的调用,因为它可能对 BLE 功能至关重要,但后来我连接到 Google 播放服务失败SIGN_IN_REQUIRED 状态令人困惑,因为我只需要 Fitness 服务的 BLE 部分。

有人知道 LocationSettingsRequest 提示用户输入位置和蓝牙的好例子吗?

你们很亲近。 LocationSettingsRequest.Builder里面有setNeedBle(boolean needBle)会弹出对话框要求BLE。不要将 Fitness API 用于 BLE 位置。

同时确保 phone 通过添加到清单中启用 BLE:

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

然后在你的代码中:

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

从 Google Play Service 中,您可以使用 SettingApi 向系统询问可用功能。该指南包含如何使用它的完整示例。

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

看起来它已在 8.1.0 中得到修复,因此 SetNeedBle (true) 按预期工作: