diff options
author | Danny Baumann <dannybaumann@web.de> | 2013-05-24 13:21:51 +0200 |
---|---|---|
committer | Danny Baumann <dannybaumann@web.de> | 2013-05-24 13:37:29 +0200 |
commit | bc965770a09f67904351291ba13dbefd78ea6092 (patch) | |
tree | c2408486c28d11efebef26ef57726d8430f8e408 | |
parent | 9bd3b2e3af27b3fb4bb78c7e7af9b1e1c3640253 (diff) | |
download | android_packages_apps_Bluetooth-bc965770a09f67904351291ba13dbefd78ea6092.tar.gz android_packages_apps_Bluetooth-bc965770a09f67904351291ba13dbefd78ea6092.tar.bz2 android_packages_apps_Bluetooth-bc965770a09f67904351291ba13dbefd78ea6092.zip |
Fix MAP accept threads sometimes spinning endlessly.
Randomly (under no obvious precondition), the MAP access threads spinned
endlessly after enabling BT, because mServerSocket.accept() always threw
IOExceptions, probably because the actual socket was already closed.
This change makes sure to exit the thread when that happens to not waste
CPU cycles and battery power.
This tracks similar changes done to the BluetoothPbapService and makes
sure to follow the (working) PBAP flow.
Change-Id: Iade905f0ea8829cce9666ddaebee7a8fd01f62b0
-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); - } - } } } } |