3

I am trying to implement the next code in a Raspberry Pi 3 to scan for BLE 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;
}

The problem is that num_rsp is equal to zero, that is, it is not finding any device.

However, if I use the command $ sudo hcitool lescanin the terminal, it finds all the devices available.

Can anyone point me in the right direction to troubleshoot this? Is there any other way to implement hcitool lescan as C++ code?

Thanks in advance.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
D. Mellow
  • 133
  • 1
  • 8
  • have a look at this [answer](http://stackoverflow.com/questions/30386577/c-c-ble-read-write-example-with-bluez) – bluepinto Jul 10 '16 at 13:58

2 Answers2

3

Using BlueZ, you can trigger a BLE scan using hci_le_set_scan_parameters and hci_le_set_scan_enable.

Here is an experiment written in C

if (hci_le_set_scan_parameters(current_hci_state.device_handle, 0x01, htobs(0x0010), htobs(0x0010), 0x00, 0x00, 1000) < 0)
{
    current_hci_state.has_error = 1;
    snprintf(current_hci_state.error_message, sizeof(current_hci_state.error_message), "Failed to set scan parameters: %s", strerror(errno));
    return;
}

if (hci_le_set_scan_enable(current_hci_state.device_handle, 0x01, 1, 1000) < 0)
{
    current_hci_state.has_error = 1;
    snprintf(current_hci_state.error_message, sizeof(current_hci_state.error_message), "Failed to enable scan: %s", strerror(errno));
    return;
}

I've made a port of this example in C++ here

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • Dead link to sample source ! – peterk Jan 25 '18 at 16:25
  • Ok downloaded but my compile cant find the headers. Any recent info out there on the necessary pre-requisites or where the install puts things if not in the system library and include paths? I've installed bluez and the python tools work. – peterk Jan 31 '18 at 17:04
  • 1
    Ok - seems I have to install libbluetooth-dev for the headers "sudo apt-get install libbluetooth-dev" – peterk Jan 31 '18 at 17:14
  • I compiled and linked C++ sample, The bluetooth service is running. hcitools scanner displays a number of LE devices. Running the sample program displays "Scanning..." and sits there waiting. – peterk Feb 01 '18 at 19:15
  • 2
    It seems LE scan requires root privileges, when I run the program as root it works fine. Note though none of the calls where a return value is checked return an error when running as non-root. – peterk Feb 01 '18 at 19:49
0

The NewAer SDK supports BLE scanning and P2P communcation between Pi 3's and iOS devices. The SDK also supports Android as well, but has limited support due to the way that OS handles BLE modes.