intel-iot-devkit / tinyb

TinyB exposes the BLE GATT API for C++, Java and other languages, using BlueZ over DBus.
MIT License
254 stars 114 forks source link

BluetoothManager::get_devices() returns devices that are no longer present #36

Open whbruce opened 8 years ago

whbruce commented 8 years ago

BluetoothManager::get_devices() seems to return a list of all devices it ever seen rather than the ones that are currently in range.

Expected behavior should be similar to hcitool lescan which just lists devices in range.

petreeftime commented 8 years ago

BluetoothManager::get_devices() does not start a scan, it returns the list of devices BlueZ knows about, whether they are recently scanned or have been connected to in the past. This is the expected behavior as per BlueZ or blueman-manager. A function like get_nearby_devices which starts a scan and checks that the devices are also in range might clear this up.

whbruce commented 8 years ago

I call BluetoothManager::start_discovery() and wait several seconds before calling BluetoothManager::get_devices() but still get previously connected devices that are no longer present. Your proposal of get_nearby_devices sounds ideal. In the meantime what is the best way to filter out devices that no longer present? I can try to connect and wait for failure but that takes too long.

petreeftime commented 8 years ago

Yes, after you start discovery, they should get an RSSI value different than 0 after a while. I'm not sure there's any other way to do this right now.

whbruce commented 8 years ago

Thanks for RSSI tip. I found out that you have to call BluetoothDevice::get_rssi() before BluetoothManager::stop_discovery() to get valid values.

A get_nearby_devices() call would do this for you, so tag this issue as a feature request.

lsimedia commented 7 years ago

I implemented an additional method called removeDevices() in the adaptor class, which will remove all known devices from internal list (so getDevice() will return an empty list). I call this method just before calling startDiscovery() so I will receive only the current nearby beacons. I've implemented it on the java side only (for c++ the method remove_device was already present)...

framspott94 commented 7 years ago

Hi Isimedia,

is the feature you implemented, already in the library? I eventually need this feature very much, therefore it would be great, if you could explain to me how I can use this function.

All the best

framspott94

lsimedia commented 7 years ago

I've done a pull request with the modification, so the maintainer can review the code and merge it if needed. For the moment you can clone my tinyb fork. The method to call is int cnt = manager.getAdapters().get(0).removeDevices(); Java code only

framspott94 commented 7 years ago

Thanks for the answer! I cloned your fork and used it succesfully!

All the best - and thanks for the feature!

framspott94

sbodmer commented 7 years ago

I have added a new method called startNearbyDiscovery() in the main BluetoothManager class with a listener as argument to be notified when a new device is found until stopNearbyDiscovery() is called. This approach is useful in my use case (and perhaps your's too...)