C语言中的linux蓝牙编程 [英] linux Bluetooth programming in c

查看:1343
本文介绍了C语言中的linux蓝牙编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

使用命令sudo apt-get install bluez安装必需的blueZ库,这表示bluez已经是最新版本.

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

但是出现错误,导致无法使用gcc -o simplescan simplescan.c -lbluetooth

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

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

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

我正在关注链接

推荐答案

也许您没有包含基本标头.

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

要在Linux上进行编译,只需完成

to compile it on linux, just do

gcc -o simplescan simplescan.c -lbluetooth

原始代码位于此处

这篇关于C语言中的linux蓝牙编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆