Lukas
Lukas

Reputation: 10380

Bluez doesn't re-discover devices when I repeat discovery

I am using the Bluez API over DBus on Ubuntu and on Raspian. For this I am using npm's dbus package but I am 99% sure that this is not the issue here. All it does is bridge Linux' DBus API to Node.js via C++. But that is why my code is written in JavaScript.

What I basically do is load the system bus, the bluez adapter and the object manager and start a discovery, like so (simplified semi-pseudocode):

const systembus = DBus.getBus("system");
const adapter = systembus.getInterface("org.bluez", "/org/bluez/hci0", "org.bluez.Adapter1");
const manager = systembus.getInterface("org.bluez", "/", "org.freedesktop.DBus.ObjectManager");

manager.on("InterfacesAdded", (path, interfaces) => {
  const device = interfaces["org.bluez.Device1"];
  if (device) console.log("Found", device);
});

adapter.StartDiscovery();

When I call this script, I see a list of lots of devices. When I stop the script and start it again, I see none. Or maybe like one or two. If I wait a few minutes I can see my devices again.

It seems to me I am missing a step here, maybe I need to reset the adapter, maybe the discovery is still running in the background and I have to wait before I can start a new one? Or maybe, I know that there are managed objects stored somewhere, maybe they won't be rediscovered? But loading those managed objects returns undefined.

Upvotes: 0

Views: 1232

Answers (1)

Martijn van Welie
Martijn van Welie

Reputation: 754

You only receive a signal on InterfacesAdded if Bluez is adding a new device to the dbus. If the device is unused it will be removed from the dbus after 180 seconds....and then you will receive InterfacesRemoved signal.

So in your case, you see a device when it is first added, but you don't receive it again because it is already there! If you wait longer than 180 seconds you will start seeing it again, unless the device has been bonded in which case it will stay on the dbus forever.

What you need to do is listen for PropertiesChanged. Every time a property of a device, such as the RSSI, changes you will get a callback. If you do that you will also 'see' devices that are already on the dbus.

Upvotes: 2

Related Questions