@SuppressWarnings("unchecked") @Test public void testRun_Handshake() throws Exception { mBluetoothServerThread.setHandshakeRequired(true); Field mSocketIoThreadsField = mBluetoothServerThread.getClass() .getDeclaredField("mSocketIoThreads"); mSocketIoThreadsField.setAccessible(true); when(mMockBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(myServerName, myUUID)) .thenReturn(mMockBluetoothServerSocket); when(mMockBluetoothServerSocket.accept()).thenAnswer(new Answer<BluetoothSocket>() { @Override public BluetoothSocket answer(InvocationOnMock invocation) throws InterruptedException, IllegalAccessException { mStopThreadField.set(mBluetoothServerThread, true); return mMockBluetoothSocket; } }); when(mMockBluetoothSocket.getInputStream()).thenReturn(mMockInputStream); when(mMockInputStream.read()).thenAnswer(new Answer<byte[]>() { @Override public byte[] answer(InvocationOnMock invocation) throws InterruptedException, IllegalAccessException { return "some text".getBytes(); } }); Thread service = new Thread(new Runnable() { @Override public void run() { mBluetoothServerThread.run(); } }); service.start(); service.join(MAX_TIMEOUT); CopyOnWriteArrayList<BluetoothSocketIoThread> mSocketIoThreads = (CopyOnWriteArrayList<BluetoothSocketIoThread>) mSocketIoThreadsField.get(mBluetoothServerThread); BluetoothSocketIoThread handshakeThread = mSocketIoThreads.get(0); assertThat("The handshake thread instance has the proper socket is set", handshakeThread.getSocket(), is(mMockBluetoothSocket)); verify(mMockListener, never()).onIncomingConnectionConnected(any(BluetoothSocket.class), any(PeerProperties.class)); verify(mMockListener, times(1)).onServerStopped(); }
@Override public void run() { Log.d(TAG, "Entering thread"); while (!mStopThread) { mBluetoothServerSocket = createServerSocket(); if (mBluetoothServerSocket != null && !mStopThread) { Log.i(TAG, "Waiting for incoming connections... Server socket = " + BluetoothUtils.portAndTypeToString(mBluetoothServerSocket)); BluetoothSocket bluetoothSocket = acceptIncomingSocket(); if (bluetoothSocket != null) { if (mHandshakeRequired) { doHandshake(bluetoothSocket); } else { String bluetoothMacAddress = BluetoothUtils.getBluetoothMacAddressFromSocket(bluetoothSocket); if (BluetoothUtils.isValidBluetoothMacAddress(bluetoothMacAddress)) { PeerProperties peerProperties = new PeerProperties(bluetoothMacAddress); mListener.onIncomingConnectionConnected(bluetoothSocket, peerProperties); } else { String errorMessage = "Invalid Bluetooth MAC address: " + bluetoothMacAddress; Log.e(TAG, errorMessage); mListener.onIncomingConnectionFailed(errorMessage); } } } else if (!mStopThread) { Log.e(TAG, "Socket is null"); mListener.onIncomingConnectionFailed("Socket is null"); mStopThread = true; } } closeBluetoothServerSocket(); } Log.d(TAG, "Exiting thread"); mListener.onServerStopped(); }
BluetoothServerThread extends AbstractBluetoothThread implements BluetoothSocketIoThread.Listener { @Override public void run() { Log.d(TAG, "Entering thread"); while (!mStopThread) { mBluetoothServerSocket = createServerSocket(); if (mBluetoothServerSocket != null && !mStopThread) { Log.i(TAG, "Waiting for incoming connections... Server socket = " + BluetoothUtils.portAndTypeToString(mBluetoothServerSocket)); BluetoothSocket bluetoothSocket = acceptIncomingSocket(); if (bluetoothSocket != null) { if (mHandshakeRequired) { doHandshake(bluetoothSocket); } else { String bluetoothMacAddress = BluetoothUtils.getBluetoothMacAddressFromSocket(bluetoothSocket); if (BluetoothUtils.isValidBluetoothMacAddress(bluetoothMacAddress)) { PeerProperties peerProperties = new PeerProperties(bluetoothMacAddress); mListener.onIncomingConnectionConnected(bluetoothSocket, peerProperties); } else { String errorMessage = "Invalid Bluetooth MAC address: " + bluetoothMacAddress; Log.e(TAG, errorMessage); mListener.onIncomingConnectionFailed(errorMessage); } } } else if (!mStopThread) { Log.e(TAG, "Socket is null"); mListener.onIncomingConnectionFailed("Socket is null"); mStopThread = true; } } closeBluetoothServerSocket(); } Log.d(TAG, "Exiting thread"); mListener.onServerStopped(); } }
BluetoothServerThread extends AbstractBluetoothThread implements BluetoothSocketIoThread.Listener { @Override public void run() { Log.d(TAG, "Entering thread"); while (!mStopThread) { mBluetoothServerSocket = createServerSocket(); if (mBluetoothServerSocket != null && !mStopThread) { Log.i(TAG, "Waiting for incoming connections... Server socket = " + BluetoothUtils.portAndTypeToString(mBluetoothServerSocket)); BluetoothSocket bluetoothSocket = acceptIncomingSocket(); if (bluetoothSocket != null) { if (mHandshakeRequired) { doHandshake(bluetoothSocket); } else { String bluetoothMacAddress = BluetoothUtils.getBluetoothMacAddressFromSocket(bluetoothSocket); if (BluetoothUtils.isValidBluetoothMacAddress(bluetoothMacAddress)) { PeerProperties peerProperties = new PeerProperties(bluetoothMacAddress); mListener.onIncomingConnectionConnected(bluetoothSocket, peerProperties); } else { String errorMessage = "Invalid Bluetooth MAC address: " + bluetoothMacAddress; Log.e(TAG, errorMessage); mListener.onIncomingConnectionFailed(errorMessage); } } } else if (!mStopThread) { Log.e(TAG, "Socket is null"); mListener.onIncomingConnectionFailed("Socket is null"); mStopThread = true; } } closeBluetoothServerSocket(); } Log.d(TAG, "Exiting thread"); mListener.onServerStopped(); } BluetoothServerThread(
            Listener listener, BluetoothAdapter bluetoothAdapter,
            UUID serviceRecordUuid, String myBluetoothName, String myIdentityString); }
BluetoothServerThread extends AbstractBluetoothThread implements BluetoothSocketIoThread.Listener { @Override public void run() { Log.d(TAG, "Entering thread"); while (!mStopThread) { mBluetoothServerSocket = createServerSocket(); if (mBluetoothServerSocket != null && !mStopThread) { Log.i(TAG, "Waiting for incoming connections... Server socket = " + BluetoothUtils.portAndTypeToString(mBluetoothServerSocket)); BluetoothSocket bluetoothSocket = acceptIncomingSocket(); if (bluetoothSocket != null) { if (mHandshakeRequired) { doHandshake(bluetoothSocket); } else { String bluetoothMacAddress = BluetoothUtils.getBluetoothMacAddressFromSocket(bluetoothSocket); if (BluetoothUtils.isValidBluetoothMacAddress(bluetoothMacAddress)) { PeerProperties peerProperties = new PeerProperties(bluetoothMacAddress); mListener.onIncomingConnectionConnected(bluetoothSocket, peerProperties); } else { String errorMessage = "Invalid Bluetooth MAC address: " + bluetoothMacAddress; Log.e(TAG, errorMessage); mListener.onIncomingConnectionFailed(errorMessage); } } } else if (!mStopThread) { Log.e(TAG, "Socket is null"); mListener.onIncomingConnectionFailed("Socket is null"); mStopThread = true; } } closeBluetoothServerSocket(); } Log.d(TAG, "Exiting thread"); mListener.onServerStopped(); } 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 void run() { Log.d(TAG, "Entering thread"); while (!mStopThread) { mBluetoothServerSocket = createServerSocket(); if (mBluetoothServerSocket != null && !mStopThread) { Log.i(TAG, "Waiting for incoming connections... Server socket = " + BluetoothUtils.portAndTypeToString(mBluetoothServerSocket)); BluetoothSocket bluetoothSocket = acceptIncomingSocket(); if (bluetoothSocket != null) { if (mHandshakeRequired) { doHandshake(bluetoothSocket); } else { String bluetoothMacAddress = BluetoothUtils.getBluetoothMacAddressFromSocket(bluetoothSocket); if (BluetoothUtils.isValidBluetoothMacAddress(bluetoothMacAddress)) { PeerProperties peerProperties = new PeerProperties(bluetoothMacAddress); mListener.onIncomingConnectionConnected(bluetoothSocket, peerProperties); } else { String errorMessage = "Invalid Bluetooth MAC address: " + bluetoothMacAddress; Log.e(TAG, errorMessage); mListener.onIncomingConnectionFailed(errorMessage); } } } else if (!mStopThread) { Log.e(TAG, "Socket is null"); mListener.onIncomingConnectionFailed("Socket is null"); mStopThread = true; } } closeBluetoothServerSocket(); } Log.d(TAG, "Exiting thread"); mListener.onServerStopped(); } 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);  }