【问题标题】:GLIB D-BUS Bluetooth - How to get the file descriptor?GLIB D-BUS 蓝牙 - 如何获取文件描述符?
【发布时间】:2021-09-09 13:48:04
【问题描述】:

我正在使用 BLUEZ 和 GLIB/D-BUS 连接 2 个 Raspberry Pi(也是一台笔记本电脑和一个 Raspberry Pi)。 到目前为止,我可以取得相当大的进步。

编辑:根据@ukBaz 的好建议,我在笔记本电脑上使用python client,并在Raspberry Pi 上使用我的C 代码服务器。

在“服务器”上,我可以使用自定义服务 UUID 和串行 RFCOMM 配置文件 UUID 注册设备,然后等待连接。与 python 客户端连接有效,我可以看到有一个可用的处理程序(请参阅下面的代码以获取调试输出) 我正在使用这段代码(在 dbus 循环中,为了便于阅读而简化了代码):

static void new_connection(GDBusMethodInvocation *inv)
{
    g_log(LOG_SERVER, G_LOG_LEVEL_MESSAGE, "New connection.");

    GDBusMessage *msg = g_dbus_method_invocation_get_message(inv);

    // This prints the output below this code snippet
    gchar *content = g_dbus_message_print(msg, 2);
    g_log(LOG_SERVER, G_LOG_LEVEL_INFO, "Message is:\n%s", content);
    g_free(content);
    GVariant *params = g_dbus_method_invocation_get_parameters(inv);

    const char *object;
    GVariant *properties;
    gint32 *handle;
    g_variant_get(params, "(oha{sv})", &object, &handle, &properties);

    // Problem here, 'handle' is NULL
    g_log(LOG_SERVER, G_LOG_LEVEL_INFO, "Object is [%s]\nHandle is [%ls]", object, handle);
    GVariantIter iter;
    g_variant_iter_init(&iter, properties);
    display_properties(&iter);
}

这是输出:

New connection.
Message is:
  Type:    method-call
  Flags:   none
  Version: 0
  Serial:  32
  Headers:
    path -> objectpath '/org/bluez/jscturret'
    interface -> 'org.bluez.Profile1'
    member -> 'NewConnection'
    destination -> ':1.18'
    sender -> ':1.11'
    signature -> signature 'oha{sv}'
    num-unix-fds -> uint32 1
  Body: (objectpath '/org/bluez/hci0/dev_00_AA_AA_AA_AA_AA', handle 0, @a{sv} {})
  UNIX File Descriptors:
    fd 7: dev=0:8,mode=0140777,ino=41101,uid=0,gid=0,rdev=0:0,size=0,atime=0,mtime=0,ctime=0

Object is [/org/bluez/hci0/dev_00_AA_AA_AA_AA_AA]
Handle is [(null)]

它显示有一个文件描述符fd 7,但是当我读取 GVariant 参数时,我得到了NULL

如何访问文件描述符?我的理解是我需要能够从/向客户端读取/写入。

我使用了https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txthttps://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt 作为参考,以及其他一些关于 SO 的帖子。 在https://www.linumiz.com/也得到了很多信息。

当前完整代码可在此处获得:btservice

【问题讨论】:

  • 如果您一次只开发蓝牙链路的一端,它可以简化问题。我会使用一个应用程序(例如Serial Bluetooth Terminal 连接到您的服务器以检查它是否正常工作。发现和配对步骤是一次性配置步骤,因此您不需要每次都调用。一旦两个设备配对后,您应该只需要在org.bluez.Device1 中调用Connect 方法。sudo btmon 将为您提供更详细的调试信息。

标签: c bluez gdbus


【解决方案1】:

哦!我很确定你应该发送一个指向整数的指针(而不是指向它的指针)。

你可以的

gint32 句柄; // 而不是 gint32 *handle;

它应该可以工作。

这个 API 的设计很糟糕(依赖可变参数,带有格式说明符……人们不喜欢 C 的原因)。

【讨论】:

  • 确实有效。但是句柄值是0,它代表标准输入......我需要弄清楚如何获得正确的句柄。
  • 我想我找到了解决方案:stackoverflow.com/questions/66061929/…
  • 太棒了!对您可能正在构建的东西感到好奇:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
  • 2021-03-14
  • 2022-11-04
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多