Bluetooth pairing in C blueZ on Linux Bluetooth pairing in C blueZ on Linux linux linux

Bluetooth pairing in C blueZ on Linux


Authentication code from hcitool (original source code can see at http://git.kernel.org/cgit/bluetooth/bluez.git/tree/tools/hcitool.c)

/* Request authentication */static void cmd_auth(int dev_id, int argc, char **argv){        struct hci_conn_info_req *cr;        bdaddr_t bdaddr;        int opt, dd;        for_each_opt(opt, auth_options, NULL) {            switch (opt) {            default:                printf("%s", auth_help);                return;            }        }        helper_arg(1, 1, &argc, &argv, auth_help);        str2ba(argv[0], &bdaddr);        if (dev_id < 0) {            dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);            if (dev_id < 0) {                fprintf(stderr, "Not connected.\n");                exit(1);            }        }        dd = hci_open_dev(dev_id);        if (dd < 0) {            perror("HCI device open failed");            exit(1);        }        cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));        if (!cr) {            perror("Can't allocate memory");            exit(1);        }        bacpy(&cr->bdaddr, &bdaddr);        cr->type = ACL_LINK;        if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {            perror("Get connection info failed");            exit(1);        }        if (hci_authenticate_link(dd, htobs(cr->conn_info->handle), 25000) < 0) {            perror("HCI authentication request failed");            exit(1);        }        free(cr);        hci_close_dev(dd);}

And setting up PIN

/* Activate encryption */static void cmd_enc(int dev_id, int argc, char **argv){    struct hci_conn_info_req *cr;    bdaddr_t bdaddr;    uint8_t encrypt;    int opt, dd;    for_each_opt(opt, enc_options, NULL) {        switch (opt) {        default:            printf("%s", enc_help);            return;        }    }    helper_arg(1, 2, &argc, &argv, enc_help);    str2ba(argv[0], &bdaddr);    if (dev_id < 0) {        dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);        if (dev_id < 0) {            fprintf(stderr, "Not connected.\n");            exit(1);        }    }    dd = hci_open_dev(dev_id);    if (dd < 0) {        perror("HCI device open failed");        exit(1);    }    cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));    if (!cr) {        perror("Can't allocate memory");        exit(1);    }    bacpy(&cr->bdaddr, &bdaddr);    cr->type = ACL_LINK;    if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {        perror("Get connection info failed");        exit(1);    }    encrypt = (argc > 1) ? atoi(argv[1]) : 1;    if (hci_encrypt_link(dd, htobs(cr->conn_info->handle), encrypt, 25000) < 0) {        perror("HCI set encryption request failed");        exit(1);    }    free(cr);    hci_close_dev(dd);}


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.


This dbus command can be used to initiate pairing

 dbus-send --system --print-reply --dest=org.bluez /org/bluez/1301/hci0 org.bluez.Adapter.CreatePairedDevice string:"XX:XX:XX:XX:XX:XX" objpath:/org/bluez/agent_1317 string:"NoInputNoOutput"

Here 1301 is the process id of bluetoothd

/org/bluez/agent_1317 is the bluetooth pairing agent.The bluezagent that comes as agent.c in bluez/test can be used for this purpose.