Bluetooth pairing in C blueZ on Linux

okipol picture okipol · Feb 11, 2013 · Viewed 28.7k times · Source

I cannot find any reference on how to pair a bluetooth device on linux in a program written in C using the BlueZ Bluetooth libraries. I already managed to do a HCI level query to get devices along with thier RSSI levels (during the device discovery) but currently I am stuck with this. I saw a suggestion to use the DBUS api for the blueZ-simple-agent - but is there any way to avoid this and just use some C level methods from BlueZ?

Answer

progo picture progo · Mar 26, 2014

You can download the newest version of the source code here: http://www.bluez.org/ There ist the tool "btmgmt" and also the bluez-simple-agent that can be used for pairing. The code is all in the sources and there is also some documentation (in docs folder). Maybe you can use code of one of these tools for your desires or maybe it helps you understand the pairing.

I want to pair 2 device with the bluez bluetooth library in the first place but I happend to find helpful code in the source for the bluez-tools. There is the file "btmgmt.c" and some files that are include in it which implement the pairing.

For me unfortunately it is not working and I can't understand why. But maybe you have more success with it. Here is how you can test it.

If you haven't already, download the newest version of the source code here: http://www.bluez.org/ Extract it and open a terminal in the bluez folder.

Then run the following in the terminal:

./configure --prefix=/usr    \
        --sysconfdir=/etc    \
        --localstatedir=/var \
        --enable-tools       \
        --disable-test       \
        --disable-systemd

I don't remember all packages you need to install but you can run this command and check why it fails, then install the package and rerun it till it works. Ask google if you don't know which package you need to install. Afterwards:

make

Now you can switch into tools folder from terminal and type ./btmgmt to see how to use it. You can also intall it to be able to use it by just typing "btmgmt" regardless of your location.

sudo /usr/bin/install -c tools/btmgmt /usr/bin/btmgmt

You need sudo rights to use it.