Code example for Handler

Methods: obtainMessagesendMessage

0
    private synchronized void setState(int state) {
        if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
        mState = state;
 
        // Give the new state to the Handler so the UI Activity can update 
        mHandler.obtainMessage(MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
    } 
 
    /** 
     * Return the current connection state. */ 
    public synchronized int getState() { 
        return mState;
    } 
 
    /** 
     * Start the chat service. Specifically start AcceptThread to begin a 
     * session in listening (server) mode. Called by the Activity onResume() */ 
    public synchronized void start(boolean secure) {
        if (D) Log.d(TAG, "start secure: " + secure + UUID.randomUUID() + " - " + UUID.randomUUID());
 
        // Cancel any thread attempting to make a connection 
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
 
        // Cancel any thread currently running a connection 
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
 
        setState(STATE_LISTEN);
 
        // Start the thread to listen on a BluetoothServerSocket 
        if (secure && mSecureAcceptThread == null) {
            mSecureAcceptThread = new AcceptThread(true);
            mSecureAcceptThread.start();
        } 
        if (!secure && mInsecureAcceptThread == null) {
            mInsecureAcceptThread = new AcceptThread(false);
            mInsecureAcceptThread.start();
        } 
    } 
 
    /** 
     * 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 (D) Log.d(TAG, "connect to: " + device);
 
        // Cancel any thread attempting to make a connection 
        if (mState == STATE_CONNECTING) {
            if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
        } 
 
        // Cancel any thread currently running a connection 
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
 
        // Start the thread to connect with the given device 
        mConnectThread = new ConnectThread(device, secure);
        mConnectThread.start();
        setState(STATE_CONNECTING);
    } 
 
    /** 
     * 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 
     */ 
    public synchronized void connected(BluetoothSocket socket, BluetoothDevice
            device, final String socketType) {
        if (D) Log.d(TAG, "connected, Socket Type: " + socketType);
 
        // Cancel the thread that completed the connection 
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
 
        // Cancel any thread currently running a connection 
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
 
        // Cancel the accept thread because we only want to connect to one device 
        if (mSecureAcceptThread != null) {
            mSecureAcceptThread.cancel();
            mSecureAcceptThread = null;
        } 
        if (mInsecureAcceptThread != null) {
            mInsecureAcceptThread.cancel();
            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);
    } 
 
    /**