BluetoothAdapter.getDefaultAdapter()不返回null [英] BluetoothAdapter.getDefaultAdapter() Not returning null

查看:568
本文介绍了BluetoothAdapter.getDefaultAdapter()不返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,所以如果我做些愚蠢的事,请告诉我.这个问题可能看起来与其他帖子相似,但与我所见过的一切差不多.

This is my first post so if I do somethign dumb let me know please. This question may look similare to other posts but is more or less the opposite of everything that I've seen.

有关项目的信息:

  • 我正在使用android 4.0-4.4应用程序.
  • 我正在使用蓝牙
  • 我正在运行Android 4.2的物理设备(Eken Necnon)上进行测试
  • 该设备没有蓝牙硬件

我遇到的问题是,当我尝试使用BluetoothAdapter.getDefaultAdapter()获取蓝牙适配器时,它应该返回null,但不是.

The problem I'm having is that when I try to get a Bluetooth Adapter using BluetoothAdapter.getDefaultAdapter() it should return null but it isn't.

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (mBluetoothAdapter == null) { // This does not ever return true.
    Log.w("Bluetooth", "Initializing bluetooth device failed: Bluetooth not supported.");
    return;
}

if (!mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.enable();
}

while (mBluetoothAdapter.getState() != BluetoothAdapter.STATE_ON) {
    try {
        Log.d(TAG, "Waiting for bluetooth adapter to turn on, taking nap.");
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

推荐答案

这似乎是设备特定的问题.或者,您可以使用PackageManager来查找是否支持蓝牙.

This appears to be a device specific issue. Alternatively you can use PackageManager to find out whether bluetooth is supported.

PackageManager pm = context.getPackageManager();
boolean hasBluetooth = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

在访问相应的API之前检查支持系统功能的位置始终是一个好习惯.

It is always a good practice to check where system feature is supported before accessing corresponding API.

这篇关于BluetoothAdapter.getDefaultAdapter()不返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆