title: Sending received message back to UI activity, but app stuck
body: <pre><code>static class BluetoothInHandler extends Handler {
          private final WeakReference&lt;Bluetooth_dataDisplay&gt; mActivity;
  
          BluetoothInHandler(Bluetooth_dataDisplay activity) {
              mActivity = new WeakReference&lt;&gt;(activity);
          }
  
          @Override
          public void handleMessage(Message msg) {
              final Bluetooth_dataDisplay thizz = mActivity.get();
              if (thizz == null) return;
              if (msg.what == thizz.handlerState) {
                 String readMessage = (String) msg.obj;
                 thizz.myLabel.setText(readMessage);
  
  
             }
          }//end of handle message
  
  
   }//end of Bluetoothin handler
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.bluetooth_datadisplay);
          myLabel = (TextView)findViewById(R.id.label);
  
          mMyHandler = new BluetoothInHandler(this);
  
  
  
      }//end oncreate
  
  
  
  @Override
      public void onResume() {
          super.onResume();  
          String MAC = getIntent().getStringExtra("MAC");
          mAdapter = BluetoothAdapter.getDefaultAdapter();
          BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC);
          ConnectingThread t = new ConnectingThread(bluetoothDevice);
          t.start();
  
      }
  
  //Setting Up a Connecting Client
  private class ConnectingThread extends Thread {
      OutputStream mmOutputStream;
      InputStream mmInputStream;
      StringBuilder recDataString = new StringBuilder();
     // private final BluetoothSocket bluetoothSocket;
      private final BluetoothDevice bluetoothDevice;
      final Handler handler = new Handler();
      public ConnectingThread(BluetoothDevice device) {
  
          BluetoothSocket temp = null;
          bluetoothDevice = device;
  
  
          // Get a BluetoothSocket to connect with the given BluetoothDevice
          try {
  
              temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
          } catch (IOException e) {
              e.printStackTrace();
          }
          bluetoothSocket = temp;
      }
  
      public void run() {
          // Cancel any discovery as it will slow down the connection
  
          mAdapter.cancelDiscovery();
  
  
          try {
  
              // This will block until it succeeds in connecting to the device
              // through the bluetoothSocket or throws an exception
              bluetoothSocket.connect();
              Log.e("bluetooth socket",".connect");
              mmOutputStream = bluetoothSocket.getOutputStream();
              mmInputStream = bluetoothSocket.getInputStream();
  
              //beginListenForData();
              Bluetooth_dataDisplay.this.runOnUiThread(new Runnable() {
  
                  @Override
                  public void run() {
                      Toast.makeText(Bluetooth_dataDisplay.this, "Connected with Device!", Toast.LENGTH_SHORT).show();
  
                  }
              });
  
            handler.post(new Runnable() {
  
                 public void run() {
  
                     byte[] buffer = new byte[1024];
                     int bytes;
  
                     // Keep looping to listen for received messages
                     while (true) {
                         try {
                             bytes = mmInputStream.read(buffer);          
                             String readMessage = new String(buffer, 0, bytes);
                             // Send the obtained bytes to the UI Activity via handler
                             mMyHandler.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
                         } catch (IOException e) {
                             break;
                         }
                     }
                 }
             });
              //beginListenForData();
              //Log.e("begin", "begindata");
  
          } catch (IOException connectException) {
              connectException.printStackTrace();
  
              try {
                  bluetoothSocket.close();
                  Log.e("click6", "blueclose");
              } catch (IOException closeException) {
                  closeException.printStackTrace();
              }
          }
  
      }
  </code></pre>
  
  <p>Trying to send data from thread back to UI activity but my app just stuck. :( I don't know what wrong with it. Please help. No error and no crashes of app. I had tried runnableUIthread as well. It didn't work too. So I don't know what to do now. </p>
  
  <p><a href="https://i.stack.imgur.com/IaMBF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IaMBF.jpg" alt="log.e(test,handler)"></a> </p>