CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 8358|回复: 0

CB4获取小米蓝牙温湿度计2的数据

[复制链接]
发表于 2019-12-24 12:38:19 | 显示全部楼层 |阅读模式
2代传数据方式和1代稍有变化。
首先安装bluez、bluetooth等软件包,为了省事儿,可以直接装上编译bluez需要的所有包:sudo apt-get build-dep bluez
然后根据CB4用户文档,确保蓝牙正常打开。
编译执行就ok了。
  1. #include <stdlib.h>

  2. #include <bluetooth/bluetooth.h>
  3. #include <bluetooth/hci.h>
  4. #include <bluetooth/hci_lib.h>

  5. int main(int argc, char *argv[]) {
  6.     int err, dd, i, result;
  7.     bdaddr_t bdaddr;
  8.     uint16_t interval, latency, max_ce_length, max_interval, min_ce_length;
  9.     uint16_t min_interval, supervision_timeout, window, handle;
  10.     uint8_t initiator_filter, own_bdaddr_type, peer_bdaddr_type;
  11.     char buf[16];
  12.     struct hci_filter flt;
  13.     peer_bdaddr_type = LE_PUBLIC_ADDRESS;
  14.     initiator_filter = 0; /* Use peer address */
  15.     dd = hci_open_dev(0);
  16.     if (dd < 0) {
  17.         perror("Could not open device");
  18.         exit(1);
  19.     }

  20.     memset(&bdaddr, 0, sizeof(bdaddr_t));
  21.     if (argv[1])
  22.         str2ba(argv[1], &bdaddr);
  23.     interval = htobs(0x0004);
  24.     window = htobs(0x0004);
  25.     own_bdaddr_type = 0x00;
  26.     min_interval = htobs(0x000F);
  27.     max_interval = htobs(0x000F);
  28.     latency = htobs(0x0000);
  29.     supervision_timeout = htobs(0x0C80);
  30.     min_ce_length = htobs(0x0001);
  31.     max_ce_length = htobs(0x0001);

  32.     err = hci_le_create_conn(dd, interval, window, initiator_filter,
  33.             peer_bdaddr_type, bdaddr, own_bdaddr_type, min_interval,
  34.             max_interval, latency, supervision_timeout,
  35.             min_ce_length, max_ce_length, &handle, 25000);
  36.     if (err < 0) {
  37.         perror("Could not create connection");
  38.         exit(1);
  39.     }

  40.     hci_filter_clear(&flt);
  41.     hci_filter_set_ptype(HCI_ACLDATA_PKT, &flt);

  42.     if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
  43.         perror("Failed to set HCI filter");
  44.         close(dd);
  45.         return -1;
  46.     }

  47.     for(i = 0; i<10; i++) {
  48.         memset(&buf, 0, sizeof(buf));
  49.         result = read(dd, &buf, sizeof(buf));
  50.         if(result == 16 && buf[10] == 0x36) {
  51.             printf("%d;%d\n", buf[12] | buf[13] << 8, buf[14]);
  52.             break;
  53.         }
  54.     }
  55.    
  56.     err = hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
  57.     if (err < 0) {
  58.         perror("Could not disconnect");
  59.         exit(1);
  60.     }

  61.     hci_close_dev(dd);
  62. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-3-29 10:19 , Processed in 0.029460 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部