蓝牙 bluez 的编程 C C++

本文介绍了如何使用Linux下的蓝牙bluez库进行编程。通过具体的代码示例,详细讲解了如何设置蓝牙广告参数并启用广告功能。这对于希望在Linux环境下开发蓝牙应用的开发者来说是一个很好的起点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蓝牙 bluez 的编程 C C++

简介
bluez目录有一个libbluetooth.a文件
有一个目录 lib目录里面存储这网络连接的部分代码
基于库的代码编程.
在linux下如果自带了蓝牙,可以通过gcc **.c -lbluetooth 直接编出一个可执行文件.

参考文献

下面的连接提供了一个测试代码.
博客链接

另外,关于 ble 使用库的方式一直出现失败的情况,是因为,没有设置ble的相关参数,具体可以参考
博客链接
中的enable_advertising函数

 int device_id = hci_get_route(NULL);

        int device_handle = 0;
        if ((device_handle = hci_open_dev(device_id)) < 0) {
                perror("Could not open device");
                exit(1);
        }

        le_set_advertising_parameters_cp adv_params_cp = { 0 };
        adv_params_cp.min_interval = htobs(advertising_interval);
        adv_params_cp.max_interval = htobs(advertising_interval);
        adv_params_cp.chan_map = 7;

        uint8_t status;
        struct hci_request rq = { 0 };
        rq.ogf = OGF_LE_CTL;
        rq.ocf = OCF_LE_SET_ADVERTISING_PARAMETERS;
        rq.cparam = &adv_params_cp;
        rq.clen = LE_SET_ADVERTISING_PARAMETERS_CP_SIZE;
        rq.rparam = &status;
        rq.rlen = 1;

        int ret = hci_send_req(device_handle, &rq, 1000);
        if (ret < 0) {
                hci_close_dev(device_handle);
                fprintf(stderr, "Can't send request %s (%d)\n", strerror(errno), errno);
                return (1);
        }

        le_set_advertise_enable_cp advertise_cp = { 0 };
        advertise_cp.enable = 0x01;

        memset(&rq, 0, sizeof(rq));
        rq.ogf = OGF_LE_CTL;
        rq.ocf = OCF_LE_SET_ADVERTISE_ENABLE;
        rq.cparam = &advertise_cp;
        rq.clen = LE_SET_ADVERTISE_ENABLE_CP_SIZE;
        rq.rparam = &status;
        rq.rlen = 1;

        ret = hci_send_req(device_handle, &rq, 1000);

        if (ret < 0) {
                hci_close_dev(device_handle);
                fprintf(stderr, "Can't send request %s (%d)\n", strerror(errno), errno);
                return (1);
        }

转载于:https://www.cnblogs.com/eat-too-much/p/8885522.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值