Linux Bluetooth programming in c

I am trying to run the c base code in linux [ubuntu] to search for a bluetooth device, but I ran into some problem.

Using the sudo apt-get install bluez to install the required blueZ library, it is said that bluez is the latest version.

But an error occurs: cannot find bluetooth.h and other files when compiling the C source code, gcc -o simplescan simplescan.c -lbluetooth

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

I follow this link

+8
source share
4 answers

You may not have included the main title.

Here is an example code for scanning 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:

The original code can be found in here.

+7
source

As I know, there are no packages for these headers. You need to download the following header files from the Internet.

  • bluetooth.h
  • hci.h
  • hci_lib.h

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

Note: when compiling links with -lbluetooth

+2
source

You need to install the linux-headers package. On Ubuntu or Debian, this is done as follows:

 sudo apt install linux-headers 
+1
source

This solved my problem:

 'apt-get install libbluetooth-dev ' 
0
source

All Articles