2017-07-11 51 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