【发布时间】: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.txt 和https://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将为您提供更详细的调试信息。