getbondeddevices()没有返回配对的蓝牙设备 [英] getbondeddevices() not returning paired bluetooth devices

查看:3612
本文介绍了getbondeddevices()没有返回配对的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与我的设备枚举所有配对的蓝牙设备。在设置我可以查看配对设备,但下面code不返回任何内容:

I am trying to enumerate all paired bluetooth devices with my device. In settings I can view the paired devices, but the following code does not return any items:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDeviceSet = bluetoothAdapter.getBondedDevices();

我已经看到并使用此方法的其他职位,但我不能似乎得到它的工作。

I have seen this and other posts that use this method, but I cannot seem to get it to work.

我有以下权限的Manifest.xml:

I have the following permissions in Manifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

此外,如果我把配对设备进入发现模式之一,扫描,​​然后将设备回来为配对。如果我检查:

Furthermore, if I put one of the paired devices into discovery mode, and scan, then the device comes back as paired. If I check:

device.getBondState() == BluetoothDevice.BOND_BONDED

从扫描,则返回true。

from the scan, it returns true.

我是什么做错了还是不理解?

What am I doing wrong or not understanding?

推荐答案

您code是完全正确的。我有同样的事情在我的应用程序,我从来没有从这个功能不起作用任何用户的任何抱怨。请检查您的应用程序的其他部分。下面是我的应用程序,做同样的事情的片段,我也像你描述的相同的权限。

Your code is completely correct. I have the exact same thing in my app and I never got any complain from any user that this feature does not work. Please check for other parts of your app. Below is the snippet of my app that does the same thing, and I have the same permission as you described.

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();

Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

if (pairedDevices.size() > 0) {
    findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
    for (BluetoothDevice device : pairedDevices) {
        mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
} else {
    mPairedDevicesArrayAdapter.add("No Paired Device.");
}

这篇关于getbondeddevices()没有返回配对的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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