Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BluetoothAdapter.getDefaultAdapter()未返回null_Android_Bluetooth - Fatal编程技术网

Android BluetoothAdapter.getDefaultAdapter()未返回null

Android BluetoothAdapter.getDefaultAdapter()未返回null,android,bluetooth,Android,Bluetooth,这是我的第一篇帖子,如果我做了什么傻事,请告诉我。这个问题可能看起来与其他帖子类似,但或多或少与我所看到的相反 关于项目的事情: 我正在开发android 4.0-4.4应用程序 我正在使用蓝牙 我正在运行android 4.2的物理设备(Eken Necnon)上进行测试 该设备没有蓝牙硬件 我遇到的问题是,当我尝试使用BluetoothAdapter.getDefaultAdapter()获取蓝牙适配器时,它应该返回null,但不是 BluetoothAdapter mBluetooth

这是我的第一篇帖子,如果我做了什么傻事,请告诉我。这个问题可能看起来与其他帖子类似,但或多或少与我所看到的相反

关于项目的事情:

  • 我正在开发android 4.0-4.4应用程序
  • 我正在使用蓝牙
  • 我正在运行android 4.2的物理设备(Eken Necnon)上进行测试
  • 该设备没有蓝牙硬件
我遇到的问题是,当我尝试使用BluetoothAdapter.getDefaultAdapter()获取蓝牙适配器时,它应该返回null,但不是

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
查看是否支持蓝牙

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

在访问相应的API之前,最好先检查系统功能支持的位置。

非常感谢,这正是我需要的。