title: Android connect to Bluetooth via Hands Free Protocol
body: <p>I would like to create an application which can connect to a Bluetooth Headset via the Hands Free Protocol (HFP). I followed the Android example and have now a <code>BluetoothSocket</code> with its <code>Input and OutputStream</code>. Below you see my read and write methods (read method is executed by another <code>Thread</code>)</p>
  
  <pre><code>public void read() {
      while (true) {
          Log.d("ME", "Waiting for data");
          try { // read until Exception is thrown
              numBytes = inStream.read(dataBuffer);
  
              String str = new String(dataBuffer,0,numBytes);
              msgHandler.obtainMessage(numBytes, str).sendToTarget();
          } catch (Exception e) {
              Log.d("ME", "Input stream was disconnected", e);
              break; // BluetoothDevice was disconnected =&gt; Exit
          }
     }
  }
  
  public void write(byte[] bytes) {
      try {
          outStream.write(bytes);
          outStream.flush();
          Log.e("ME", "Wrote: " + new String(bytes));
      } catch (IOException e) {
          Log.e("ME", "Error occurred when sending data", e);
      }
  }
  </code></pre>
  
  <p>When the connection is opened the Bluetooth headset sends <code>AT+BRSF=191</code> over the <code>InputStream</code>. I tried to response with <code>+BRSF:20\r</code> but here is my problem. After that the device does not send any other data over the <code>InputStream</code>. It does not come to an <code>Exception</code> - it's more like the device does not know how to responde to my message. Do I send the wrong data? I have all the information from <a href="https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=238193" rel="nofollow noreferrer">here</a>: (HF = Hands-Free Unit AG = Audio Gateway)</p>
  
  <p><a href="https://i.stack.imgur.com/Ow3KD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ow3KD.png" alt="enter image description here"></a></p>
  
  <p>Do you have any ideas what I did wrong? Have I missed something?</p>
  
  <p><strong>EDIT</strong>: These are my write calls:</p>
  
  <pre><code>write("+BRSF: 191\r");
  write("OK\r");
  </code></pre>