User Tools

Site Tools


tutorials:common:development:bluez_programming

Bluetooth C Programming Quick Start

About this Article

Abstract

This is a brief C programming quick start for Linux bluetooth novice. We mainly exploit the bluez library to implement bluetooth functions like scanning, reading from and writing to target bluetooth devices. Bluetooth programming is complicated and difficult to some extent, because the bluetooth standard itself is too complicated, so I am not able to cover too many stuff in this short tutorial.

Prerequisite

Install the necessary tools and library first, it is easy in Cubieboard Ubuntu/Debian Linux.

apt-get install bluez libbluetooth-dev

And you need do some coding job from now on.

Scan example

Scan the bluetooth nearby. Notice that only slave devices could be scanned.

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

Read and Write example

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
 
int main(int argc, char **argv)
{
    struct sockaddr_rc addr = { 0 };
    int s, status, len=0;
    char dest[18] = "00:12:01:31:01:13";
    char buf[256];
    // allocate a socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
 
    // set the connection parameters (who to connect to)
    addr.rc_family = AF_BLUETOOTH;
    addr.rc_channel = (uint8_t) 1;
    str2ba( dest, &addr.rc_bdaddr );
 
    // connect to server
    status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
 
 
    if(status){
        printf(" failed to connect the device!\n");
        return -1;
    }
 
 
    do{
        len = read(s, buf, sizeof buf);
 
     if( len>0 ) {
         buf[len]=0;
         printf("%s\n",buf);
         write(s, buf, strlen(buf));
     }
    }while(len>0);
 
    close(s);
    return 0;
}

Compile the program

$ gcc bluez_demo.c -lbluetooth -o bluez_demo

Preference

tutorials/common/development/bluez_programming.txt · Last modified: 2013/12/23 14:50 (external edit)