相关文章推荐
酷酷的烈马  ·  java 调用 ...·  1 周前    · 
无邪的松树  ·  飞猪 - 开放平台·  1 年前    · 
旅途中的羊肉串  ·  Leyi Wei ...·  1 年前    · 
痛苦的苹果  ·  Android ...·  1 年前    · 
玩命的甜瓜  ·  java获取文件路径 - 简书·  1 年前    · 
千杯不醉的瀑布  ·  getclass().getclassloa ...·  1 年前    · 
addr.hci_family = AF_BLUETOOTH; hci_dev = hci_devid( "XX:XX:XX:XX:XX:XX" ); // Replace with actual MAC of device bind(sock, (struct sockaddr *)&addr, sizeof(addr)

而对于接收数据,我正在做

recvmsg(sock, &msg, 0))

但我无法收到味精。

在安卓系统中,有一个串行蓝牙终端,它可以连接到蓝牙LE设备并通过蓝牙LE发送/接收数据。在这个应用程序中,我正在接收来自微控制器的数据。

#include <sys/types.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
struct dump_hdr 
  __u16 len;
  __u8  in;
  __u8  pad;
  __u32 ts_sec;
  __u32   ts_usec;
} __attribute__ ((packed));
#define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
struct frame 
  void  *data;
  int   data_len;
  void  *ptr;
  int   len;
  int   in;
  int   handle;
  long  flags;
  struct timeval ts;
int main(void)
    printf("Starting...");
    struct sockaddr_hci addr;
    struct hci_filter filter;
    int sock, one = 1;
    char packet[HCI_MAX_FRAME_SIZE];
    struct cmsghdr *cmsg;
    struct msghdr msg;
    struct iovec  iv;
    struct dump_hdr *dh;
    struct frame frm;
    char *buf, *ctrl;
    if((sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0)
         perror("socket");
         exit(1);
    if(setsockopt(sock, SOL_HCI, HCI_DATA_DIR, &one, sizeof(one)) < 0) 
        perror("Can't enable data direction info");
        exit(1);
    if(setsockopt(sock, SOL_HCI, HCI_TIME_STAMP, &one, sizeof(one)) < 0) 
        perror("Can't enable time stamp");
        exit(1);
    hci_filter_clear(&filter);
    hci_filter_all_ptypes(&filter);
    hci_filter_all_events(&filter);
   if(setsockopt(sock, SOL_HCI, HCI_FILTER, &filter, sizeof(filter)) < 0) 
      perror("Can't set HCI filter");
      exit(1);
   addr.hci_family = AF_BLUETOOTH;
   addr.hci_dev = hci_devid( "XX:XX:XX:XX:XX:XX" );//eplace with actual MAC of device
   if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) 
       perror("bind");
       exit(1);
   if (!(buf = malloc(DUMP_HDR_SIZE))) 
       perror("Can't allocate data buffer");
       exit(1);
   dh = (void *) buf;
   frm.data = buf + DUMP_HDR_SIZE;
   if (!(ctrl = malloc(100))) 
       perror("Can't allocate control buffer");
       exit(1);
   memset(&msg, 0, sizeof(msg));
   while (1) 
       printf(".");
      iv.iov_base = frm.data;
      iv.iov_len  = sizeof(frm.data);//snap_len;
      msg.msg_iov = &iv;
      msg.msg_iovlen = 1;
      msg.msg_control = ctrl;
      msg.msg_controllen = 100;
      if ((frm.data_len = recvmsg(sock, &msg, 0)) < 0) 
          perror("Receive failed");
          exit(1);
       /* Process control message */
       frm.in = 0;
       cmsg = CMSG_FIRSTHDR(&msg);
       while (cmsg) 
           printf("%s\r\n", cmsg);
           switch (cmsg->cmsg_type) 
               case HCI_CMSG_DIR:
                 frm.in = *((int *)CMSG_DATA(cmsg));
                 break;
               case HCI_CMSG_TSTAMP:
                 frm.ts = *((struct timeval *)CMSG_DATA(cmsg));
               break;
         cmsg = CMSG_NXTHDR(&msg, cmsg);
       frm.ptr = frm.data;
       frm.len = frm.data_len;
       /* Parse and print */
       hci_dump(&frm);
   close(sock);
   return 0;
void hci_dump(struct frame *frm)
   __u8 type = *(__u8 *)frm->ptr; 
   frm->ptr++; frm->len--;
   if(type == HCI_ACLDATA_PKT)
       hci_acl_hdr *hdr = (void *) frm->ptr;
      __u16 handle = btohs(hdr->handle);
      __u16 dlen = btohs(hdr->dlen);
      __u8 flags = acl_flags(handle);
      printf("ACL data: handle 0x%4.4x flags 0x%2.2x dlen %d\n",
      acl_handle(handle), flags, dlen);
    
5 个评论
看起来你在那个代码块的最后一行缺少一个);。我想这是一个复制错误,但它仍然缺少
请不要使用像BLE这样令人困惑的缩略语。在计算机领域,自古以来,BLE就意味着 "比特长度编码"。
@Lundin BLE是一个非常常见的术语,与比特长度编码相比,蓝牙低能耗更被广泛接受和认可。
@ChuckClaunch 只是因为有些人a决定发明另一个TLA。虽然我有点同意,现在蓝牙的BLE是一个比两年前我写这篇评论时更常见的术语。
@Lundin BLE这个词已经存在了至少十年了。 我个人开发BLE相关的软件至少也有五六年了。 只是在一个与蓝牙低能耗相关的问题上,把BLE作为一个混乱的术语来使用似乎很愚蠢。
c
linux
bluetooth-lowenergy
SG92
SG92
发布于 2018-08-17
1 个回答
Thomas Jager
Thomas Jager
发布于 2018-08-17
0 人赞同

You need to use the result of hci_devid.

hci_dev = hci_devid( "XX:XX:XX:XX:XX:XX" );

should become