title: HC-06 send data to Android
body: <p>I'm working on an Arduino project with an Android device. But we have a tiny problem.</p>
  
  <p>We want to send PPG sensor values to the Android device and display it in the UI. We managed to connect with Bluetooth. But we can't receive data in the Android device.</p>
  
  <p>(We use an Arduino Uno board with an HC-06 Bluetooth module.)</p>
  
  <p>Arduino code:</p>
  
  <pre><code>#include &lt;SoftwareSerial.h&gt;
  #define pinRX 12 //rx pin num
  #define pinTX 13 //tx pin num 
  SoftwareSerial BTSerial(pinRX, pinTX);
  
  byte buffer[1024];
  int bufferPosition;
  const int analogInPin = A1;
  int outputValue = 0;
  
  void setup() {
    Serial.begin(9600);
    Serial.println("Hello!");
    BTSerial.begin(9600);
  }
  
  void loop() {
    //ppg sensor
    int sensorValue = analogRead(analogInPin);
    Serial.println(sensorValue);
    Serial.write(sensorValue);
    delay(1000);
  }
  </code></pre>
  
  <p>Android code:</p>
  
  <pre><code>//create new class for connect thread
  private class ConnectedThread extends Thread {
      private final InputStream mmInStream;
      private final OutputStream mmOutStream;
      //creation of the connect thread
      public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        try {
          //Create I/O streams for connection
          tmpIn = socket.getInputStream();
          tmpOut = socket.getOutputStream();
          Log.i("TEST", "TESTRTETE ::  Thread :: " + tmpIn);
        } catch (IOException e) {
        }
        mmInStream = tmpIn;
        mmOutStream = tmpOut;
      }
  
      public void run() {
        byte[] buffer = new byte[256];
        int bytes;
        int i;
        // Keep looping to listen for received messages
        while (true) {
          try {
            Log.i("TEST", "TESTRTETE ::  run() " + mmInStream.available());
            bytes = mmInStream.read(buffer);
            Log.i("TEST", "TESTRTETE ::  bytes = " + bytes);//read bytes from input buffer
            String readMessage = new String(buffer, 0, bytes);
            // Send the obtained bytes to the UI Activity via handler
            Log.i("TEST", "TESTRTETE ::  readMSG ::  " + readMessage + "");
            bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
          } catch (IOException e) {
            Log.i("TEST", "TESTRTETE :: error :: ");
            break;
          }
        }
      }
  </code></pre>
  
  <p>The thread runs well... But we don't know why it goes to IOException.</p>
  
  <pre><code>11-18 14:48:04.421 27950-27950/com.example.sonminhee.bttest I/TEST: TESTRTETE :: btSocket :: 
  11-18 14:48:05.461 27950-27950/com.example.sonminhee.bttest I/TEST: TESTRTETE :: thread.connect() :: 
  11-18 14:48:05.466 27950-27950/com.example.sonminhee.bttest I/TEST: TESTRTETE ::  Thread :: android.bluetooth.BluetoothInputStream@2ea790e7
  11-18 14:48:05.466 27950-27950/com.example.sonminhee.bttest I/TEST: TESTRTETE :: thread.start() :: 
  11-18 14:48:05.466 27950-9233/com.example.sonminhee.bttest I/TEST: TESTRTETE ::  run() 0
  </code></pre>
  
  <p>This is the Android log.</p>
  
  <p>There seems to be a problem with receiving data from Android.
  Please give me any solution. Thank you!</p>