开发者

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 开发者_JS百科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");
}


There are no direct API for that, you can compare the common devices which and returned by discovery and getBondedDevices to find bonded devices in vicinity and that are visible/discoverable.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜