我使用本教程学习如何在raspberry pi:https://people.csail.mit.edu/albert/bluez-intro/c404.html上使用c中的蓝牙。
我目前正在尝试运行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;
}当我使用以下命令编译时:
gcc -o simplescan simplescan.c -lbluetooth我知道这个错误:
/usr/bin/ld: cannot find -lbluetooth
collect2: error: ld returned 1 exit status编辑:由于某些原因,当我试图编译时,错误已更改为:
simplescan.c:5:10: fatal error: bluetooth/bluetooth.h: No such file or directory
#include <bluetooth/bluetooth.h>我已经读到BlueZ是预装在Raspbian上的,我有最新的版本,但我似乎找不到蓝牙库文件夹或其中的任何.h文件。
本教程可能已经过时了,所以事情可能已经发生了变化。蓝牙库是和bluez一起预装的吗?如果是的话,我应该在哪里确认呢?
如果这个库不在我的RPI上,我应该从哪里得到它?
发布于 2020-10-08 10:15:00
该教程已经过时,因为BlueZ从那时起就一直在前进。早在2012年,就有主要发展项目将转向新的API
然后在2017年,来自布吕兹的不建议使用8种工具。
现在要使用的API是mgmt,它专注于系统级的功能,并记录在:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt中。
对于应用程序级别,您应该使用的是D总线API。这些文档分布在:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc中的许多文档中。
这里的C例子不多。源代码树中的示例用于D-Bus API,并且全部使用Python:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test。
查看bluetoothctl工具的代码可能会给您提供更好的示例。此代码可在以下站点获得:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client
https://stackoverflow.com/questions/64255087
复制相似问题