【问题标题】:Lint error in getting Bluetooth adapter获取蓝牙适配器时出现 Lint 错误
【发布时间】:2014-04-26 18:57:13
【问题描述】:

我正在关注低功耗蓝牙设备的文档以扫描 BLE 设备。

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

BluetoothAdapter mBluetoothAdapter = null;

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

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

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

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

所以我将代码更改为--

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

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

【问题讨论】:

  • 目前安卓只支持一个蓝牙适配器,所以你可以用BluetoothManager.getAdapter()
  • 你的意思是我可以用-BluetoothAdapter.getDefaultAdapter()
  • 当然可以,但现在没有意义,因为上面的原因

标签: android bluetooth-lowenergy android-bluetooth


【解决方案1】:

您可以拨打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();
}

【讨论】:

猜你喜欢
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多