Bluetooth programming in C - Secure Connection and Data transfer

前端 未结 2 1469
灰色年华
灰色年华 2021-02-06 14:17

I am trying write program in C to send/receive data over bluetooth. I got a reference to a book by Albert Huang which has sample programs and also good source of information. Li

相关标签:
2条回答
  • 2021-02-06 14:39

    There are a few more resources which you could check:

    Bluetooth Protocol Stack for Linux developed by Nokia Research Center.

    Using Bluetooth - DrDobbs.

    Bluetooth programming for Linux.

    0 讨论(0)
  • 2021-02-06 14:41

    First you must do a search for all BT devices.

    Second check is the Name of any funded devices match to your specific name or not.

    If true perform send to server codes.

    Client

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

    Server

    just only use

    rfcomm-server.c
    

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

    These two code work for me.

    I run rfcomm-client.c in my PC, and rfcomm-server.c on A laptop. my laptop BT name is "ss" that i hard coded it on rfcomm-client.c

    client send a hello massage and server show it if receives.

    0 讨论(0)
提交回复
热议问题