I'm programming in BlueZ on my Raspberry Pi with an USB Bluetooth dongle.
I need to be able to programmatically connect to an Arduino BT, the problem is that the Bluetooth module of the Arduino is still using legacy pairing so whenever I try to open a socket to the device I get a Permission Denied
.
How do I send along a PIN to complete the pairing request through BlueZ?

- 7,044
- 3
- 27
- 46

- 35,759
- 6
- 62
- 122
-
Have you test with the `simple-agent`? – Ôrel Apr 21 '15 at 08:50
-
I want to do it without third party software, just through the bluez C API. – Hatted Rooster Apr 21 '15 at 09:06
-
Have try this: http://stackoverflow.com/questions/14820004/bluetooth-pairing-in-c-bluez-on-linux – Ôrel Apr 21 '15 at 09:14
-
I would suggest you use the bluez DBUS APIs rather than the internal hcitools code. It's better supported and at a higher level. If you go that route, what you need to do is to register your application as an Agent. Then whenever a BT authentication is required, your Agent will receive the PIN/passkey request. Please see the bluez [agent doc](https://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/agent-api.txt) for the API. Or you can initiate a pairing using the Device api. See the device [device doc](https://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/device-api.txt) for details. – kaylum Apr 21 '15 at 23:05
-
@Alan Au it looks like those API's are both for Python, is there no documentation for C? – Hatted Rooster Apr 21 '15 at 23:12
-
They are not python APIs. They are DBUS APIs. If you haven't dealt with DBUS then there is a learning curve. Basically DBUS is a messaging system. There are different bindings (C, python, etc) which contain dbus support. For C, two common ones are [glib](https://developer.gnome.org/gio/stable/gdbus-convenience.html) and QT (no link as I don't use that one). bluez internally uses glib. – kaylum Apr 21 '15 at 23:16
-
The docs tell you what the Bluez DBUS interfaces and properties are. Then what you do is (using your chosen dbus binding) invoke the Bluez DBUS apis to Discover, Pair, Unpair, etc. For a quick start, look at the [bluez implementation for its bluetoothctl](https://git.kernel.org/cgit/bluetooth/bluez.git/tree/client) command. That uses the DBUS APIs to implement a command line that performs various BT operations. – kaylum Apr 21 '15 at 23:19
-
@alan au oh I see, thanks. I assume there are no examples of the DBUS API used for bluetooth agent purposes? Since DBUS is for more things than jusr Bluetooth I'm afraid I might be overwhelmed – Hatted Rooster Apr 21 '15 at 23:20
-
@alan au Thanks for the link, I'll check that out to start – Hatted Rooster Apr 21 '15 at 23:21
-
No problems. If you have any specific questions after looking at that please post a new question and hopefully myself or someone can help you with that. – kaylum Apr 21 '15 at 23:22
1 Answers
You might want to check out the main.c
file in the client folder of the most recent Bluez source code. It's the source code for the bluetoothctl tool. Run it too. The source code shows exactly how they use GDBus, including proxies, agents, calling methods like described in the API (/doc folder) and all that. It's in C and uses the high level API.
I suggest you step through the code because it took me 2 weeks endlessly trying to understand Bluez in C and the fact that there's no documentation, but when I read that main.c file I was ready in a day. Read up on proper Dbus API documentation and more importantly the concepts. Some documents that helped me:
The gdbus tool: https://developer.gnome.org/gio/stable/gdbus.html
These contain all the calls to gdbus and objects in the main.c
file and explain them very well.
https://developer.gnome.org/gio/stable/gdbus-convenience.html
D-Feet, an invaluable tool to inspecting and learning about Dbus on your system. Try checking out the /bluez bus. https://wiki.gnome.org/action/show/Apps/DFeet?action=show&redirect=DFeet
or
sudo apt-get install d-feet
Not much of a tutorial, but worth a read to understand some concepts, as the bluetoothctl tool fits into what they're trying to say here. http://dbus.freedesktop.org/doc/dbus-tutorial.html
The bluetoothctl creates an interactive shell though, so it might not be wise to waste time trying to fit in your code, but just pick what you need from it.

- 1,870
- 2
- 23
- 41