Loading android/app/src/com/android/bluetooth/pbap/BluetoothPbapService.java +57 −41 Original line number Diff line number Diff line Loading @@ -297,10 +297,6 @@ public class BluetoothPbapService extends Service { super.onDestroy(); setState(BluetoothPbap.STATE_DISCONNECTED, BluetoothPbap.RESULT_CANCELED); if (mWakeLock != null) { mWakeLock.release(); mWakeLock = null; } closeService(); if(mSessionStatusHandler != null) { mSessionStatusHandler.removeCallbacksAndMessages(null); Loading @@ -326,11 +322,12 @@ public class BluetoothPbapService extends Service { private final boolean initSocket() { if (VERBOSE) Log.v(TAG, "Pbap Service initSocket"); boolean initSocketOK = true; boolean initSocketOK = false; final int CREATE_RETRY_TIME = 10; // It's possible that create will fail in some cases. retry for 10 times for (int i = 0; i < CREATE_RETRY_TIME && !mInterrupted; i++) { initSocketOK = true; try { // It is mandatory for PSE to support initiation of bonding and // encryption. Loading @@ -350,20 +347,24 @@ public class BluetoothPbapService extends Service { Log.w(TAG, "initServerSocket failed as BT is (being) turned off"); break; } synchronized (this) { try { if (VERBOSE) Log.v(TAG, "wait 300 ms"); Thread.sleep(300); } catch (InterruptedException e) { Log.e(TAG, "socketAcceptThread thread was interrupted (3)"); mInterrupted = true; } break; } } else { break; } } if (mInterrupted) { initSocketOK = false; // close server socket to avoid resource leakage closeServerSocket(); } if (initSocketOK) { if (VERBOSE) Log.v(TAG, "Succeed to create listening socket "); Loading @@ -373,21 +374,26 @@ public class BluetoothPbapService extends Service { return initSocketOK; } private final void closeSocket(boolean server, boolean accept) throws IOException { if (server == true) { // Stop the possible trying to init serverSocket mInterrupted = true; private final synchronized void closeServerSocket() { // exit SocketAcceptThread early if (mServerSocket != null) { try { // this will cause mServerSocket.accept() return early with IOException mServerSocket.close(); mServerSocket = null; } catch (IOException ex) { Log.e(TAG, "Close Server Socket error: " + ex); } } } if (accept == true) { private final synchronized void closeConnectionSocket() { if (mConnSocket != null) { try { mConnSocket.close(); mConnSocket = null; } catch (IOException e) { Log.e(TAG, "Close Connection Socket error: " + e.toString()); } } } Loading @@ -395,11 +401,9 @@ public class BluetoothPbapService extends Service { private final void closeService() { if (VERBOSE) Log.v(TAG, "Pbap Service closeService in"); try { closeSocket(true, true); } catch (IOException ex) { Log.e(TAG, "CloseSocket error: " + ex); } // exit initSocket early mInterrupted = true; closeServerSocket(); if (mAcceptThread != null) { try { Loading @@ -410,11 +414,19 @@ public class BluetoothPbapService extends Service { Log.w(TAG, "mAcceptThread close error" + ex); } } if (mWakeLock != null) { mWakeLock.release(); mWakeLock = null; } if (mServerSession != null) { mServerSession.close(); mServerSession = null; } closeConnectionSocket(); mHasStarted = false; if (mStartId != -1 && stopSelfResult(mStartId)) { if (VERBOSE) Log.v(TAG, "successfully stopped pbap service"); Loading Loading @@ -473,12 +485,8 @@ public class BluetoothPbapService extends Service { mAcceptThread = null; try { closeSocket(false, true); mConnSocket = null; } catch (IOException e) { Log.e(TAG, "closeSocket error: " + e.toString()); } closeConnectionSocket(); // Last obex transaction is finished, we start to listen for incoming // connection again if (mAdapter.isEnabled()) { Loading Loading @@ -516,9 +524,9 @@ public class BluetoothPbapService extends Service { @Override public void run() { BluetoothServerSocket serverSocket; if (mServerSocket == null) { if (!initSocket()) { closeService(); return; } } Loading @@ -526,10 +534,21 @@ public class BluetoothPbapService extends Service { while (!stopped) { try { if (VERBOSE) Log.v(TAG, "Accepting socket connection..."); mConnSocket = mServerSocket.accept(); serverSocket = mServerSocket; if (serverSocket == null) { Log.w(TAG, "mServerSocket is null"); break; } mConnSocket = serverSocket.accept(); if (VERBOSE) Log.v(TAG, "Accepted socket connection..."); synchronized (BluetoothPbapService.this) { if (mConnSocket == null) { Log.w(TAG, "mConnSocket is null"); break; } mRemoteDevice = mConnSocket.getRemoteDevice(); } if (mRemoteDevice == null) { Log.i(TAG, "getRemoteDevice() = null"); break; Loading Loading @@ -783,12 +802,9 @@ public class BluetoothPbapService extends Service { mServerSession.close(); mServerSession = null; } try { closeSocket(false, true); mConnSocket = null; } catch (IOException ex) { Log.e(TAG, "Caught the error: " + ex); } closeConnectionSocket(); setState(BluetoothPbap.STATE_DISCONNECTED, BluetoothPbap.RESULT_CANCELED); break; default: Loading Loading
android/app/src/com/android/bluetooth/pbap/BluetoothPbapService.java +57 −41 Original line number Diff line number Diff line Loading @@ -297,10 +297,6 @@ public class BluetoothPbapService extends Service { super.onDestroy(); setState(BluetoothPbap.STATE_DISCONNECTED, BluetoothPbap.RESULT_CANCELED); if (mWakeLock != null) { mWakeLock.release(); mWakeLock = null; } closeService(); if(mSessionStatusHandler != null) { mSessionStatusHandler.removeCallbacksAndMessages(null); Loading @@ -326,11 +322,12 @@ public class BluetoothPbapService extends Service { private final boolean initSocket() { if (VERBOSE) Log.v(TAG, "Pbap Service initSocket"); boolean initSocketOK = true; boolean initSocketOK = false; final int CREATE_RETRY_TIME = 10; // It's possible that create will fail in some cases. retry for 10 times for (int i = 0; i < CREATE_RETRY_TIME && !mInterrupted; i++) { initSocketOK = true; try { // It is mandatory for PSE to support initiation of bonding and // encryption. Loading @@ -350,20 +347,24 @@ public class BluetoothPbapService extends Service { Log.w(TAG, "initServerSocket failed as BT is (being) turned off"); break; } synchronized (this) { try { if (VERBOSE) Log.v(TAG, "wait 300 ms"); Thread.sleep(300); } catch (InterruptedException e) { Log.e(TAG, "socketAcceptThread thread was interrupted (3)"); mInterrupted = true; } break; } } else { break; } } if (mInterrupted) { initSocketOK = false; // close server socket to avoid resource leakage closeServerSocket(); } if (initSocketOK) { if (VERBOSE) Log.v(TAG, "Succeed to create listening socket "); Loading @@ -373,21 +374,26 @@ public class BluetoothPbapService extends Service { return initSocketOK; } private final void closeSocket(boolean server, boolean accept) throws IOException { if (server == true) { // Stop the possible trying to init serverSocket mInterrupted = true; private final synchronized void closeServerSocket() { // exit SocketAcceptThread early if (mServerSocket != null) { try { // this will cause mServerSocket.accept() return early with IOException mServerSocket.close(); mServerSocket = null; } catch (IOException ex) { Log.e(TAG, "Close Server Socket error: " + ex); } } } if (accept == true) { private final synchronized void closeConnectionSocket() { if (mConnSocket != null) { try { mConnSocket.close(); mConnSocket = null; } catch (IOException e) { Log.e(TAG, "Close Connection Socket error: " + e.toString()); } } } Loading @@ -395,11 +401,9 @@ public class BluetoothPbapService extends Service { private final void closeService() { if (VERBOSE) Log.v(TAG, "Pbap Service closeService in"); try { closeSocket(true, true); } catch (IOException ex) { Log.e(TAG, "CloseSocket error: " + ex); } // exit initSocket early mInterrupted = true; closeServerSocket(); if (mAcceptThread != null) { try { Loading @@ -410,11 +414,19 @@ public class BluetoothPbapService extends Service { Log.w(TAG, "mAcceptThread close error" + ex); } } if (mWakeLock != null) { mWakeLock.release(); mWakeLock = null; } if (mServerSession != null) { mServerSession.close(); mServerSession = null; } closeConnectionSocket(); mHasStarted = false; if (mStartId != -1 && stopSelfResult(mStartId)) { if (VERBOSE) Log.v(TAG, "successfully stopped pbap service"); Loading Loading @@ -473,12 +485,8 @@ public class BluetoothPbapService extends Service { mAcceptThread = null; try { closeSocket(false, true); mConnSocket = null; } catch (IOException e) { Log.e(TAG, "closeSocket error: " + e.toString()); } closeConnectionSocket(); // Last obex transaction is finished, we start to listen for incoming // connection again if (mAdapter.isEnabled()) { Loading Loading @@ -516,9 +524,9 @@ public class BluetoothPbapService extends Service { @Override public void run() { BluetoothServerSocket serverSocket; if (mServerSocket == null) { if (!initSocket()) { closeService(); return; } } Loading @@ -526,10 +534,21 @@ public class BluetoothPbapService extends Service { while (!stopped) { try { if (VERBOSE) Log.v(TAG, "Accepting socket connection..."); mConnSocket = mServerSocket.accept(); serverSocket = mServerSocket; if (serverSocket == null) { Log.w(TAG, "mServerSocket is null"); break; } mConnSocket = serverSocket.accept(); if (VERBOSE) Log.v(TAG, "Accepted socket connection..."); synchronized (BluetoothPbapService.this) { if (mConnSocket == null) { Log.w(TAG, "mConnSocket is null"); break; } mRemoteDevice = mConnSocket.getRemoteDevice(); } if (mRemoteDevice == null) { Log.i(TAG, "getRemoteDevice() = null"); break; Loading Loading @@ -783,12 +802,9 @@ public class BluetoothPbapService extends Service { mServerSession.close(); mServerSession = null; } try { closeSocket(false, true); mConnSocket = null; } catch (IOException ex) { Log.e(TAG, "Caught the error: " + ex); } closeConnectionSocket(); setState(BluetoothPbap.STATE_DISCONNECTED, BluetoothPbap.RESULT_CANCELED); break; default: Loading