alaskalinuxuser@alaskalinuxuser-OptiPlex-7010:~/Documents/c++/bluetooth/cbluez$ ./simplescan
FC:58:FA:08:72:E4 CANZ
alaskalinuxuser@alaskalinuxuser-OptiPlex-7010:~/Documents/c++/bluetooth/cbluez$ ./simplescan
FC:58:FA:08:72:E4 CANZ
alaskalinuxuser@alaskalinuxuser-OptiPlex-7010:~/Documents/c++/bluetooth/cbluez$ ./simplescan
00:00:00:00:5A:AD bacon
FC:58:FA:08:72:E4 CANZ
alaskalinuxuser@alaskalinuxuser-OptiPlex-7010:~/Documents/c++/bluetooth/cbluez$

That was the terminal output from the program “simplescan”, which I got from an instructive how to guide. The program itself is rather straight forward, like so:

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

But I did run into an error when I executed it. Turns out, I needed to install a few libraries:

# apt-get install libbluetooth-dev

Once that was installed, the #include files could then be found. How people get so smart to put this stuff together is beyond me! But I was rather impressed. My goal is to write an app for my Ubuntu Touch phone that will control my home-made Bluetooth auto start that I put together. I already created the Android app, but now I need something for my UT phone.

simplescan

The problem is, I don’t know anything about Bluetooth from a C or C++ programming standpoint, so I am trying to learn about it by working through various guides and simple tutorials like the one above. I’m hoping to learn a lot, and really looking forward to the adventure!

Linux – keep it simple.

Leave a Reply

Your email address will not be published.