Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glib.h>
- #include <gio/gio.h>
- #include <stdio.h>
- #define AGENT_PATH "/org/bluez/AutoPinAgent"
- int main(void) {
- GError *err = NULL;
- GMainLoop *loop;
- loop = g_main_loop_new(NULL, FALSE);
- GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
- if(err != NULL) {
- g_print("%s\n%s\n", "Error while attempting to establish a connection with DBUS", err->message);
- return 1;
- } else {
- g_print("\n%s\n", "Connection to DBUS successful!");
- }
- GDBusProxy *agent_manager_proxy = g_dbus_proxy_new_sync(conn,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.bluez",
- "/org/bluez",
- "org.bluez.AgentManager1",
- NULL,
- &err);
- if(err != NULL) {
- g_print("%s\n%s\n", "Error while attempting to create agent proxy", err->message);
- return 1;
- } else {
- g_print("\n%s\n", "Agent proxy created!");
- }
- g_dbus_proxy_call_sync(agent_manager_proxy,
- "RegisterAgent",
- g_variant_new("(os)", AGENT_PATH, "NoInputNoOutput"),
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- &err);
- if(err != NULL) {
- g_print("%s\n%s\n", "Error while attempting to register agent", err->message);
- return 1;
- } else {
- g_print("\n%s\n", "Agent registered.");
- }
- g_dbus_proxy_call_sync(agent_manager_proxy,
- "RequestDefaultAgent",
- g_variant_new("(o)", AGENT_PATH),
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- &err);
- if(err != NULL) {
- g_print("%s\n%s\n", "Error while attempting to register agent", err->message);
- return 1;
- } else {
- g_print("\n%s\n", "Agent now default.");
- }
- GDBusProxy *device_proxy = g_dbus_proxy_new_sync(conn,
- G_DBUS_PROXY_FLAGS_NONE,
- NULL,
- "org.bluez",
- "/org/bluez/hci0/dev_DC_3F_32_42_62_B0",
- "org.bluez.Device1",
- NULL,
- &err);
- if(err != NULL) {
- g_print("%s\n%s\n", "Error while attempting to create device proxy", err->message);
- return 1;
- } else {
- g_print("\n%s\n", "Device proxy created!");
- }
- g_dbus_proxy_call_sync(device_proxy,
- "Pair",
- NULL,
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- &err);
- if(err != NULL) {
- g_print("%s\n%s\n", "Pair error", err->message);
- return 1;
- } else {
- g_print("\n%s\n", "Device paired!");
- }
- g_dbus_proxy_call_sync(device_proxy,
- "Connect",
- NULL,
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- &err);
- if(err != NULL) {
- g_print("%s\n%s\n", "Error while attempting to create device proxy", err->message);
- return 1;
- } else {
- g_print("\n%s\n", "Device connected!");
- }
- g_main_loop_run(loop);
- g_object_unref(conn);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement