2016-07-08 15 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がゼロに等しいということです任意のデバイスを見つける。

しかし、端末でコマンド$ 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 thanksリンクを更新しました –

+0

Okがダウンロードされましたが、私のコンパイルでヘッダーが見つかりません。 システムライブラリに含まれていなくてもパスが含まれていれば、必要な前提条件やインストールで何かが置かれる最新情報はありますか?私はbluezをインストールして、Pythonツールが動作します。 – peterk

0

NewAer SDKには、この例のポートを作ったパイ3者とiOSデバイス間のBLEスキャンやP2Pのコミュニケーションツールをサポートしています。 SDKはAndroidもサポートしていますが、OSがBLEモードを処理する方法が限られているため、サポートは限られています。

関連する問題