Guest User

Untitled

a guest
Feb 21st, 2021
43
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <glib.h>
  2. #include <gio/gio.h>
  3. #include <stdio.h>
  4.  
  5. static void hello_world(GDBusProxy *proxy, GAsyncResult *res, gpointer data) {
  6. GVariant *result = NULL;
  7. GError *err = NULL;
  8. result = g_dbus_proxy_call_finish(proxy, res, &err);
  9.  
  10. if(err != NULL) {
  11. g_print("%s\n%s\n", "Error while attempting to establish a connection with DBUS", err->message); // res is void :(
  12.  
  13. } else {
  14. g_print("\n%s %s\n", "Notifications on: ", g_variant_get_type_string(res));
  15. }
  16.  
  17. }
  18.  
  19. int main(void) {
  20. GError *err = NULL;
  21. GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
  22.  
  23. GMainLoop *loop;
  24. loop = g_main_loop_new(NULL, FALSE);
  25. if(err != NULL) {
  26. g_print("%s\n%s\n", "Error while attempting to establish a connection with DBUS", err->message);
  27. return 1;
  28. } else {
  29. g_print("\n%s\n", "Connection to DBUS successful!");
  30. }
  31.  
  32. GDBusProxy *beacon_proxy = g_dbus_proxy_new_sync(conn,
  33. G_DBUS_PROXY_FLAGS_NONE,
  34. NULL,
  35. "org.bluez",
  36. "/org/bluez/hci0/dev_DC_3F_32_42_62_B0",
  37. "org.bluez.Device1",
  38. NULL,
  39. &err);
  40.  
  41. if(err != NULL) {
  42. g_print("%s\n%s\n", "Error while attempting to create a proxy", err->message);
  43. return 1;
  44. } else {
  45. g_print("\n%s\n", "Proxy created successfully!");
  46. }
  47.  
  48. g_dbus_proxy_call_sync(beacon_proxy,
  49. "Connect",
  50. NULL,
  51. G_DBUS_CALL_FLAGS_NONE,
  52. -1,
  53. NULL,
  54. &err);
  55. if(err != NULL) {
  56. g_print("%s\n%s\n", "Error while calling a method through proxy123", err->message);
  57. return 1;
  58. } else {
  59. g_print("\n%s\n", "Proxy method call, successful!");
  60. }
  61.  
  62. sleep(3);
  63.  
  64. ## Code to test connection quality and flash led
  65. GDBusProxy *gatt_proxy = g_dbus_proxy_new_sync(conn,
  66. G_DBUS_PROXY_FLAGS_NONE,
  67. NULL,
  68. "org.bluez",
  69. "/org/bluez/hci0/dev_DC_3F_32_42_62_B0/service0035/char0036",
  70. "org.bluez.GattCharacteristic1",
  71. NULL,
  72. &err);
  73. if(err != NULL) {
  74. g_print("%s\n%s\n", "Error while attempting to create a proxy", err->message);
  75. return 1;
  76. } else {
  77. g_print("\n%s\n", "Proxy created successfully!");
  78. }
  79.  
  80. for(int i=0; i<15; i++) {
  81. GVariantBuilder builder;
  82. g_variant_builder_init (&builder, G_VARIANT_TYPE("a{sv}"));
  83. GVariant *argument = g_variant_builder_end(&builder);
  84.  
  85. char* data = "1";
  86. if(i%2==0) {
  87. data = "";
  88. } else {data = "1";}
  89. GVariant *char_value = g_variant_new_from_data(G_VARIANT_TYPE("ay"), data, 1, TRUE, NULL, NULL);
  90.  
  91.  
  92. GVariant *result = g_dbus_proxy_call_sync(gatt_proxy,
  93. "WriteValue",
  94. g_variant_new("(@ay@a{sv})", char_value, argument),
  95. G_DBUS_CALL_FLAGS_NONE,
  96. -1,
  97. NULL,
  98. &err);
  99. if(err != NULL) {
  100. g_print("%s\n%s\n", "Error while calling a method through proxy", err->message);
  101. return 1;
  102. } else {
  103. g_print("\n%s\n", "Proxy method call, successful!");
  104. }
  105.  
  106. usleep(100);
  107. }
  108.  
  109. ## Code to test if notifications work? TODO: Find out how to receive notif after
  110. GDBusProxy *gatt_proxy2 = g_dbus_proxy_new_sync(conn,
  111. G_DBUS_PROXY_FLAGS_NONE,
  112. NULL,
  113. "org.bluez",
  114. "/org/bluez/hci0/dev_DC_3F_32_42_62_B0/service0018/char0019",
  115. "org.bluez.GattCharacteristic1",
  116. NULL,
  117. &err);
  118.  
  119. g_dbus_proxy_call(gatt_proxy2,
  120. "StartNotify",
  121. NULL,
  122. G_DBUS_CALL_FLAGS_NONE,
  123. -1,
  124. NULL,
  125. hello_world,
  126. NULL);
  127. if(err != NULL) {
  128. g_print("%s\n%s\n", "Error while starting notify", err->message);
  129. return 1;
  130. } else {
  131. g_print("\n%s\n", "Notify started!");
  132. }
  133.  
  134.  
  135.  
  136. g_main_loop_run(loop);
  137. g_object_unref(conn);
  138. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×