检查蓝牙配对

Check Bluetooth Pairing

我正在尝试查看是否除了 Bluetooth 之外,是否与任何设备配对。 要查看 Bluetooth 是否打开,我使用以下命令:

BluetoothAdapter.getDefaultAdapter().isEnabled()

我需要知道那个蓝牙适配器是否与任何设备配对。

谢谢你,希望你的回答

编辑

如果我使用:

BluetoothAdapter.getDefaultAdapter().getBondedDevices();

并且size() > 0,是配对的意思吗?或者已经存储设备?

编辑

对不起,我需要的不是获取已配对设备的列表,而是如果其中一些已经配对的设备已连接到我的智能手机

您可以使用此查询配对设备:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

Before performing device discovery, its worth querying the set of paired devices to see if the desired device is already known. To do so, call getBondedDevices(). This will return a Set of BluetoothDevices representing paired devices. For example, you can query all paired devices and then show the name of each device to the user, using an ArrayAdapter.

如官方文档所述here and nice sample for simple Bluetooth chat application here

获取已配对的设备列表使用此:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices == null || pairedDevices.size() == 0) {
             Toast.makeText(getApplicationContext(),"No Paired Devices Found",Toast.LENGTH_SHORT).show();
        } else {
            ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>();
            list.addAll(pairedDevices);           
        }