简体   繁体   中英

How to get bluetooth paired devices in the area

I want to get a list of all the devices in the area.

For that i'm requesting the bonded devices with getBondedDevices() and then making a discovery. But this gives me the list of all bonded devices (in the area or not) and the discoverable ones. And if I just make the discovery (without using getBondedDevices()) I don't get the bonded devices in the area.

So I want to get a list with the bonded devices (but only those in the area) and the discoverable devices.

Thanks for the help

The way I solved this was to try and connect to the device. It seems to work as long as the device in question is actually listening for a connection. If you don't know what type of device you're dealing with you have to scan for the available services it provides. This is a bit weird since it seems you can only call the getUuids by using reflection. See code below.

try {
    Method method = device.getClass().getMethod("getUuids"); /// get all services
    ParcelUuid[] parcelUuids = (ParcelUuid[]) method.invoke(device); /// get all services

    BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(parcelUuids[0].getUuid()); ///pick one at random
    socket.connect();
    socket.close();
} catch (Exception e) {
    Log.e("BluetoothPlugin", device.getName() + "Device is not in range");
}

没有直接的API,您可以比较发现和getBondedDevices返回的公共设备,以查找附近的可见/可发现的绑定设备。

The Bluetooth devices I have aren't discoverable once they're bonded, which is why the discovery doesn't list them. This is the nature of the devices (no one else can use a device if you're already using it), not a limitation of the Android API.

So probably the only way to get a complete list of devices in the area is to try opening a connection to each bonded device to see if it's in the area, and merge that subset with the discoverable list. It's an indirect way, but I couldn't find another way.

I think the way other software deals with this is to keep the bonded and discoverable lists separate and make the user determine which bonded devices they care about (known to be in range through personal knowledge).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM