Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include <glib.h>
  2. #include <gio/gio.h>
  3. #include <stdio.h>
  4. #define AGENT_PATH "/org/bluez/AutoPinAgent"
  5.  
  6. int main(void) {
  7. GError *err = NULL;
  8. GMainLoop *loop;
  9. loop = g_main_loop_new(NULL, FALSE);
  10.  
  11. GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
  12.  
  13.  
  14. if(err != NULL) {
  15. g_print("%s\n%s\n", "Error while attempting to establish a connection with DBUS", err->message);
  16. return 1;
  17. } else {
  18. g_print("\n%s\n", "Connection to DBUS successful!");
  19. }
  20.  
  21. GDBusProxy *agent_manager_proxy = g_dbus_proxy_new_sync(conn,
  22. G_DBUS_PROXY_FLAGS_NONE,
  23. NULL,
  24. "org.bluez",
  25. "/org/bluez",
  26. "org.bluez.AgentManager1",
  27. NULL,
  28. &err);
  29. if(err != NULL) {
  30. g_print("%s\n%s\n", "Error while attempting to create agent proxy", err->message);
  31. return 1;
  32. } else {
  33. g_print("\n%s\n", "Agent proxy created!");
  34. }
  35.  
  36. g_dbus_proxy_call_sync(agent_manager_proxy,
  37. "RegisterAgent",
  38. g_variant_new("(os)", AGENT_PATH, "NoInputNoOutput"),
  39. G_DBUS_CALL_FLAGS_NONE,
  40. -1,
  41. NULL,
  42. &err);
  43.  
  44. if(err != NULL) {
  45. g_print("%s\n%s\n", "Error while attempting to register agent", err->message);
  46. return 1;
  47. } else {
  48. g_print("\n%s\n", "Agent registered.");
  49. }
  50.  
  51. g_dbus_proxy_call_sync(agent_manager_proxy,
  52. "RequestDefaultAgent",
  53. g_variant_new("(o)", AGENT_PATH),
  54. G_DBUS_CALL_FLAGS_NONE,
  55. -1,
  56. NULL,
  57. &err);
  58.  
  59. if(err != NULL) {
  60. g_print("%s\n%s\n", "Error while attempting to register agent", err->message);
  61. return 1;
  62. } else {
  63. g_print("\n%s\n", "Agent now default.");
  64. }
  65.  
  66.  
  67. GDBusProxy *device_proxy = g_dbus_proxy_new_sync(conn,
  68. G_DBUS_PROXY_FLAGS_NONE,
  69. NULL,
  70. "org.bluez",
  71. "/org/bluez/hci0/dev_DC_3F_32_42_62_B0",
  72. "org.bluez.Device1",
  73. NULL,
  74. &err);
  75. if(err != NULL) {
  76. g_print("%s\n%s\n", "Error while attempting to create device proxy", err->message);
  77. return 1;
  78. } else {
  79. g_print("\n%s\n", "Device proxy created!");
  80. }
  81.  
  82.  
  83. g_dbus_proxy_call_sync(device_proxy,
  84. "Pair",
  85. NULL,
  86. G_DBUS_CALL_FLAGS_NONE,
  87. -1,
  88. NULL,
  89. &err);
  90. if(err != NULL) {
  91. g_print("%s\n%s\n", "Pair error", err->message);
  92. return 1;
  93. } else {
  94. g_print("\n%s\n", "Device paired!");
  95. }
  96.  
  97.  
  98.  
  99. g_dbus_proxy_call_sync(device_proxy,
  100. "Connect",
  101. NULL,
  102. G_DBUS_CALL_FLAGS_NONE,
  103. -1,
  104. NULL,
  105. &err);
  106. if(err != NULL) {
  107. g_print("%s\n%s\n", "Error while attempting to create device proxy", err->message);
  108. return 1;
  109. } else {
  110. g_print("\n%s\n", "Device connected!");
  111. }
  112.  
  113. g_main_loop_run(loop);
  114. g_object_unref(conn);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement