From 232f305c3f845fcd75918447fc7262a19fdd0399 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Wed, 26 Jul 2017 22:10:04 -0700 Subject: binding: bluetooth: autoconnect to first paired device Attempt to connect to the first paired device, and poll every 5 seconds till it is successful or another device is connected. Change-Id: Iadc78ca809f0c492d65e0f1cc59837a0f5d2fe45 Bug-AGL: SPEC-722 Signed-off-by: Matt Ranostay --- binding-bluetooth/bluetooth-manager.c | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/binding-bluetooth/bluetooth-manager.c b/binding-bluetooth/bluetooth-manager.c index 13210bb..3038a6e 100644 --- a/binding-bluetooth/bluetooth-manager.c +++ b/binding-bluetooth/bluetooth-manager.c @@ -1066,6 +1066,52 @@ int BluetoothMonitorInit() return 0; } +gboolean bt_autoconnect(gpointer ptr) +{ + LOGD("\n"); + gboolean ret = TRUE; + GSList *list, *tmp; + gchar *bdaddr = NULL; + + devices_list_lock(); + + list = GetBluezDevicesList(); + tmp = list; + + while (tmp) + { + struct bt_device *BDdevice = tmp->data; + tmp = tmp->next; + + if (BDdevice->paired && bdaddr == NULL) + { + bdaddr = g_strdup(BDdevice->bdaddr); + } + + if (BDdevice->connected) + { + g_slist_free_full(list, g_free); + devices_list_unlock(); + return FALSE; + } + } + + g_slist_free_full(list, g_free); + devices_list_unlock(); + + /* + * NOTE: Attempt to connect to first paired device, and once connected don't + * poll anymore. + */ + if (bdaddr != NULL) + { + ret = device_connect(bdaddr, NULL) ? TRUE : FALSE; + g_free(bdaddr); + } + + return ret; +} + /* * Bluetooth Manager Thread * register callback function and create a new GMainLoop structure @@ -1084,6 +1130,9 @@ static void *bt_event_loop_thread() BluetoothManage_InitFlag_Set(TRUE); BluetoothMonitorInit(); + + g_timeout_add_seconds(5, bt_autoconnect, NULL); + LOGD("g_main_loop_run\n"); g_main_loop_run(cli.clientloop); -- cgit 1.2.3-korg