diff options
Diffstat (limited to 'binding')
-rw-r--r-- | binding/bluetooth-api.c | 2 | ||||
-rw-r--r-- | binding/bluetooth-api.h | 2 | ||||
-rw-r--r-- | binding/bluetooth-bluez.c | 47 |
3 files changed, 51 insertions, 0 deletions
diff --git a/binding/bluetooth-api.c b/binding/bluetooth-api.c index 58ed636..5d26f3a 100644 --- a/binding/bluetooth-api.c +++ b/binding/bluetooth-api.c @@ -420,6 +420,8 @@ static struct bluetooth_state *bluetooth_init(GMainLoop *loop) bluetooth_monitor_init(); + g_timeout_add_seconds(5, bluetooth_autoconnect, ns); + return ns; err_no_device_sub: diff --git a/binding/bluetooth-api.h b/binding/bluetooth-api.h index 209f992..f8ce9d8 100644 --- a/binding/bluetooth-api.h +++ b/binding/bluetooth-api.h @@ -142,6 +142,8 @@ gboolean bluez_set_property(struct bluetooth_state *ns, gboolean is_json_name, const char *name, json_object *jval, GError **error); +gboolean bluetooth_autoconnect(gpointer data); + /* convenience access methods */ static inline gboolean device_property_dbus2json(json_object *jprop, const gchar *key, GVariant *var, gboolean *is_config, diff --git a/binding/bluetooth-bluez.c b/binding/bluetooth-bluez.c index 37c4469..052ea7b 100644 --- a/binding/bluetooth-bluez.c +++ b/binding/bluetooth-bluez.c @@ -518,3 +518,50 @@ gboolean bluez_set_property(struct bluetooth_state *ns, return TRUE; } + +gboolean bluetooth_autoconnect(gpointer data) +{ + struct bluetooth_state *ns = data; + GError *error = NULL; + json_object *jresp, *jobj = NULL; + int i; + + jresp = object_properties(ns, &error); + + json_object_object_get_ex(jresp, "devices", &jobj); + + for (i = 0; i < json_object_array_length(jobj); i++) { + json_object *idx = json_object_array_get_idx(jobj, i); + json_object *props = NULL; + json_object_object_get_ex(idx, "properties", &props); + + if (props) { + json_object *paired = NULL; + json_object_object_get_ex(props, "paired", &paired); + + if (paired && json_object_get_boolean(paired)) { + GVariant *reply; + json_object *tmp = NULL; + const char *adapter, *device; + gchar *path; + + json_object_object_get_ex(idx, "device", &tmp); + device = json_object_get_string(tmp); + + json_object_object_get_ex(idx, "adapter", &tmp); + adapter = json_object_get_string(tmp); + + path = g_strconcat("/org/bluez/", adapter, "/", device, NULL); + + reply = bluez_call(ns, "device", path, "Connect", NULL, NULL); + g_free(path); + + if (reply) + return FALSE; + g_variant_unref(reply); + } + } + } + + return FALSE; +} |