title: Connect to paired bluetooth device from Xamarin on Android
body: <p>We need our application to be able to connect to a paired bluetooth device automatically when an application starts via A2DP or Hands Free Profile.</p>
  
  <p>We are working in Xamarin (monodroid), for Android platform.</p>
  
  <p>I've found this stackoverflow question: <a href="https://stackoverflow.com/questions/5171248/programmatically-connect-to-paired-bluetooth-device">Programmatically connect to paired Bluetooth device</a></p>
  
  <p>But it relates to native ways of achieving this (see answer by kcoppock). I'd like to know if there is a way to achieve this via Xamarin. We can connect to SPP endpoint since it's an RFCOMM based connection, but we need that and the audio connection, so we are loking for a way to connect to A2DP.</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>We have tried to connect using <code>CreateInsecureRfcommSocketToServiceRecord</code> method like this:</p>
  
  <p><code>
  mmSocket = device.CreateInsecureRfcommSocketToServiceRecord(0000110A-0000-1000-8000-00805F9B34FB);
  mmSocket.Connect();
  </code></p>
  
  <p>Upon a call to Connect, we are getting an error:</p>
  
  <p><code>read failed, socket might closed or timeout, read ret: -1</code></p>
  
  <p>Stack trace begins with:</p>
  
  <p><code>Java.IO.IOException at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00062] in /Users/buil…</code></p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>By the way, whene we try to connect via the native java test app using <a href="https://github.com/kcoppock/bluetooth-a2dp" rel="nofollow noreferrer">the approach by kcoppock</a>, the connection code seems to work without an error, although the device isn't being connected as an A2DP headset.</p>
  
  <p><strong>The only programming way we have seen to be able to do it</strong> was <a href="https://play.google.com/store/apps/details?id=org.myklos.btautoconnect&amp;hl=uk" rel="nofollow noreferrer">this Google Play app</a>, which proves that it is possible.</p>