Search code examples
ubuntubluezparallelsbluetooth-lowenergy

bluez-tools bt-adapter -d requires usb dongle reset before next use


Im writing a BLE app to read/write to peripheral characteristics and am using bluez-tools as an example. One of the requirements for this is to discover nearby BLE devices and it would be preferred if this process would work on every app launch.

However, after using bt-adapter -d for the first time like this

$ bt-adapter -d
Searching...
[69:F6:C6:1E:2F:B2]
  Name: 69E90E1E
  Alias: 69E90E1E
  Address: 69:F6:C6:1E:2F:B2
  Icon: (null)
  Class: 0x0
  LegacyPairing: 0
  Paired: 0
  RSSI: -63

^C
$

then for the second time I dont get any results.

$ bt-adapter -d
Searching...
^C
$

meanwhile hcitool lescan find the peripheral successfully.

If I unplug and replug my ble dongle I get this error:

$ bt-adapter -d
Searching...
Error: GDBus.Error:org.bluez.Error.NotReady: Resource Not Ready
$

And after I reset the hci0

$ sudo hciconfig hci0 down
$ sudo hciconfig hci0 up

The discovery is working again.

How do I keep the bt-adapter -d working without the need to reset ble dongle after each app launch?

Im using Parallels 11 on a mac with Ubuntu 15.10


Solution

  • Now that I have investigated bluez a bit more I understand that it keeps the previously discovered devices in the cache and because of this the InterfacesAdded signal is not invoked on the ObjectManager.

    I have followed this example which uses g_dbus_object_manager_get_objects before StartDiscovery to check if bluez has cached devices.