Working with some bluetooth coding :)

It’s been quite some time since I started poking with bluetooth coding, but didn’t understand it much. But it was quite some moment this afternoon. I was trying to study the rfcomm source code, and finally could able to find out the part of the code responsible for the “bind” option for rfcomm. Had a code already for scanning for nearby bluetooth devices. Edited some of theirs codes, added some lines, and came up with a code in c which scans for nearby bluetooth devices, displays them, asks the users to choose one of the devices to bind, and accordingly binds that device. But for that, the device must be already paired up with the host computer. Here’s the code:

include

#include
#include
#include
#include
#include
#include
#include
#include

include

#include
#include
#include

int btscan (char baddrlist[][19], char namelist[][248] )
{
inquiry
info *ii = NULL;
int maxrsp, numrsp;
int dev_id, sock, len, flags;
int i, j;
char addr[19] = {0};
char name[248] = {0};

struct sockaddrrc locaddr = { 0 }, remaddr = { 0 };
char buf[1024] = {0};
int s, client, bytes
read;
socklent opt = sizeof (remaddr);

devid = hcigetroute(NULL);
sock = hci
opendev( devid);
if (dev_id < 0 || sock < 0){
perror(“opening socket”);
exit(1);
}

len = 8;
maxrsp = 255;
flags = IREQ
CACHEFLUSH;
ii = (inquiry
info*)malloc(maxrsp * sizeof(inquiryinfo));

numrsp = hciinquiry(devid, len, maxrsp, NULL, &ii, flags);
if( numrsp < 0 ) perror(“hciinquiry”);

for (i = 0; i < numrsp; i++) {
ba2str(&(ii+i)->bdaddr, addr);
memset(name, 0, sizeof(name));
if (hci
readremotename(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0)
strcpy(name, “[unknown]”);
strcpy(namelist[i], name); strcpy(baddrlist[i], addr);
printf(“%d. %s %sn”, i+1, addr, name);
}

free(ii);
close( sock );
return i;

}

static int createdev(int ctl, int dev, uint32t flags, bdaddrt *bdaddr, char *strba)
{
struct rfcomm
dev_req req;
int err;

memset(&req, 0, sizeof(req));
req.dev_id = dev;
req.flags = flags;
bacpy(&req.src, bdaddr);

str2ba(strba, &req.dst);

req.channel = 1;

err = ioctl(ctl, RFCOMMCREATEDEV, &req);
if (err == EOPNOTSUPP)
fprintf(stderr, “RFCOMM TTY support not availablen”);
else if (err < 0)
perror(“Can’t create device”);

return err;
}

int bindbt(char *strba)
{
bdaddr
t bdaddr;
int i, opt, ctl, devid, showall = 0, err;

bacpy(&bdaddr, BDADDRANY);
ctl = socket(AF
BLUETOOTH, SOCKRAW, BTPROTORFCOMM);
if (ctl < 0) {
perror(“Can’t open RFCOMM control socket”);
exit(1);
}

dev_id = atoi (“/dev/rfcomm0″);

err = createdev(ctl, devid, 0, &bdaddr, strba);
close(ctl);

return err;
}

int main (int argc, char *argv[])
{
int count, choice, err;
char namelist[10][248], baddrlist [10][19], strba[19];

count = btscan(baddrlist, name_list);
if (count > 0){
x:
printf(“Enter the serial number of the device that you want to connect to : “);
scanf (“%d”, &choice);

if (choice > count){
printf (“Enter proper choice…n”);
goto x;
}
else{
strcpy(strba, baddrlist[choice-1]);
err = bind
bt(strba);
if (err < 0) printf (“Might be you don’t have sufficient privileges, try running it as rootn”);
}
}
return 0;
}

To compile this code :

gcc foo.c -lbluetooth

Run the a.out file as root because the rfcomm binding operation needs root privileges.

Any suggestions or ways to improve the code are welcome.

Check the next post: How to make a custom Fedora Linux Distribution »

Share on:
comments powered by Disqus