@Test public void testShutdown_exception() throws Exception { Field mBluetoothServerSocketField = mBluetoothServerThread.getClass() .getDeclaredField("mBluetoothServerSocket"); mBluetoothServerSocketField.setAccessible(true); mBluetoothServerSocketField.set(mBluetoothServerThread, mMockBluetoothServerSocket); doThrow(IOException.class).when( mMockBluetoothServerSocket).close(); Field mSocketIoThreadsField = mBluetoothServerThread.getClass() .getDeclaredField("mSocketIoThreads"); mSocketIoThreadsField.setAccessible(true); CopyOnWriteArrayList<BluetoothSocketIoThread> mySocketIoThreads = new CopyOnWriteArrayList<>(); mSocketIoThreadsField.set(mBluetoothServerThread, mySocketIoThreads); mySocketIoThreads.add(mMockBluetoothSocketIoThread); mBluetoothServerThread.shutdown(); verify(mMockBluetoothSocketIoThread, times(1)).close(true, true); assertThat("The list of IO threads is cleared", mySocketIoThreads.isEmpty(), is(true)); }
@Override public synchronized void shutdown() { Log.d(TAG, "shutdown"); mStopThread = true; final BluetoothServerSocket bluetoothServerSocket = mBluetoothServerSocket; if (bluetoothServerSocket != null) { try { bluetoothServerSocket.close(); } catch (IOException | NullPointerException e) { Log.e(TAG, "shutdown: Failed to close the Bluetooth server socket: " + e.getMessage(), e); } } for (BluetoothSocketIoThread thread : mSocketIoThreads) { if (thread != null) { thread.close(true, true); } } mSocketIoThreads.clear(); }
BluetoothServerThread extends AbstractBluetoothThread implements BluetoothSocketIoThread.Listener { @Override public synchronized void shutdown() { Log.d(TAG, "shutdown"); mStopThread = true; final BluetoothServerSocket bluetoothServerSocket = mBluetoothServerSocket; if (bluetoothServerSocket != null) { try { bluetoothServerSocket.close(); } catch (IOException | NullPointerException e) { Log.e(TAG, "shutdown: Failed to close the Bluetooth server socket: " + e.getMessage(), e); } } for (BluetoothSocketIoThread thread : mSocketIoThreads) { if (thread != null) { thread.close(true, true); } } mSocketIoThreads.clear(); } }
BluetoothServerThread extends AbstractBluetoothThread implements BluetoothSocketIoThread.Listener { @Override public synchronized void shutdown() { Log.d(TAG, "shutdown"); mStopThread = true; final BluetoothServerSocket bluetoothServerSocket = mBluetoothServerSocket; if (bluetoothServerSocket != null) { try { bluetoothServerSocket.close(); } catch (IOException | NullPointerException e) { Log.e(TAG, "shutdown: Failed to close the Bluetooth server socket: " + e.getMessage(), e); } } for (BluetoothSocketIoThread thread : mSocketIoThreads) { if (thread != null) { thread.close(true, true); } } mSocketIoThreads.clear(); } BluetoothServerThread(
            Listener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString); }
BluetoothServerThread extends AbstractBluetoothThread implements BluetoothSocketIoThread.Listener { @Override public synchronized void shutdown() { Log.d(TAG, "shutdown"); mStopThread = true; final BluetoothServerSocket bluetoothServerSocket = mBluetoothServerSocket; if (bluetoothServerSocket != null) { try { bluetoothServerSocket.close(); } catch (IOException | NullPointerException e) { Log.e(TAG, "shutdown: Failed to close the Bluetooth server socket: " + e.getMessage(), e); } } for (BluetoothSocketIoThread thread : mSocketIoThreads) { if (thread != null) { thread.close(true, true); } } mSocketIoThreads.clear(); } BluetoothServerThread(
            Listener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString); @Override void run(); @Override synchronized void shutdown(); @Override void onBytesRead(byte[] bytes, int size, BluetoothSocketIoThread who); @Override void onBytesWritten(byte[] bytes, int size, BluetoothSocketIoThread who); @Override void onDisconnected(String reason, BluetoothSocketIoThread who); }
BluetoothServerThread extends AbstractBluetoothThread implements BluetoothSocketIoThread.Listener { @Override public synchronized void shutdown() { Log.d(TAG, "shutdown"); mStopThread = true; final BluetoothServerSocket bluetoothServerSocket = mBluetoothServerSocket; if (bluetoothServerSocket != null) { try { bluetoothServerSocket.close(); } catch (IOException | NullPointerException e) { Log.e(TAG, "shutdown: Failed to close the Bluetooth server socket: " + e.getMessage(), e); } } for (BluetoothSocketIoThread thread : mSocketIoThreads) { if (thread != null) { thread.close(true, true); } } mSocketIoThreads.clear(); } BluetoothServerThread(
            Listener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString); @Override void run(); @Override synchronized void shutdown(); @Override void onBytesRead(byte[] bytes, int size, BluetoothSocketIoThread who); @Override void onBytesWritten(byte[] bytes, int size, BluetoothSocketIoThread who); @Override void onDisconnected(String reason, BluetoothSocketIoThread who);  }