Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluez auto reconnect devices

I am writing a library in C/C++ for a Bluetooth low energy device. So far I have been using the D-Bus interface exposed by Bluez and been able to discover, connect and communicate with the device.

However, when the device disconnects either due to a link failure or it being out of range, reconnecting to it is not trivial. Ideally I would like to be able to create pending connections to all disconnected devices, but Bluez doesn't seem to support that. It seems that Bluez only supports one simultaneous connect call, which timeouts after 15-20 seconds.

A solution would thus be to listen for advertisement packages, and connect when a known device is detected. The good people on #bluez told me that this was already implemented, and the way to do it is to register an object that implements GattProfile1 with the GattManager1 RegisterProfile method. Trying this gave no result, the device stays disconnected after a link failure. It is also very poorly documented, so there is a good chance I have missed something.

My questions are: Is this the right solution? Will it provide a seamless and fast reconnections? If it is, what can be wrong?

like image 272
prinsen Avatar asked Aug 29 '15 17:08

prinsen


People also ask

How do I get my Bluetooth to automatically reconnect?

A headset or a speaker doesn't reconnect automatically Return to Settings, search for Bluetooth auto-connect and enable this feature. Make sure that the other device is within the effective Bluetooth range from your smartphone and restart it. Re-enable Bluetooth on the phone and check if the issue is fixed*.


1 Answers

If you wish to reconnect to a single bluetooth device, you can monitor the connection state by receiving org.freedesktop.DBus.Properties.PropertiesChanged, and calling org.bluez.Device1.Connect to reconnect when necessary. Since this will timeout, you can put Connect() inside a loop which you will exit only when the org.bluez.Device1.Connected property is true.

Has the original author managed to do this yet for multiple devices by implementing GattProfile1?

like image 200
davhoo Avatar answered Sep 18 '22 06:09

davhoo