【问题标题】:linux Bluetooth programming in cc语言中的linux蓝牙编程
【发布时间】:2012-07-09 15:38:34
【问题描述】:

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

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

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

有没有完整的库包,还是必须下载这些头文件?

我正在关注这个link

【问题讨论】:

  • 我是一名 C++ 程序员,但我认为您需要源代码。 Bluetooth.h 是一个 C++ 头文件。
  • 试试apt-get install libbluetooth-dev
  • apt-get 不工作,我可以从launchpad.net/ubuntu/lucid/+source/bluez/4.60-0ubuntu8下载这个包
  • 我无法使用 apt-get 命令,因为我没有连接到互联网,但是如果我从外部下载这些库并通过 pendrive 在我的 ubuntu PC 中使用,是否可以 1) Glib 库,2) Dbus 库,3) Bluez 4) Bluez Utilities
  • 这是DrDubbs未来工作的良好蓝牙链接。

标签: linux bluetooth bluez


【解决方案1】:

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

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

#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

【讨论】:

  • 你自己写的代码还是你从somewhere...那里得到的?
  • 因为你忘了在回答中提到它。
  • 需要安装库:sudo apt-get install libbluetooth-dev 编译前
【解决方案2】:

这解决了我的问题:

apt-get install libbluetooth-dev 

【讨论】:

  • 如果可能,请解释一下这个命令的作用以及它是如何解决问题的。
  • 这个答案让我在我的 Ubuntu 系统上安装 PyBluez。
【解决方案3】:

据我所知,这些标头没有包。您必须从互联网下载以下头文件。

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

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

注意:编译链接时使用-lbluetooth

【讨论】:

    【解决方案4】:

    您需要安装 linux-headers 软件包。在 Ubuntu 或 Debian 上,这是通过这样做来完成的:

    sudo apt install linux-headers
    

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多