Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9418043
  • 博文数量: 1748
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 20070
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1748)

文章存档

2024年(24)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2016-09-20 12:10:45


点击(此处)折叠或打开

  1. //client.c gcc -lbluetooth

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <bluetooth/bluetooth.h>
  7. #include <bluetooth/rfcomm.h>

  8. int main( int argc , char **argv)
  9. {
  10.    struct sockaddr_rc addr={0};
  11.    int s,status;
  12.    char buf[1024];
  13.    char dst[] = "58:88:97:4F:2C:41";
  14.    time_t t;

  15.    // a l l o c a t e a s oc k e t
  16.    s=socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
  17.    if(s<0) {
  18.        perror("create socket error");
  19.        exit(1);
  20.    }

  21.    // s e t the conne c t ion parameter s (who to connect to )
  22.    addr.rc_family=AF_BLUETOOTH;
  23.    addr.rc_channel=(uint8_t)1;
  24.    str2ba(dst,&addr.rc_bdaddr);
  25.    // connect to s e r v e r
  26.    printf("connectting...\n");
  27.    status=connect(s,(struct sockaddr *)&addr,sizeof(addr));

  28.    // send a message
  29.    if(status==0) {
  30.      printf("scuess!\n");

  31.      do{
  32.         gets(buf);
  33.         time(&t);
  34.         sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","360ND", "00000000", t);
  35.    // sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","SEC", "sec12345", t);
  36.         printf("[%s]\n", buf);
  37.         status=write(s,buf,strlen(buf));
  38.         if(status<0) perror("uh oh");
  39.      }while(strcmp(buf,"goodbye")!=0);
  40.    }
  41.    else {
  42.      printf("Failed!\n");
  43.    }

  44.    close(s);
  45.    return 0;
  46. }


点击(此处)折叠或打开

  1. //client.c gcc -lbluetooth

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <bluetooth/bluetooth.h>
  7. #include <bluetooth/rfcomm.h>

  8. int main( int argc , char **argv)
  9. {
  10.    struct sockaddr_rc addr={0};
  11.    int s,status;
  12.    char buf[1024];
  13.    char dst[] = "58:88:97:4F:2C:41";
  14.    time_t t;

  15.    // a l l o c a t e a s oc k e t
  16.    s=socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
  17.    if(s<0) {
  18.        perror("create socket error");
  19.        exit(1);
  20.    }

  21.    // s e t the conne c t ion parameter s (who to connect to )
  22.    addr.rc_family=AF_BLUETOOTH;
  23.    addr.rc_channel=(uint8_t)1;
  24.    str2ba(dst,&addr.rc_bdaddr);
  25.    // connect to s e r v e r
  26.    printf("connectting...\n");
  27.    status=connect(s,(struct sockaddr *)&addr,sizeof(addr));

  28.    // send a message
  29.    if(status==0) {
  30.      printf("scuess!\n");

  31.      do{
  32.         gets(buf);
  33.         time(&t);
  34.         sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","360ND", "00000000", t);
  35.    // sprintf(buf, "{\"ssid\":\"%s\", \"psw\":\"%s\", \"timestamp\":%ld}","SEC", "sec12345", t);
  36.         printf("[%s]\n", buf);
  37.         status=write(s,buf,strlen(buf));
  38.         if(status<0) perror("uh oh");
  39.      }while(strcmp(buf,"goodbye")!=0);
  40.    }
  41.    else {
  42.      printf("Failed!\n");
  43.    }

  44.    close(s);
  45.    return 0;
  46. }

点击(此处)折叠或打开

  1. //服务器端应当执行 bluetoothctl -a < cmd.list
  2. //cmd list 的内容 power on + default-agent

  3. //server.c

  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <bluetooth/bluetooth.h>
  7. #include <bluetooth/rfcomm.h>


  8. static int s = -1, client = -1;

  9. void on_app_exit(void)
  10. {
  11.     if (client > 0) {
  12.         close(client);
  13.         client = -1;
  14.     }
  15.     if (s > 0)
  16.     {
  17.         close(s);
  18.         s = -1;
  19.     }

  20.     printf("Application Exited. \n");
  21. }

  22. int main (int argc,char **argv)
  23. {
  24.     system("bluetoothctl -a < bluetoothctl.conf");

  25.     struct sockaddr_rc loc_addr ={0},rem_addr={0};
  26.     char buf[1024] ={0};//,*addr;
  27.     int bytes_read,result;
  28.     int opt = sizeof(rem_addr);

  29.     atexit(on_app_exit);

  30.     printf("Creating socket...\n");
  31.     s =socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
  32.     if(s<0) {
  33.         perror("create socket error");
  34.         exit(1);
  35.     }
  36.     else {
  37.         printf("success!\n");
  38.     }

  39.     memset(&loc_addr, 0, sizeof(loc_addr));
  40.     loc_addr.rc_family=AF_BLUETOOTH;
  41.     loc_addr.rc_bdaddr= (bdaddr_t) {{0, 0, 0, 0, 0, 0}};
  42.     loc_addr.rc_channel=(uint8_t)1;

  43.     printf("Binding socket...\n");
  44.     result=bind(s,(struct sockaddr *)&loc_addr, sizeof(loc_addr));
  45.     if(result < 0) {
  46.         perror("bind socket error:");
  47.         system("reboot");
  48. }
  49.     else {
  50.         printf("success!\n");
  51.     }

  52.     printf("Listen... ");
  53.     result=listen(s,1);
  54.     if(result < 0) {
  55.         printf("error:%d\n:",result);
  56.         perror("listen error:");
  57.         exit(1);
  58.     }
  59.     else {
  60.         printf("requested!\n");
  61.     }

  62.     while (1) {
  63.         printf("Accepting...\n");
  64.         client= accept(s,(struct sockaddr *)&rem_addr, (socklen_t*)&opt);
  65.         if(client<0) {
  66.             perror("accept error");
  67.             continue;
  68.         }
  69.         else {
  70.             printf("OK!\n");
  71.         }
  72.         ba2str(&rem_addr.rc_bdaddr,buf);
  73.         fprintf(stderr,"accepted connection from %s \n",buf);
  74.         memset(buf,0,sizeof(buf));

  75.         while(1) {
  76.             bytes_read = recv(client,buf,sizeof(buf),0);
  77.             if(bytes_read > 0) {
  78.                 printf("received [%d] bytes.\n",bytes_read);
  79.                 memset(buf,0,bytes_read);
  80.             }
  81.         }
  82.     }

  83.     on_app_exit();
  84.     return 0 ;
  85. }


阅读(1832) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~