I'm making a app which can send a character ("B") to activate microcontroller then accept the result data from it. The data will be numbers 0-100. However I'm stuck in converting stream data to string.
I've tried the codes in comment but the process ended up being looping forever.
Here is my codes :
public Bluetooth(startover so, Handler handler, ProgressDialog pd) {
    // TODO Auto-generated constructor stub
    this.handler=handler;
    activity=so;
    this.pd=pd;
}
@Override
public void run() {
// TODO Auto-generated method stub
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
   try 
   {
       btSocket =device.createRfcommSocketToServiceRecord(MY_UUID);
       //Toast.makeText(this, "Bluetooth_socket is created", Toast.LENGTH_LONG).show();
   } 
   catch (Exception e) 
   {
       Log.e(TAG, "ON RESUME: Socket creation failed.", e);
   }
    try
   {
    btSocket.connect();
    Log.e(TAG, "ON RESUME: BT connection established, datatransfer link open.");
   //Toast.makeText(this, "Bluetooth_socket is connected", Toast.LENGTH_LONG).show();
    } 
   catch (Exception e) 
    {              
    try 
    {
    btSocket.close();
            } 
            catch (IOException e2)
            {
            Log.e(TAG, "ON RESUME: Unable to close socket during connection failure", e2);
           }
            }
message = "B";
sendMessage(message);
InputStream mInput=null;
try {
mInput = btSocket.getInputStream();
System.out.println("Input Open");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!Thread.currentThread().isInterrupted() && mBluetoothAdapter.isEnabled()) {
   try { 
     int bytesAvailable = mInput.available();                       
     if(bytesAvailable > 0) {
     System.out.println("Pesan Diterima");
     BufferedReader r = new BufferedReader(new InputStreamReader(mInput));
     StringBuilder total = new StringBuilder(bytesAvailable);
     String line;
     //BACA LEBIH DARI 1 KARAKTER
     //while ((line = r.readLine()) != null) {
       //  total.append(line);
     //}
     final String message=total.toString();
     handler.post(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            pd.dismiss();
            System.out.println("Turn "+message);
            activity.startActivity(new Intent(activity.getApplicationContext(),result.class).putExtra("data", message));
        }
    });
     break;
     }
   }
   catch (IOException ex) {
   }
}
super.run();
}   
private void sendMessage (String message) {
byte[] send = message.getBytes();
try {
    OutputStream OutStream = btSocket.getOutputStream();
    OutStream.write(send);
    System.out.println("Send "+message);
}
catch (IOException e) {
       Log.e(TAG, "Exception during write", e);
}
   }
}
