0

I m trying to compile the follwing code in Codeblocks (copied from https://people.csail.mit.edu/albert/bluez-intro/c404.html). In the given instructions it said to compile invoke gcc and link against libbluetooth "# gcc -o simplescan simplescan.c -lbluetooth". If the cause is that likage with libbluetooth how could I possibly do that in Codblocks.

#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;
}
bora
  • 11
  • 3
  • You need to add the library to your compile command, e.g. `gcc -Wall -Wextra -o your_exe_name your_c_file.c -lbluetooth`. (note: the addition of `-lbluetooth`, and you drop the `"lib"` from the front of `libbluetooth.so` so it is just `-l` (ell) `name minus 'lib'` for `-lbluetooth`. – David C. Rankin Jun 07 '18 at 15:29
  • with gcc that works fine, how to do that in codeblocks ? just include ? – bora Jun 08 '18 at 07:38
  • Where is `bluetooth.h` located? You will have to include the include search-path to it (e.g. `-Ic:\path\to\dir\with\bluetooth.h`). You will add that as another command line option. I doubt it will install to the normal MinGW `include` directory (it might -- you just have to find it) The file itself is usually part of the `bluez` package (or the development `bluez` package) I don't have it installed on windows -- so you will also need to make sure you have the package providing `bluetooth.h` installed (it's not part of the C library) – David C. Rankin Jun 08 '18 at 07:47
  • Thancks for your help – bora Jun 08 '18 at 08:34

0 Answers0