From 6a3afe1cd35fde2a8b9caa7472d75a262d6f78c3 Mon Sep 17 00:00:00 2001 From: sirmordred Date: Wed, 15 Jun 2016 14:07:42 +0300 Subject: Various updates and fixes https://github.com/googlesamples/android-BluetoothChat/pull/9 BluetoothChatService: Robustify connection states * Declare connection states within methods instead of before nor after method * States should be called immediately after creating sockets && before running thread * Fixes race condition and restore two-way chat again BluetoothChatService: Update setState method * Connection state changes are handled externally(mState) now * So rename setState method to updateUserInterfaceTitle method BluetoothChatService: Remove unnecessary calls * BluetoothChatService.this.start() is already declared in connectionLost() method BluetoothChatService: Set connection state after failure * and update the UI title Change-Id: Iae5412832205ee13894aa6ba3c786652155e23f2 --- .../bluetoothchat/BluetoothChatService.java | 41 ++++++++++++++-------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'connectivity/bluetooth') diff --git a/connectivity/bluetooth/BluetoothChat/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java b/connectivity/bluetooth/BluetoothChat/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java index a1e7cc01..b07ffe16 100644 --- a/connectivity/bluetooth/BluetoothChat/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java +++ b/connectivity/bluetooth/BluetoothChat/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java @@ -60,6 +60,7 @@ public class BluetoothChatService { private ConnectThread mConnectThread; private ConnectedThread mConnectedThread; private int mState; + private int state; // Constants that indicate the current connection state public static final int STATE_NONE = 0; // we're doing nothing @@ -76,17 +77,17 @@ public class BluetoothChatService { public BluetoothChatService(Context context, Handler handler) { mAdapter = BluetoothAdapter.getDefaultAdapter(); mState = STATE_NONE; + state = mState; mHandler = handler; } /** - * Set the current state of the chat connection - * - * @param state An integer defining the current connection state + * Update UI title according to the current state of the chat connection */ - private synchronized void setState(int state) { - Log.d(TAG, "setState() " + mState + " -> " + state); - mState = state; + private synchronized void updateUserInterfaceTitle() { + mState = getState(); + Log.d(TAG, "updateUserInterfaceTitle() " + state + " -> " + mState); + state = mState; // Give the new state to the Handler so the UI Activity can update mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, state, -1).sendToTarget(); @@ -118,8 +119,6 @@ public class BluetoothChatService { mConnectedThread = null; } - setState(STATE_LISTEN); - // Start the thread to listen on a BluetoothServerSocket if (mSecureAcceptThread == null) { mSecureAcceptThread = new AcceptThread(true); @@ -129,6 +128,8 @@ public class BluetoothChatService { mInsecureAcceptThread = new AcceptThread(false); mInsecureAcceptThread.start(); } + // Update UI title + updateUserInterfaceTitle(); } /** @@ -157,7 +158,8 @@ public class BluetoothChatService { // Start the thread to connect with the given device mConnectThread = new ConnectThread(device, secure); mConnectThread.start(); - setState(STATE_CONNECTING); + // Update UI title + updateUserInterfaceTitle(); } /** @@ -202,8 +204,8 @@ public class BluetoothChatService { bundle.putString(Constants.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); - - setState(STATE_CONNECTED); + // Update UI title + updateUserInterfaceTitle(); } /** @@ -231,7 +233,9 @@ public class BluetoothChatService { mInsecureAcceptThread.cancel(); mInsecureAcceptThread = null; } - setState(STATE_NONE); + mState = STATE_NONE; + // Update UI title + updateUserInterfaceTitle(); } /** @@ -263,6 +267,10 @@ public class BluetoothChatService { msg.setData(bundle); mHandler.sendMessage(msg); + mState = STATE_NONE; + // Update UI title + updateUserInterfaceTitle(); + // Start the service over to restart listening mode BluetoothChatService.this.start(); } @@ -278,6 +286,10 @@ public class BluetoothChatService { msg.setData(bundle); mHandler.sendMessage(msg); + mState = STATE_NONE; + // Update UI title + updateUserInterfaceTitle(); + // Start the service over to restart listening mode BluetoothChatService.this.start(); } @@ -309,6 +321,7 @@ public class BluetoothChatService { Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e); } mmServerSocket = tmp; + mState = STATE_LISTEN; } public void run() { @@ -396,6 +409,7 @@ public class BluetoothChatService { Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e); } mmSocket = tmp; + mState = STATE_CONNECTING; } public void run() { @@ -465,6 +479,7 @@ public class BluetoothChatService { mmInStream = tmpIn; mmOutStream = tmpOut; + mState = STATE_CONNECTED; } public void run() { @@ -484,8 +499,6 @@ public class BluetoothChatService { } catch (IOException e) { Log.e(TAG, "disconnected", e); connectionLost(); - // Start the service over to restart listening mode - BluetoothChatService.this.start(); break; } } -- cgit v1.2.3