bluez glib编程 --- 代码片段参考
创建数组 ay, a{sv}
gchar data_gchar[] = {0xAA,0x00,0x00,0x00,0x01,0x00,0xFE,0x14,0x4F,0x01,0x00,0x00,0xA3,0x21};
GVariantBuilder builder_ay; g_variant_builder_init(&builder_ay, G_VARIANT_TYPE("ay")); for (guint16 i = 0; i < sizeof(data_gchar); i++) { g_variant_builder_add_value(&builder_ay, g_variant_new_byte(data_gchar[i])); } GVariant *variant_ay = g_variant_builder_end(&builder_ay); gchar *str_ay = g_variant_print(variant_ay, TRUE); RM_LOGI("%s", str_ay); GVariantBuilder builder_asv; g_variant_builder_init(&builder_asv, G_VARIANT_TYPE("a{sv}")); g_variant_builder_add(&builder_asv, "{sv}", "offset", g_variant_new_uint16(0)); g_variant_builder_add(&builder_asv, "{sv}", "type", g_variant_new_string("command")); GVariant *variant_asv = g_variant_builder_end(&builder_asv); gchar *str_asv = g_variant_print(variant_asv, TRUE); RM_LOGI("%s", str_asv);
创建复合变量 aya{sv}
gchar data_gchar[] = {0xAA,0x00,0x00,0x00,0x01,0x00,0xFE,0x14,0x4F,0x01,0x00,0x00,0xA3,0x21}; GVariantBuilder builder_ay; g_variant_builder_init(&builder_ay, G_VARIANT_TYPE("ay")); for (guint16 i = 0; i < sizeof(data_gchar); i++) { g_variant_builder_add_value(&builder_ay, g_variant_new_byte(data_gchar[i])); } GVariantBuilder builder_asv; g_variant_builder_init(&builder_asv, G_VARIANT_TYPE("a{sv}")); g_variant_builder_add(&builder_asv, "{sv}", "offset", g_variant_new_uint16(0)); g_variant_builder_add(&builder_asv, "{sv}", "type", g_variant_new_string("command")); GVariant *variant = g_variant_new("(aya{sv})", &builder_ay, &builder_asv); gchar *str = g_variant_print(variant, TRUE); RM_LOGI("%s", str);
获取某个接口的某个属性
GDBusProxy *device1; device1 = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.bluez", path, "org.bluez.Device1", NULL, &error); PRINT_ERROR_LOG("Device1"); GVariant *name = NULL; char *name_str = NULL; name = g_dbus_proxy_get_cached_property(device1, "Name"); if(name != NULL) { g_variant_get(name, "s", &name_str); g_variant_unref(name); } else { goto done; }
遍历远端蓝牙某个接口的所有属性
result = g_dbus_connection_call_sync( conn, "org.bluez", "/org/bluez/hci0/dev_A4_C1_38_10_09_2C", "org.freedesktop.DBus.Properties", "GetAll", g_variant_new("(s)", "org.bluez.Device1"), // 指定获取某个接口的所有属性 G_VARIANT_TYPE("(a{sv})"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error ); if(error != NULL) { printf("error get property: %s\n", error->message); g_error_free(error); return 1; } GVariantIter *prop_iter; gchar *prop_name; GVariant *prop_value; // 遍历所有属性 g_variant_get(result, "(a{sv})", &prop_iter); while (g_variant_iter_next(prop_iter, "{sv}", &prop_name, &prop_value)) { gchar *value_str = g_variant_print(prop_value, TRUE); g_print(" %s: %s\n", prop_name, value_str); g_free(value_str); g_variant_unref(prop_value); g_free(prop_name); } g_variant_iter_free(prop_iter); g_variant_unref(result);
多重循环
GVariantIter *iter; gchar *service_path; GVariant *interfaces_and_properties; gchar *object; g_variant_get(managedObjects, "(a{oa{sa{sv}}})", &iter); while (g_variant_iter_next(iter, "{oa{sa{sv}}}", &service_path, &interfaces_and_properties)) { printf("%s\n", service_path); if (g_str_has_prefix(service_path, object)) { GVariantIter *iface_iter; gchar *interface_name; GVariant *properties; g_variant_get(interfaces_and_properties, "a{sa{sv}}", &iface_iter); while (g_variant_iter_next(iface_iter, "{sa{sv}}", &interface_name, &properties)) { if (g_strcmp0(interface_name, "org.bluez.GattService1") == 0) { g_print("Found service: %s\n", service_path); // 在这里你可以进一步获取特征并进行操作 } g_variant_unref(properties); g_free(interface_name); } g_variant_iter_free(iface_iter); } g_variant_unref(interfaces_and_properties); g_free(service_path); } g_variant_iter_free(iter);