16

I am trying to run a basic code of c in linux[ubuntu] to search bluetooth device, but i am facing some problem.

By using command sudo apt-get install bluez, to install required blueZ library it is saying that bluez is already newest version.

But error comes that not able to find bluetooth.h and other files in compiling C source code, with gcc -o simplescan simplescan.c -lbluetooth

Is there a complete library package, or do I have to download these header files?.

I am following this link

SiHa
  • 7,830
  • 13
  • 34
  • 43
Himanshu Pradhan
  • 171
  • 1
  • 2
  • 5
  • I'am bot a C++ programmer but i think you need the sources. Bluetooth.h is a C++ header file. – René Höhle Jul 10 '12 at 07:44
  • 11
    Try `apt-get install libbluetooth-dev` . – Piotr Praszmo Jul 10 '12 at 08:04
  • apt-get is not working, can i download this package from https://launchpad.net/ubuntu/lucid/+source/bluez/4.60-0ubuntu8 – Himanshu Pradhan Jul 11 '12 at 08:53
  • I am not able to use apt-get command, because i am not connected to internet, but can if i download these library from outside and use in my ubuntu PC through pendrive , is it possible 1) Glib library, 2) Dbus library,3) Bluez 4) Bluez Utilities – Himanshu Pradhan Jul 12 '12 at 14:12
  • Here is Good Bluetooth link for future work at [DrDubbs](http://www.drdobbs.com/mobile/using-bluetooth/232500828). – EsmaeelE Oct 26 '17 at 12:09

4 Answers4

11

This solved my problem:

apt-get install libbluetooth-dev 
10

Maybe you didn't include the essential header.

here's an example of code to scan for bluetooth devices

#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;
}

to compile it on linux, just do

gcc -o simplescan simplescan.c -lbluetooth

EDIT:

Original code can be found in here

SamuelNLP
  • 4,038
  • 9
  • 59
  • 102
3

As for i know there no packages for these headers. You have to download the following header files from internet.

  1. bluetooth.h
  2. hci.h
  3. hci_lib.h

and create a directory called "bluetooth" under /usr/lib/ in your host machine and copy the above headers to /usr/lib/bluetooth/. Then compile your program, it should work.

Note: while compiling link with -lbluetooth

Sathish
  • 3,740
  • 1
  • 17
  • 28
1

You need to install the linux-headers package. On Ubuntu or Debian this is done by doing this:

sudo apt install linux-headers
Eric des Courtis
  • 5,135
  • 6
  • 24
  • 37