/** Stop all threads */ public synchronized void stop() { if (DEBUG) { Log.i(TAG, "Synchronized stop()"); } if (mConnectThread != null) { mConnectThread.closeSocket(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.closeSocket(); mConnectedThread = null; } if (mSecureAcceptThread != null) { mSecureAcceptThread.closeSocket(); mSecureAcceptThread = null; } if (mInsecureAcceptThread != null) { mInsecureAcceptThread.closeSocket(); mInsecureAcceptThread = null; } setState(STATE_NONE); }
/** * Start the chat service. Specifically start AcceptThread to begin a session in listening * (server) mode. Called by the Activity onResume() */ public synchronized void start() { if (DEBUG) { Log.i(TAG, "Synchronized start()"); } // Cancel any thread attempting to make a connection if (mConnectThread != null) { mConnectThread.closeSocket(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.closeSocket(); mConnectedThread = null; } setState(STATE_LISTEN); // Start the thread to listen on a BluetoothServerSocket if (mSecureAcceptThread == null) { mSecureAcceptThread = new AcceptThread(true); mSecureAcceptThread.start(); } if (mInsecureAcceptThread == null) { mInsecureAcceptThread = new AcceptThread(false); mInsecureAcceptThread.start(); } }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected * @param socketType */ public synchronized void connected( BluetoothSocket socket, BluetoothDevice device, final String socketType) { if (DEBUG) { Log.i(TAG, "Synchronized connected, Socket Type:" + socketType); } // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.closeSocket(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.closeSocket(); mConnectedThread = null; } // Cancel the accept thread because we only want to connect to one device if (mSecureAcceptThread != null) { mSecureAcceptThread.closeSocket(); mSecureAcceptThread = null; } if (mInsecureAcceptThread != null) { mInsecureAcceptThread.closeSocket(); mInsecureAcceptThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket, socketType); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
/** * Start the ConnectThread to initiate a connection to a remote device. * * @param device The BluetoothDevice to connect * @param secure Socket Security type - Secure (true) , Insecure (false) */ public synchronized void connect(BluetoothDevice device, boolean secure) { if (DEBUG) { Log.i(TAG, "Synchronized connect to: " + device); } // Cancel any thread attempting to make a connection if (mState == STATE_CONNECTING) { if (mConnectThread != null) { mConnectThread.closeSocket(); mConnectThread = null; } } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.closeSocket(); mConnectedThread = null; } // Start the thread to connect with the given device mConnectThread = new ConnectThread(device, secure); mConnectThread.start(); setState(STATE_CONNECTING); }