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

View File

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