From 7fd89e491f5d818cb3e9ba609d7e7577990b1887 Mon Sep 17 00:00:00 2001 From: Chengyi Zhao Date: Wed, 18 Aug 2021 20:38:27 +0800 Subject: [PATCH] bluetooth: Try to reconnect SCO When the SCO connection is in use, if you disconnect first and then connect, the SCO connection will occasionally fail, and the Bluetooth error code is 42 (0x2A in hexadecimal). This is usually because an error occurred when the SCO connection was initiated, we need to try to reconnect to optimize the handling of this problem. The log returned by the kernel is as follows: Bluetooth: sco_connect_cfm: hcon 0000000003328902 bdaddr 40:ef:4c:0c:11:f0 status 42 Bluetooth: sco_sock_connect status is -38 Part-of: --- src/modules/bluetooth/backend-native.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/modules/bluetooth/backend-native.c b/src/modules/bluetooth/backend-native.c index a0b33a373..88ce89dd8 100644 --- a/src/modules/bluetooth/backend-native.c +++ b/src/modules/bluetooth/backend-native.c @@ -108,6 +108,9 @@ static uint32_t hfp_features = * The choice seems to be a bit arbitrary -- it looks like at least channels 2, 4 and 5 also work*/ #define HSP_HS_DEFAULT_CHANNEL 3 +/* Total number of trying to reconnect */ +#define SCO_RECONNECTION_COUNT 3 + #define PROFILE_INTROSPECT_XML \ DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ "" \ @@ -320,11 +323,22 @@ fail: static int sco_acquire_cb(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu) { int sock; socklen_t len; + int i; if (optional) sock = sco_do_accept(t); - else - sock = sco_do_connect(t); + else { + for (i = 0; i < SCO_RECONNECTION_COUNT; i++) { + sock = sco_do_connect(t); + + if (sock < 0) { + pa_log_debug("err is %s and reconnection count is %d", pa_cstrerror(errno), i); + pa_msleep(300); + continue; + } else + break; + } + } if (sock < 0) goto fail;