2017-07-11 52 views
1

我正在嘗試使用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地址進行連接嗎? 在這裏我沒有看到如何設置PIN或認證的藍牙連接通信。有沒有辦法來實現它。由於沒有身份驗證過程,套接字連接是否足夠安全?C中的藍牙編程 - 安全連接和數據傳輸

我使用debian機器和Bluez軟件包進行開發。 我試圖實現的拓撲結構是 - 1個連接到N(根據藍牙協議規範最多7個)的主/服務器客戶端,它可以是任何嵌入式簡單芯片計算機,並且應該能夠通信,直到連接被任一個或客戶端)。

有沒有人做過類似的事情或可能是任何引用/ s。

我在SO上發現了很少的參考文獻,但所有內容都指向本書中描述的相同內容。我無法找到與我所尋找的內容類似的內容。

任何幫助表示讚賞。

感謝您的時間和幫助。

+0

https:// stackoverflow。com/questions/6494006/how-to-send-and-receive-data-in-android-programming-using-bluetooth-without-pair – 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

回答

1

首先,您必須搜索所有BT設備。

第二個檢查是任何資助設備的名稱與您的具體名稱是否匹配。

如果爲true,則發送到服務器代碼。

客戶

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

服務器

單單隻出現在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; 
} 

這兩個代碼工作中使用

rfcomm-server.c 

我。

我在我的電腦中運行rfcomm-client.c,在筆記本電腦上運行rfcomm-server.c。 我的筆記本電腦的BT名稱是"ss",我硬編碼它rfcomm-client.c

客戶端發送hello消息和服務器顯示它,如果收到。

+0

感謝您的回覆。你回答了我的一部分困惑,但我擔心的另一部分是連接驗證。 你也在這裏使用套接字編程嗎?套接字編程是實現藍牙連接的唯一方法嗎?我不知道它是否安全,因爲沒有身份驗證,這是我主要關心的問題。 – sachin

+0

是的,我只是使用套接字編程進行藍牙通信[藍牙套接字編程]。在運行這些代碼之前,我將兩個系統與Ubuntu設置>藍牙菜單配對。不通過代碼手動進行認證。 – EsmaeelE

+0

是的,我們有其他的藍牙編程選項。使用特殊的包而不是套接字編程。請參閱http://blog.kevindoran.co/bluetooth-programming-with-python-3/,但我認爲這個軟件包也使用套接字。 – EsmaeelE