From a746ed4a2bce3db85e6887a03347fc7dbb6396ab Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Wed, 18 Mar 2015 12:29:10 +0100 Subject: [PATCH] reconnect support. Closes #10 --- .../BluetoothCommunicationService.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 0b9b2b1fa..b9487ce22 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -289,6 +289,7 @@ public class BluetoothCommunicationService extends Service { private OutputStream mmOutStream = null; private boolean mQuit = false; private boolean mmIsConnected = false; + private int mmConnectionAttempts = 0; public BtSocketIoThread(String btDeviceAddress) { mmBtDeviceAddress = btDeviceAddress; @@ -369,10 +370,21 @@ public class BluetoothCommunicationService extends Service { } } catch (IOException e) { if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right - mBtSocket = null; - setReceiversEnableState(false); - Log.i(TAG, "Bluetooth socket closed, will quit IO Thread"); - mQuit = true; + updateNotification("connection lost, trying to reconnect"); + + while (mmConnectionAttempts++ < 10) { + Log.i(TAG, "Trying to reconnect (attempt " + mmConnectionAttempts + ")"); + mmIsConnected = connect(mmBtDeviceAddress); + if (mmIsConnected) + break; + } + mmConnectionAttempts = 0; + if (!mmIsConnected) { + mBtSocket = null; + setReceiversEnableState(false); + Log.i(TAG, "Bluetooth socket closed, will quit IO Thread"); + mQuit = true; + } } } }