Revert to previous state if connection fails, this fixes multiple bugs when pebble is reconnecting

This commit is contained in:
Andreas Shimokawa 2015-04-13 13:26:22 +02:00
parent bb44cb1e19
commit 40438ebe0e
2 changed files with 7 additions and 2 deletions

View File

@ -104,7 +104,7 @@ public class BluetoothCommunicationService extends Service {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit();
if (btDeviceAddress != null && !isConnected()) {
if (btDeviceAddress != null && !isConnected() && !isConnecting()) {
// currently only one thread allowed
if (mGBDeviceIoThread != null) {
mGBDeviceIoThread.quit();
@ -201,6 +201,10 @@ public class BluetoothCommunicationService extends Service {
return mGBDevice != null && mGBDevice.getState() == State.CONNECTED;
}
private boolean isConnecting() {
return mGBDevice != null && mGBDevice.getState() == State.CONNECTING;
}
@Override
public void onDestroy() {
super.onDestroy();

View File

@ -80,6 +80,7 @@ public class PebbleIoThread extends GBDeviceIoThread {
protected boolean connect(String btDeviceAddress) {
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
ParcelUuid uuids[] = btDevice.getUuids();
GBDevice.State originalState = gbDevice.getState();
try {
mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
mBtSocket.connect();
@ -87,7 +88,7 @@ public class PebbleIoThread extends GBDeviceIoThread {
mmOutStream = mBtSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
gbDevice.setState(originalState);
mmInStream = null;
mmOutStream = null;
mBtSocket = null;