From 3b250a45688c1c868c6950ea832dddb17bdae2fe Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sat, 19 Nov 2016 23:49:18 +0100 Subject: [PATCH] Pebble 2: initial hacky reconnect support Works sometimes, at least less crashes (#432) --- .../service/devices/pebble/PebbleIoThread.java | 4 ++-- .../service/devices/pebble/ble/PebbleGATTClient.java | 4 +++- .../service/devices/pebble/ble/PebbleGATTServer.java | 3 +++ .../service/devices/pebble/ble/PebbleLESupport.java | 7 ++++++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java index 32238019..27fa3587 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java @@ -368,8 +368,8 @@ class PebbleIoThread extends GBDeviceIoThread { } catch (InterruptedException e) { e.printStackTrace(); } - } catch (IOException e) { - if (e.getMessage() != null && e.getMessage().contains("socket closed")) { //FIXME: this does not feel right + } catch (IOException | ArrayIndexOutOfBoundsException e) { + if (e.getMessage() != null && (e instanceof ArrayIndexOutOfBoundsException || e.getMessage().contains("socket closed"))) { //FIXME: this does not feel right LOG.info(e.getMessage()); mIsConnected = false; int reconnectAttempts = prefs.getInt("pebble_reconnect_attempts", 10); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java index 8121e0a6..82a8aedb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java @@ -93,7 +93,9 @@ class PebbleGATTClient extends BluetoothGattCallback { if (newState == BluetoothGatt.STATE_CONNECTED) { LOG.info("calling discoverServices()"); gatt.discoverServices(); - + } + else if (newState == BluetoothGatt.STATE_DISCONNECTED){ + mPebbleLESupport.close(); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java index 81edbc60..08b58f61 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java @@ -127,6 +127,9 @@ class PebbleGATTServer extends BluetoothGattServerCallback { public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { LOG.info("Connection state change for device: " + device.getAddress() + " status = " + status + " newState = " + newState); + if (newState == BluetoothGattServer.STATE_DISCONNECTED) { + mPebbleLESupport.close(); + } } public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java index 55c0008b..ff1dafcb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java @@ -50,7 +50,7 @@ public class PebbleLESupport { } } - public void close() { + synchronized public void close() { destroyPipedInputReader(); if (mPebbleGATTServer != null) { mPebbleGATTServer.close(); @@ -60,6 +60,11 @@ public class PebbleLESupport { mPebbleGATTClient.close(); mPebbleGATTClient = null; } + try { + mPipedInputStream.close(); + mPipedOutputStream.close(); + } catch (IOException ignore) { + } } void createPipedInputReader() {