c - BlueZ 的替代品?

标签 c linux bluetooth bluez

一天中的大部分时间我都在尝试让 BlueZ 在我的 Ubuntu 桌面上运行。它就是行不通。我可以通过系统对话将设备与计算机成功配对,但 BlueZ 扫描却找不到任何东西。 BlueZ 有替代方案吗?如果设备已经配对,是否有办法在没有 BlueZ 的情况下操作蓝牙设备?

为了更好地衡量,这是我一直在运行的代码( http://people.csail.mit.edu/albert/bluez-intro/c404.html#simplescan.c ):

 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/hci.h>
 #include <bluetooth/hci_lib.h>

 int main(int argc, char **argv)
 {
     inquiry_info *ii = NULL;
     int max_rsp, num_rsp;
     int dev_id, sock, len, flags;
     int i;
     char addr[19] = { 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 = 255;
     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]");
         printf("%s  %s\n", addr, name);
     }

     free( ii );
     close( sock );
     return 0;
 }

最佳答案

我正在使用相同的代码。我进入手机设置并切换了允许其他设备看到我的手机的设置。然后再次运行该程序,它就会显示在列表中。希望这至少有助于验证代码是否正常工作。

关于c - BlueZ 的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329077/

相关文章:

c - 在 C 编程语言中,在 telnet session 中获取按键的正确方法是什么?

ios - 在 IOS 中通过蓝牙连接条码扫描器设备时,默认键盘不出现

c - 使用 Tab 移动到主应用程序窗口中的另一个编辑控件

c - gethostbyname() 返回具有负地址的结构

c++ - 如何递归查找最大数组元素的索引

linux - 使用 txt 文件在 shell 脚本中添加用户

C++:可以为一个 int 分配一个 char* 吗?

linux - xentop VBD_RD & VBD_WR 输出

Python 蓝牙密码/密码 Linux

android - 使用蓝牙 socket 处理方向变化的最佳方法