我试图在linuxubuntu中运行一个基本的c代码来搜索蓝牙设备,但是我遇到了一些问题。
通过使用命令sudo apt-get install bluez来安装所需的blueZ库,这说明bluez已经是最新版本。
但是在用gcc -o simplescan simplescan.c -lbluetooth编译C源代码时,出现了找不到bluetooth.h等文件的问题
是否有一个完整的库包,或者我必须下载这些头文件?
我正在关注这个link
发布于 2013-02-11 20:39:25
也许你没有包含必要的头文件。
以下是扫描蓝牙设备的代码示例
#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;
}要在linux上编译它,只需执行以下操作
gcc -o simplescan simplescan.c -lbluetooth编辑:
原始代码可以在here中找到
发布于 2019-04-26 04:22:05
这解决了我的问题:
apt-get install libbluetooth-dev 发布于 2014-11-27 19:29:16
至于我知道没有这些头文件的包。你必须从互联网上下载以下头文件。
bluetooth.hhci.hhci_lib.h然后在主机的/usr/lib/下创建一个名为"bluetooth“的目录,并将上面的头文件复制到/usr/lib/bluetooth/中。然后编译你的程序,它应该可以工作。
注意:使用-lbluetooth编译链接时
https://stackoverflow.com/questions/11408609
复制相似问题