2016-07-08 7 views
2

내가 BLE 장치를 검색하는 라즈베리 파이 3에서 다음 코드를 구현하기 위해 노력하고 있습니다 : 그것은 아니다,라즈베리 파이 3 BLE 스캔

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

문제는 num_rsp이 0 일이라는 것이다 모든 장치를 찾는 것.

그러나 터미널에서 $ sudo hcitool lescan 명령을 사용하면 사용 가능한 모든 장치가 검색됩니다.

누구나 올바른 방향으로이 문제를 해결할 수 있습니까? 다른 방법으로 hcitool lescan을 C++ 코드로 구현할 수 있습니까?

미리 감사드립니다.

+0

이 봐 [답변] (http://stackoverflow.com/questions/30386577/c-c-ble-read-write-example-with-bluez) – bluepinto

답변

0

BlueZ를 사용하면 hci_le_set_scan_parametershci_le_set_scan_enable을 사용하여 BLE 스캔을 트리거 할 수 있습니다.

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

나는 C에서이 예제의 포트 ++ here

+0

샘플 소스에 대한 데드 링크! – peterk

+0

@peterk 덕분에 나는 방금 링크를 업데이트했습니다 –

+0

Ok 다운로드했지만 컴파일러가 헤더를 찾을 수 없습니다. 시스템 라이브러리 및 경로가 포함되어 있지 않은 경우 설치에 필요한 사전 요구 사항이나 설치 위치에 대한 최신 정보가 있습니까? bluez를 설치했고 파이썬 도구가 작동합니다. – peterk

0

NewAer SDK는 파이 3의 및 iOS 기기 사이의 BLE 검사 및 P2P의 통신의 경우 지원을했습니다. SDK는 Android도 지원하지만 OS가 BLE 모드를 처리하는 방식으로 인해 제한적으로 지원됩니다.