diff options
-rw-r--r-- | src/com/android/bluetooth/map/BluetoothMasService.java | 79 |
1 files changed, 39 insertions, 40 deletions
diff --git a/src/com/android/bluetooth/map/BluetoothMasService.java b/src/com/android/bluetooth/map/BluetoothMasService.java index 2bbd8f67b..b356f84be 100644 --- a/src/com/android/bluetooth/map/BluetoothMasService.java +++ b/src/com/android/bluetooth/map/BluetoothMasService.java @@ -560,7 +560,7 @@ public class BluetoothMasService extends Service { for (BluetoothMasObexConnection connection : mConnections) { // Stop the possible trying to init serverSocket connection.mInterrupted = true; - connection.closeConnection(); + connection.closeConnection(true); } } @@ -645,12 +645,6 @@ public class BluetoothMasService extends Service { if (VERBOSE) Log.v(TAG, "Map Service startRfcommSocketListener"); - if (mServerSocket == null) { - if (!initSocket()) { - closeService(); - return; - } - } if (mAcceptThread == null) { mAcceptThread = new SocketAcceptThread(mnsClient, mMasId); mAcceptThread.setName("BluetoothMapAcceptThread " + mPortNum); @@ -701,14 +695,24 @@ public class BluetoothMasService extends Service { return initSocketOK; } - private final void closeSocket() throws IOException { - if (mConnSocket != null) { + private final void closeSocket(boolean server, boolean accept) throws IOException { + if (server) { + // Stop the possible trying to init serverSocket + mInterrupted = true; + + if (mServerSocket != null) { + mServerSocket.close(); + mServerSocket = null; + } + } + + if (accept && mConnSocket != null) { mConnSocket.close(); mConnSocket = null; } } - public void closeConnection() { + public void closeConnection(boolean closeServer) { if (VERBOSE) Log.v(TAG, "Mas connection closing"); // Release the wake lock if obex transaction is over @@ -716,21 +720,8 @@ public class BluetoothMasService extends Service { if (mWakeLock.isHeld()) { if (VERBOSE) Log.v(TAG,"Release full wake lock"); mWakeLock.release(); - mWakeLock = null; - } else { - mWakeLock = null; - } - } - - if (mAcceptThread != null) { - try { - mAcceptThread.shutdown(); - mAcceptThread.join(); - } catch (InterruptedException ex) { - Log.w(TAG, "mAcceptThread close error" + ex); - } finally { - mAcceptThread = null; } + mWakeLock = null; } if (mServerSession != null) { @@ -739,10 +730,24 @@ public class BluetoothMasService extends Service { } try { - closeSocket(); + closeSocket(closeServer, true); } catch (IOException ex) { Log.e(TAG, "CloseSocket error: " + ex); } + + if (mAcceptThread != null) { + try { + if (closeServer) { + mAcceptThread.shutdown(); + mAcceptThread.join(); + } + } catch (InterruptedException ex) { + Log.w(TAG, "mAcceptThread close error" + ex); + } finally { + mAcceptThread = null; + } + } + if (VERBOSE) Log.v(TAG, "Mas connection closed"); } @@ -795,7 +800,7 @@ public class BluetoothMasService extends Service { private void stopObexServerSession() { if (VERBOSE) Log.v(TAG, "Map Service stopObexServerSession "); - closeConnection(); + closeConnection(false); // Last obex transaction is finished, we start to listen for incoming // connection again @@ -822,11 +827,15 @@ public class BluetoothMasService extends Service { @Override public void run() { + if (mServerSocket == null) { + if (!initSocket()) { + closeService(); + return; + } + } + while (!stopped) { try { - if (mServerSocket == null) { - break; - } BluetoothSocket connSocket = mServerSocket.accept(); BluetoothDevice device = connSocket.getRemoteDevice(); @@ -875,9 +884,7 @@ public class BluetoothMasService extends Service { } stopped = true; // job done ,close this thread; } catch (IOException ex) { - if (stopped) { - break; - } + stopped = true; if (VERBOSE) Log.v(TAG, "Accept exception: " + ex.toString()); } @@ -888,14 +895,6 @@ public class BluetoothMasService extends Service { if (VERBOSE) Log.v(TAG, "AcceptThread shutdown for MAS id: " + mMasId); stopped = true; interrupt(); - if (mServerSocket != null) { - try { - mServerSocket.close(); - mServerSocket = null; - } catch (IOException e) { - Log.e(TAG, "Failed to close socket", e); - } - } } } } |