Bluez 的 C/C++ BLE 读/写示例

C/C++ BLE read/write example with Bluez(Bluez 的 C/C++ BLE 读/写示例)

本文介绍了Bluez 的 C/C++ BLE 读/写示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用微控制器和树莓派(tardis BLE 加密狗)构建一个简单的 BLE 网络.作为起点,我正在寻找一个简单的 C 或 C++ 示例来读/写 BLE 设备,类似于我能够通过命令行执行的操作.到目前为止,我发现的例子相当复杂.作为一名 BLE 初学者,我需要一些非常简单的示例来构建,然后再进行更复杂的设计.我可以像下面的 gatttool 示例中那样对 BLE 设备名称进行硬编码.

I am starting to build up a simple BLE network with a microcontroller and a raspberry pi (tardis BLE dongle). As a starting point, I am looking for a simple C or C++ example to read/write a BLE device, similar to what I am able to do over the command line. The examples I have found so far are quite complicated. As a BLE beginner I need some very simple examples to build from before moving forward with a more complicated design. I am okay with hard-coding the BLE device name as I have in the gatttool example below.

这是我目前使用 Bluez 命令行选项的方式.

Here is how I currently use the command line options from Bluez.

从命令行我可以使用:

$ sudo hcitool lescan
LE Scan ...
BB:A0:50:02:18:07 MyDevice

接下来我可以使用 gatttool 在命令行上连接到设备:

Next I am able to connect to the device on the command line with gatttool:

$ sudo gatttol -b BB:A0:50:02:18:07 -I
[BB:A0:50:02:18:07][LE]> connect
Attempting to connect to BB:A0:50:02:18:07
Connection successful

我终于可以使用适当的句柄进行读写了

Finally I am able to read and write using the appropriate handles

[BB:A0:50:02:18:07][LE]> char-write-req 000f 0100
Characteristic value was written successfully
[BB:A0:50:02:18:07][LE]> char-write-cmd 0011 4C467A

我用于初步研究和入门的一些网站:
http://people.csail.mit.edu/albert/bluez-intro/c404.html
https://github.com/carsonmcdonald/bluez-实验/blob/master/experiments/scantest.c

Some sites I have used for initial research and to get started:
http://people.csail.mit.edu/albert/bluez-intro/c404.html
https://github.com/carsonmcdonald/bluez-experiments/blob/master/experiments/scantest.c

推荐答案

Bluez(Linux 官方蓝牙堆栈)已移至 DBUS 用于其 API.在过去,接受"分叉"bluez 代码以访问 Linux 上的 BLE 支持,而现在的方法是使用 DBUS.Bluez DBUS API 记录在此处:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc

Bluez (Linux official Bluetooth stack) has moved to DBUS for its API. While in the past, it was 'accepted' to 'fork' bluez code to access BLE support on Linux, now the approach is to use DBUS. Bluez DBUS API is documented here: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc

要么直接与 DBUS 对话以获取蓝牙 C/C++ 应用程序,要么使用 GATT 库作为助手.第二种方法可能是初学者的最佳方法(也适用于希望让 Bluez 程序不可知的非初学者,例如支持其他操作系统或 Bluez pre-DBUS API 或使其源代码更具可读性).

Either you directly speak to DBUS for your bluetooth C/C++ application or you use a GATT library as a helper. The second one is probably the best approach for beginner (and also for non-beginner who would prefer to keep their program Bluez agnostic for instance to support other OSes or Bluez pre-DBUS API or to have their sources more readable).

支持现代 D-BUS API 的 GATT 库之一是 gattlib(注意:我是该库的作者).这是一个基于此库的简单示例,用于读取/写入 BLE 设备:https://github.com/labapart/gattlib/blob/master/examples/read_write/read_write.c

One of these GATT libraries that support modern D-BUS API is gattlib (note: I am the author of this library). Here is a simple example based on this library for reading/writing a BLE device: https://github.com/labapart/gattlib/blob/master/examples/read_write/read_write.c

这篇关于Bluez 的 C/C++ BLE 读/写示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Bluez 的 C/C++ BLE 读/写示例

基础教程推荐