简体   繁体   English

如何在C ++中使用Bluez5 DBUS API来配对和连接新设备?

[英]How can I use Bluez5 DBUS API in C++ to pair and connect new devices?

I am writing a bluetooth driver for Intel Edison . 我正在为英特尔爱迪生编写一个蓝牙驱动程序。 Board software is latest available, and I am developing using the Eclipse based IDE. Board软件是最新的,我正在使用基于Eclipse的IDE进行开发。 Bluez version number in this edison release is 5.37. 此爱迪生版本中的Bluez版本号为5.37。

I am designing a system which has to meet the following requirements: 我正在设计一个必须满足以下要求的系统:

  • Scan for bluetooth devices nearby. 扫描附近的蓝牙设备。 [X] [X]
  • Detect sensor devices based on name and MAC address. 根据名称和MAC地址检测传感器设备。 [X] [X]
  • Pair and connect sensor devices automatically. 自动配对和连接传感器设备。 [] []

Last item is the problem since I can detect sensor devices but I am not able to pair them using the bluez5 interface. 最后一项是问题,因为我可以检测传感器设备,但我无法使用bluez5接口配对它们。 So far I have tried to use the D-BUS interface but it is not working since I keep getting the following error message: 到目前为止,我已尝试使用D-BUS接口,但由于我不断收到以下错误消息,因此无法正常工作:

Method "FindAdapter" with signature "s" on interface "org.bluez.Manager" doesn't exist 接口“org.bluez.Manager”上带有签名“s”的方法“FindAdapter”不存在

Code is presented here. 代码在此处显示。 Please note: 请注意:

  • DBusConnection *conn -> DBUS_BUS_SYSTEM DBusConnection * conn - > DBUS_BUS_SYSTEM
  • const char *adapter -> "hci0". const char * adapter - >“hci0”。

Code: 码:

DBusMessage *msg, *reply;
DBusError err;
const char *reply_path;
char *path;

msg = dbus_message_new_method_call("org.bluez", "/","org.bluez.Manager", "FindAdapter");
dbus_message_append_args(msg, DBUS_TYPE_STRING, &adapter,DBUS_TYPE_INVALID);
dbus_error_init(&err);
reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
dbus_message_unref(msg);

Any ideas? 有任何想法吗?

To give you an anwser, Pair and Connect are associated with the device-api.txt . 为了给你一个anwser,Pair和Connect与device-api.txt相关联。 To call these methods you can send dbus messages (like you did in the code presented above) or build a Proxy object with the following parameters (found in the documentation) : 要调用这些方法,您可以发送dbus消息(就像您在上面的代码中所做的那样)或使用以下参数构建Proxy对象(可在文档中找到):

name : "org.bluez" 名称:“org.bluez”

interface "org.bluez.Device1" 接口“org.bluez.Device1”

path : "/org/bluez/dev_AA_BB_CC_DD_EE" where AA_BB_CC_DD_EE is your device mac address. path:“/ org / bluez / dev_AA_BB_CC_DD_EE”其中AA_BB_CC_DD_EE是您的设备mac地址。

If you choose to build a proxy object, you can call methods like Pair or Connect through the proxy. 如果选择构建代理对象,则可以通过代理调用Pair或Connect等方法。


Could you explain what you are trying to achieve in the code above ? 你能解释一下你在上面的代码中想要实现的目标吗? I understand that you want to find which adapter to use (I see the "FindAdapter" method) however it seems you already knows that your adapter name is "hci0". 我知道你想找到使用哪个适配器(我看到“FindAdapter”方法)但是你似乎已经知道你的适配器名称是“hci0”。


I have been working with the DBus API exposed by Bluez recently and I was unfamiliar with the interface "org.bluez.Manager". 我最近一直在使用Bluez公开的DBus API,我不熟悉接口“org.bluez.Manager”。

After a quick search in the official documentation ( https://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc ) I was able to find the following commit which specifies that the interface was dropped in 2012 : https://git.kernel.org/cgit/bluetooth/bluez.git/commit/doc?id=86a7b07c22f3a595ba3c48092359287905bf0878 在官方文档( https://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc )中快速搜索后,我能够找到以下提交,指定在2012年删除了接口: https :?//git.kernel.org/cgit/bluetooth/bluez.git/commit/doc ID = 86a7b07c22f3a595ba3c48092359287905bf0878

I also noticed you were using the DBus low-level API, as advised by freedesktop themselves (read at the bottom of the page here : https://dbus.freedesktop.org/doc/api/html/group__DBus.html ), this is very complex API useful to create bindings in other languages. 我也注意到你正在使用DBus低级API,正如freedesktop自己所建议的那样(在页面底部阅读: https//dbus.freedesktop.org/doc/api/html/group__DBus.html ),这个是非常复杂的API,可用于在其他语言中创建绑定。 If you can, switch to GLib GDBus for a much simpler API. 如果可以,请切换到GLib GDBus以获得更简单的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM