Pebble 2: initial hacky reconnect support

Works sometimes, at least less crashes
(#432)
This commit is contained in:
Andreas Shimokawa 2016-11-19 23:49:18 +01:00
parent c95587c915
commit 3b250a4568
4 changed files with 14 additions and 4 deletions

View File

@ -368,8 +368,8 @@ class PebbleIoThread extends GBDeviceIoThread {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} catch (IOException e) { } catch (IOException | ArrayIndexOutOfBoundsException e) {
if (e.getMessage() != null && e.getMessage().contains("socket closed")) { //FIXME: this does not feel right if (e.getMessage() != null && (e instanceof ArrayIndexOutOfBoundsException || e.getMessage().contains("socket closed"))) { //FIXME: this does not feel right
LOG.info(e.getMessage()); LOG.info(e.getMessage());
mIsConnected = false; mIsConnected = false;
int reconnectAttempts = prefs.getInt("pebble_reconnect_attempts", 10); int reconnectAttempts = prefs.getInt("pebble_reconnect_attempts", 10);

View File

@ -93,7 +93,9 @@ class PebbleGATTClient extends BluetoothGattCallback {
if (newState == BluetoothGatt.STATE_CONNECTED) { if (newState == BluetoothGatt.STATE_CONNECTED) {
LOG.info("calling discoverServices()"); LOG.info("calling discoverServices()");
gatt.discoverServices(); gatt.discoverServices();
}
else if (newState == BluetoothGatt.STATE_DISCONNECTED){
mPebbleLESupport.close();
} }
} }

View File

@ -127,6 +127,9 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
LOG.info("Connection state change for device: " + device.getAddress() + " status = " + status + " newState = " + 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, public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,

View File

@ -50,7 +50,7 @@ public class PebbleLESupport {
} }
} }
public void close() { synchronized public void close() {
destroyPipedInputReader(); destroyPipedInputReader();
if (mPebbleGATTServer != null) { if (mPebbleGATTServer != null) {
mPebbleGATTServer.close(); mPebbleGATTServer.close();
@ -60,6 +60,11 @@ public class PebbleLESupport {
mPebbleGATTClient.close(); mPebbleGATTClient.close();
mPebbleGATTClient = null; mPebbleGATTClient = null;
} }
try {
mPipedInputStream.close();
mPipedOutputStream.close();
} catch (IOException ignore) {
}
} }
void createPipedInputReader() { void createPipedInputReader() {