bluetoothctl demo

扫码查看
点击(此处)折叠或打开//client.c gcc -lbluetooth#include stdio.h>#include stdlib.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 addr={0};   int s,status;   char buf[1024];   char dst[] = "58:88:97:4F:2C:41";   time_t t;   // a l l o c a t e a s oc k e t   s=socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);   if(s0) {       perror("create socket error");       exit(1);   }   // s e t the conne c t ion parameter s (who to connect to )   addr.rc_family=AF_BLUETOOTH;   addr.rc_channel=(uint8_t)1;   str2ba(dst,&addr.rc_bdaddr);   // connect to s e r v e r   printf("connectting...\n");   status=connect(s,(struct sockaddr *)&addr,sizeof(addr));   // send a message   if(status==0) {     printf("scuess!\n");     do{        gets(buf);        time(&t);        sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","360ND", "00000000", t);   // sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","SEC", "sec12345", t);        printf("[%s]\n", buf);        status=write(s,buf,strlen(buf));        if(status0) perror("uh oh");     }while(strcmp(buf,"goodbye")!=0);   }   else {     printf("Failed!\n");   }   close(s);   return 0;}点击(此处)折叠或打开//client.c gcc -lbluetooth#include stdio.h>#include stdlib.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 addr={0};   int s,status;   char buf[1024];   char dst[] = "58:88:97:4F:2C:41";   time_t t;   // a l l o c a t e a s oc k e t   s=socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);   if(s0) {       perror("create socket error");       exit(1);   }   // s e t the conne c t ion parameter s (who to connect to )   addr.rc_family=AF_BLUETOOTH;   addr.rc_channel=(uint8_t)1;   str2ba(dst,&addr.rc_bdaddr);   // connect to s e r v e r   printf("connectting...\n");   status=connect(s,(struct sockaddr *)&addr,sizeof(addr));   // send a message   if(status==0) {     printf("scuess!\n");     do{        gets(buf);        time(&t);        sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","360ND", "00000000", t);   // sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","SEC", "sec12345", t);        printf("[%s]\n", buf);        status=write(s,buf,strlen(buf));        if(status0) perror("uh oh");     }while(strcmp(buf,"goodbye")!=0);   }   else {     printf("Failed!\n");   }   close(s);   return 0;}点击(此处)折叠或打开//服务器端应当执行 bluetoothctl -a cmd.list//cmd list 的内容 power on + default-agent//server.c#include sys/types.h>#include sys/socket.h>#include bluetooth/bluetooth.h>#include bluetooth/rfcomm.h>static int s = -1, client = -1;void on_app_exit(void){    if (client > 0) {        close(client);        client = -1;    }    if (s > 0)    {        close(s);        s = -1;    }    printf("Application Exited. \n");}int main (int argc,char **argv){    system("bluetoothctl -a );    struct sockaddr_rc loc_addr ={0},rem_addr={0};    char buf[1024] ={0};//,*addr;    int bytes_read,result;    int opt = sizeof(rem_addr);    atexit(on_app_exit);    printf("Creating socket...\n");    s =socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);    if(s0) {        perror("create socket error");        exit(1);    }    else {        printf("success!\n");    }    memset(&loc_addr, 0, sizeof(loc_addr));    loc_addr.rc_family=AF_BLUETOOTH;    loc_addr.rc_bdaddr= (bdaddr_t) {{0, 0, 0, 0, 0, 0}};    loc_addr.rc_channel=(uint8_t)1;    printf("Binding socket...\n");    result=bind(s,(struct sockaddr *)&loc_addr, sizeof(loc_addr));    if(result 0) {        perror("bind socket error:");        system("reboot");}    else {        printf("success!\n");    }    printf("Listen... ");    result=listen(s,1);    if(result 0) {        printf("error:%d\n:",result);        perror("listen error:");        exit(1);    }    else {        printf("requested!\n");    }    while (1) {        printf("Accepting...\n");        client= accept(s,(struct sockaddr *)&rem_addr, (socklen_t*)&opt);        if(client0) {            perror("accept error");            continue;        }        else {            printf("OK!\n");        }        ba2str(&rem_addr.rc_bdaddr,buf);        fprintf(stderr,"accepted connection from %s \n",buf);        memset(buf,0,sizeof(buf));        while(1) {            bytes_read = recv(client,buf,sizeof(buf),0);            if(bytes_read > 0) {                printf("received [%d] bytes.\n",bytes_read);                memset(buf,0,bytes_read);            }        }    }    on_app_exit();    return 0 ;}
10-25 20:56
查看更多