JGoss
JGoss

Reputation: 125

Bluetooth C program cannot find library -lbluetooth

I am using this tutorial to learn how to use bluetooth in c on my raspberry pi: https://people.csail.mit.edu/albert/bluez-intro/c404.html

I am currently trying to get running simplescan.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

int main(int argc, char **argv)
{
    inquiry_info *ii = NULL;
    int max_rsp, num_rsp;
    int dev_id, sock, len, flags;
    int i;
    char addr[19] = { 0 };
    char name[248] = { 0 };

    dev_id = hci_get_route(NULL);
    sock = hci_open_dev( dev_id );
    if (dev_id < 0 || sock < 0) {
        perror("opening socket");
        exit(1);
    }

    len  = 8;
    max_rsp = 255;
    flags = IREQ_CACHE_FLUSH;
    ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
    
    num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
    if( num_rsp < 0 ) perror("hci_inquiry");

    for (i = 0; i < num_rsp; i++) {
        ba2str(&(ii+i)->bdaddr, addr);
        memset(name, 0, sizeof(name));
        if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), 
            name, 0) < 0)
        strcpy(name, "[unknown]");
        printf("%s  %s\n", addr, name);
    }

    free( ii );
    close( sock );
    return 0;
}

When I compile using this command:

gcc -o simplescan simplescan.c -lbluetooth

I get this error:

/usr/bin/ld: cannot find -lbluetooth
collect2: error: ld returned 1 exit status

EDIT: For some reason now when I try to compile the error has changed to this:

simplescan.c:5:10: fatal error: bluetooth/bluetooth.h: No such file or directory
 #include <bluetooth/bluetooth.h>

I have read that BlueZ comes pre-installed on Raspbian, and I have the newest version, but I can't seem to find the bluetooth library folder or any of the .h files in it.

This tutorial might be old so things might have moved around. Does the bluetooth library come preinstalled along with bluez? If so, where should I look to confirm?

If this library is not on my RPI where should I get it from?

Upvotes: 0

Views: 628

Answers (1)

ukBaz
ukBaz

Reputation: 7994

That tutorial is out of date as BlueZ has moved on since then. Back in 2012 there was major development to move to new APIs

Then in 2017 8 tools were deprecated from Bluez.

The API's to use now are the mgmt API which focused on system level functionality and is documented at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt

For application level, it is the D-Bus API's that you should be using. These are spread across a number of documents in: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc

There are not many C examples around. The examples in the source tree are for the D-Bus API and all use Python: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test

Looking at the code for the bluetoothctl tool might give you better examples. This code is available at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client

Upvotes: 2

Related Questions