我正在尝试使用蓝光D总线API和GDBus来检测添加的蓝牙设备/适配器。但是,我无法检查添加的D总线接口的名称.
我已经尝试使用底层的GDBusInterfaceInfo C对象访问接口名称,但是在Gio::DBus::接口上调用get_info()会导致分段错误或返回一个空指针。
此外,在Gio::DBUS::Object上调用get_interface("org.bluez.Adapter1")会打印此警告:
** (进程:60136):警告**:11:11:58.443: Glib::wrap_auto_interface():C++实例(N3Gio4DBus5ProxyE)不对接口进行dynamic_cast。
这是我的密码。我用:g++ dbus.cpp `pkg-config --cflags --libs glibmm-2.4 giomm-2.4` -g编译了它,我的glibmm版本是glibmm 2.66.4-1。
#include <glibmm.h>
#include <giomm.h>
void on_object_added(const Glib::RefPtr<Gio::DBus::Object>& o)
{
for (auto iface : o->get_interfaces())
{
auto info = iface->get_info(); // Causes Segmentation fault.
if (!info)
{
std::cout << "Null InterfaceInfo\n";
}
}
}
int main()
{
Gio::init();
auto loop = Glib::MainLoop::create();
auto objman = Gio::DBus::ObjectManagerClient::create_for_bus_sync(
Gio::DBus::BUS_TYPE_SYSTEM, "org.bluez", "/");
objman->signal_object_added().connect(sigc::ptr_fun(&on_object_added));
for (const auto& o : objman->get_objects())
{
std::cout << o->get_object_path() << '\n';
// The next line prints:
// ** (process:60136): WARNING **: 11:11:58.443: Glib::wrap_auto_interface(): The C++ instance (N3Gio4DBus5ProxyE) does not dynamic_cast to the interface.
auto adapter = o->get_interface("org.bluez.Adapter1");
for (const auto& iface : o->get_interfaces())
{
// iface is not a GDBus Proxy instance,
// but a PN3Gio4DBus9InterfaceE.
std::cout << " " << typeid(iface.operator->()).name() << '\n';
}
std::cout << '\n';
}
loop->run();
}我做错了什么?当我不处理GDBusProxy实例时,如何查看接口的名称?是否可以使用GDBusProxy获取GDBusObjectManagerClient实例?
我找不到如何做这件事的任何例子。似乎Giomm GDBus的例子和支持是短缺的。
发布于 2022-09-25 22:35:01
尝试:
auto proxy = std::dynamic_pointer_cast<Gio::DBus::Proxy>(iface);
std::cout << ' ' << proxy->get_interface_name() << '\n';这适用于我使用glibmm-2.68。
Gio文档::DBus::Interface表明,Gio::DBus::Interface是Gio::DBus::Proxy的超类,实际上,您提供的代码中的所有Gio::DBus::Interface实例也都是Gio::DBus::Proxy实例。
https://stackoverflow.com/questions/72422116
复制相似问题