获取蓝牙适配器时的Lint错误

My *_*God 7 android bluetooth-lowenergy android-bluetooth

我正在关注Bluetooth Low Energy设备的文档以扫描BLE设备.

正如文档中所提到的,我定义了---

BluetoothAdapter mBluetoothAdapter = null;

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

mBluetoothAdapter = bluetoothManager.getAdapter(); //Lint Error..
Run Code Online (Sandbox Code Playgroud)

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

调用需要API级别18(当前最小值为8):android.bluetooth.BluetoothManager#getAdapter

所以我改变了我的代码 -

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Run Code Online (Sandbox Code Playgroud)

代码是否替换上述lint错误?

Pan*_*mar 8

你可以打电话BluetoothAdapter.getDefaultAdapter().BluetoothManager文档说明了这一点

使用带有BLUETOOTH_SERVICE的getSystemService(java.lang.String)创建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();
}
Run Code Online (Sandbox Code Playgroud)