android - 获取蓝牙适配器时出现 Lint 错误

标签 android bluetooth-lowenergy android-bluetooth

我正在按照低功耗蓝牙设备的文档扫描 BLE 设备。

如文档中所述,我定义了 ---

BluetoothAdapter mBluetoothAdapter = null;

final BluetoothManager bluetoothManager = 
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = bluetoothManager.getAdapter(); //Lint Error..

但是我遇到了一个 Lint 错误---

Call requires API level 18 (current min is 8): android.bluetooth.BluetoothManager#getAdapter

所以我将我的代码更改为——

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

上述 lint 错误的代码替换了吗?

最佳答案

您可以调用 BluetoothAdapter.getDefaultAdapter()BluetoothManager documentation说是

使用 getSystemService(java.lang.String)BLUETOOTH_SERVICE创建一个 BluetoothManager , 然后调用 getAdapter()获得BluetoothAdapter .

或者,您可以只调用静态助手 getDefaultAdapter() .


或者您可以检查构建版本并初始化mBluetoothAdapter,如下所示

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        mBluetoothAdapter = bluetoothManager.getAdapter();
} else {
       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

关于android - 获取蓝牙适配器时出现 Lint 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218859/

相关文章:

android - Unity 2019 - SDK 工具版本 0.0 < 26.1.1

android - 未生成蓝牙 HCI 监听日志

android - 如何以编程方式判断蓝牙设备是否已连接?

android - 无法使用 gradle 导入 android.support.v7 和注释库

android - 如何解决 'Failed to reslove : ' com.android.support :support-v4-preferencefragment:1. 0.0l

android - bindservice 中的 onServiceConnected 只被调用一次

Android - 低功耗蓝牙遥控键盘和鼠标

使用过滤器的 Android BLE 设备扫描不起作用

android - OutOfMemory 异常 Android VectorBitmap

random - BLE IoT 设备通常如何生成其私有(private) MAC 地址?