2017-07-11 95 views
1

私はBluetoothでデータを送受信するためにC言語の書き込みプログラムを試しています。 私は、サンプルプログラムと情報源が豊富なAlbert Huangの本を参照しています。 リンク:https://people.csail.mit.edu/albert/bluez-intro/index.htmlhttps://people.csail.mit.edu/albert/bluez-intro/x502.html)の例4.2では、ソケットプログラミングを使用したRFCOMM接続を示しています。また、BDアドレスの代わりにデバイスのBluetooth名を使用して接続する方法はありますか? ここでは、Bluetooth接続による通信のPINまたは認証の設定方法はわかりません。それを実装する方法はありますか?また、認証プロセスがないので、ソケット接続は十分に保護されていますか?CでのBluetoothプログラミング - 安全な接続とデータ転送

私の開発には、debian machineとBluezパッケージを使用しています。 私が達成しようとしているトポロジは、1つのマスタ/サーバがN(最大7つのブルートゥースプロトコル仕様)クライアントに接続されていて、埋め込まれたシンプルチップコンピュータであっても、またはクライアント)。

誰かが似たようなことをしたことがありますか、または参考文献である可能性があります。

私はSOを少ししか紹介していませんでしたが、すべてが本に記載されたものと同じものに戻ってくると私は指摘しました。私が探していたものに似たものは見つかりませんでした。

何か助けていただければ幸いです。

ありがとうございました。

Bluetooth Protocol Stack for Linux developed by Nokia Research Centerを:

+0

https:// stackoverflow。コム/質問/ 6494006/ – EsmaeelE

+0

https://electronics.stackexchange.com/questions/47273/is-bluetooth-方法・ツー・受信データ・イン・アンドロイド・プログラミング・使用・ブルートゥース・なしのペアを送信、および、通信可能な-なしの対 – EsmaeelE

+0

http://pages.iu.edu/~rwisman/c490/html/pythonandbluetooth.htm https://stackoverflow.com/questions/14820004/bluetooth-pairing-in-c-bluez- on-linux – EsmaeelE

答えて

2

は、あなたがチェックすることができ、いくつかのより多くのリソースがあります。

Using Bluetooth - DrDobbs

Bluetooth programming for Linux

+0

私は、DrDobbsの資料をBTに保管しなければならないと思います。私はこのウェブサイトが廃止されるかもしれないと聞きました。 – EsmaeelE

1

まず、すべてのBTデバイスを検索する必要があります。

2回目のチェックは、特定の名前に一致する資金提供済みデバイスの名前です。

trueの場合、サーバーコードへの送信を実行します。

はちょうど私だけのために Albert Huang Good BT tutorial

#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 loc_addr = { 0 }, rem_addr = { 0 }; 
    char buf[1024] = { 0 }; 
    int s, client, bytes_read; 
    socklen_t opt = sizeof(rem_addr); 

    // allocate socket 
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 

    // bind socket to port 1 of the first available 
    // local bluetooth adapter 
    loc_addr.rc_family = AF_BLUETOOTH; 
    loc_addr.rc_bdaddr = *BDADDR_ANY; 
    loc_addr.rc_channel = (uint8_t) 1; 
    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); 

    //get local address ? 
    //~ ba2str(&loc_addr.rc_bdaddr, buf); 
    //~ fprintf(stdout, "local %s\n", buf); 

    // put socket into listening mode 
    listen(s, 1); 

    // accept one connection 
    client = accept(s, (struct sockaddr *)&rem_addr, &opt); 


    ba2str(&rem_addr.rc_bdaddr, buf); 
    fprintf(stderr, "accepted connection from %s\n", buf); 


    memset(buf, 0, sizeof(buf)); 

    // read data from the client 
    bytes_read = read(client, buf, sizeof(buf)); 

    if(bytes_read > 0) { 
     printf("received [%s]\n", buf); 
    } 

    // close connection 
    close(client); 
    close(s); 
    return 0; 
} 

これらの2つのコードの作業に

rfcomm-server.c 

を使用するクライアント

#include <stdio.h> 
// POSIX sys lib: fork, pipe, I/O (read, write) 
#include <unistd.h> 
// superset of unistd, same 
#include <stdlib.h> 

//Bluetooth 
#include <bluetooth/bluetooth.h> 
#include <bluetooth/rfcomm.h> 
#include <bluetooth/hci.h> 
#include <bluetooth/hci_lib.h> 
#include <bluetooth/sdp.h> 
#include <bluetooth/sdp_lib.h> 
#include <bluetooth/sco.h> 

//socket 
#include <sys/socket.h> 


int main(int argc, char **argv) 
{ 
     int flag=0; 

    struct sockaddr_rc addrress = { 0 }; 
    int s, status; 

    char dest[18]="";// = "B0:10:41:3F:6E:80";//My destination address Laptop 
    char namelaptop[20]="ss"; 


    // allocate a socket 
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 
    ///create a socket 

    // set the connection parameters (who to connect to) 
    addrress.rc_family = AF_BLUETOOTH; 
    addrress.rc_channel = (uint8_t) 1;//must use sdp to work in real devices 
    //may this channel not ready 


    printf("Search for BT Devices...\n"); 

///search 

    inquiry_info *ii = NULL; 
    int max_rsp, num_rsp; 
    int dev_id, sock, len, flags; 
    int i; 

    char addr[18] = { 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 = 2; 
    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]"); 

     else{ 
     printf("\nFind #%d\n",i); 

     printf("Addr:%s Name:%s\n", addr, name); 

     int a=strcmp(name, namelaptop); 
     //printf("compare:%d\n",a); 

     if (!a){ // if name mached 
      str2ba(addr, &addrress.rc_bdaddr);//copy dest-->addr.rc_bdaddr  
      flag =1; 
     } 
     } 

    } 


/// End Search 



///Connect and send 

    if (flag==0){ 
     printf("Not find dest: %s\n",name); 
     exit(0); 
    } 

    // connect to server, throw socket s 
    status = connect(s, (struct sockaddr *)&addrress, sizeof(addrress)); 
    //successful, connect() returns 0. 

    printf("connection status: %d\n\n",status);//0 show OK 

    // send a message to server 
    if(status == 0) { 
     status = write(s, "hello!", 6); 
     if (status == 6){ 
      printf("Send data to server done\n"); 
     } 
    } 
    else 
     if(status < 0){ 
      perror("send message to server Failed\n"); 
    } 

    printf("Closing socket\n"); 

    ///close the socket 
    close(s); 

///End connect and send 


    free(ii); 
    close(sock); 

    return 0; 
} 

サーバー。

私のPCにはrfcomm-client.c、ノートパソコンにはrfcomm-server.cを実行しました。 私のラップトップのBTの名前は"ss"です。ハードコードされています。rfcomm-client.c

クライアントはハローマッサージを送信し、受信した場合はそれを表示します。

+0

お返事ありがとうございます。あなたは私の混乱の一部に答えましたが、私が心配している他の部分は接続の認証です。 あなたはあまりにもここでソケットプログラミングを使用していますか?ソケットプログラミングはブルートゥース接続を実装する唯一の方法ですか?私はそれが安全であり、認証がなく、私の主な関心事であるかどうかはわかりません。 – sachin

+0

はい私はBluetooth通信のためのソケットプログラミングを使用しています[Bluetoothソケットプログラミング]。これらのコードを実行する前に、Ubuntuの設定> bluetoothメニューで2つのシステムをペアにしました。コードではなく手動で認証が行われます。 – EsmaeelE

+0

はい、ブルートゥースプログラミングのための他のオプションがあります。ソケットプログラミングの代わりに特別なパッケージを使用する。 http://blog.kevindoran.co/bluetooth-programming-with-python-3/を見てください。しかし、私はこのパッケージもソケットを使うと思います。 – EsmaeelE

関連する問題