蓝牙C程序找不到库-lbluetooth

问题描述

我正在使用本教程来学习如何在树莓派上的c中使用蓝牙: https://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,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,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上,我应该从哪里获得它?

解决方法

该教程已经过时,因为BlueZ从那以后开始发展。早在2012年,major development to move to new APIs

然后在2017年8 tools were deprecated中来自Bluez。

现在要使用的API是mgmt API,该API专注于系统级功能,并记录在:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt

对于应用程序级别,应该使用D-Bus 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