我正在按照低功耗蓝牙设备的文档扫描 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/