linux用c语言编程蓝牙

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11408609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:46:34  来源:igfitidea点击:

linux Bluetooth programming in c

linuxbluetoothbluez

提问by Himanshu Pradhan

I am trying to run a basic code of c in linux[ubuntu] to search bluetooth device, but i am facing some problem.

我正在尝试在 linux[ubuntu] 中运行基本的 c 代码来搜索蓝牙设备,但我遇到了一些问题。

By using command sudo apt-get install bluez, to install required blueZ library it is saying that bluez is already newest version.

通过使用 command sudo apt-get install bluez,安装所需的 blueZ 库是说 bluez 已经是最新版本。

But error comes that not able to find bluetooth.hand other files in compiling C source code, with gcc -o simplescan simplescan.c -lbluetooth

但是错误来了,bluetooth.h在编译C源代码时无法找到和其他文件,用gcc -o simplescan simplescan.c -lbluetooth

Is there a complete library package, or do I have to download these header files?.

是否有完整的库包,或者我必须下载这些头文件?。

I am following this link

我正在关注此链接

回答by SamuelNLP

Maybe you didn't include the essential header.

也许你没有包含必要的标题。

here's an example of code to scan for bluetooth devices

这是扫描蓝牙设备的代码示例

#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;
}

to compile it on linux, just do

要在 linux 上编译它,只需执行

gcc -o simplescan simplescan.c -lbluetooth

EDIT:

编辑:

Original code can be found in here

原始代码可以在这里找到

回答by Sathish

As for i know there no packages for these headers. You have to download the following header files from internet.

至于我知道这些标题没有包。您必须从 Internet 下载以下头文件。

  1. bluetooth.h
  2. hci.h
  3. hci_lib.h
  1. bluetooth.h
  2. hci.h
  3. hci_lib.h

and create a directory called "bluetooth" under /usr/lib/in your host machine and copy the above headers to /usr/lib/bluetooth/. Then compile your program, it should work.

并在您的主机bluetooth下创建一个名为“ ”的目录/usr/lib/,并将上述标题复制到/usr/lib/bluetooth/. 然后编译你的程序,它应该可以工作。

Note: while compiling link with -lbluetooth

注意:在编译链接时 -lbluetooth

回答by Eric des Courtis

You need to install the linux-headers package. On Ubuntu or Debian this is done by doing this:

您需要安装 linux-headers 包。在 Ubuntu 或 Debian 上,这是通过执行以下操作来完成的:

sudo apt install linux-headers

回答by Viktor Bliakhovskyi

This solved my problem:

这解决了我的问题:

apt-get install libbluetooth-dev