20

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.

We are working in Xamarin (monodroid), for Android platform.

I've found this stackoverflow question: Programmatically connect to paired Bluetooth device

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.

Update 1:

We have tried to connect using CreateInsecureRfcommSocketToServiceRecord method like this:

mmSocket = device.CreateInsecureRfcommSocketToServiceRecord(0000110A-0000-1000-8000-00805F9B34FB); mmSocket.Connect();

Upon a call to Connect, we are getting an error:

read failed, socket might closed or timeout, read ret: -1

Stack trace begins with:

Java.IO.IOException at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00062] in /Users/buil…

Update 2:

By the way, whene we try to connect via the native java test app using the approach by kcoppock, the connection code seems to work without an error, although the device isn't being connected as an A2DP headset.

The only programming way we have seen to be able to do it was this Google Play app, which proves that it is possible.

Community
  • 1
  • 1
Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174
  • One of the reasons could be that another part of your program is holding open the same file that the connection(extractor) was supposed to read off ? – SeahawksRdaBest Mar 14 '14 at 06:12
  • At the point of connection we only have an SPP connection via RFComm I think, but our main task is that we have the Android device connected via SSP and A2DP simultaniously. If we connect to SPP from code and connect to A2DP manually via Android settings, everything works, so this proves that technically we can have two profiles connected at the same time. – Maxim V. Pavlov Mar 14 '14 at 10:01
  • This might be a bandwidth issue. Check the baudrate of your specific device and how much you are streaming in audio. Obviously if you are coming close to the limit the rfcomm channel will fail. – SeahawksRdaBest Mar 14 '14 at 14:43

3 Answers3

1

Remember that Xamarin binds to native api so don't worry that something "relates to native ways" ;) Based on the anwser You referenced I wrote and tested the code below. I hope it will help You.

class btListener : Java.Lang.Object, IBluetoothProfileServiceListener
{
    public void OnServiceConnected([GeneratedEnum] ProfileType profile, IBluetoothProfile proxy)
    {
        String deviceName = "JABRA WAVE+";

        BluetoothDevice result = null;

        var devices = BluetoothAdapter.DefaultAdapter.BondedDevices;
        if (devices != null)
        {
            foreach (BluetoothDevice device in devices)
            {
                if (deviceName == device.Name)
                {
                    result = device;
                    break;
                }
            }
        }
        var connect = Java.Lang.Class.FromType(typeof(BluetoothA2dp)).GetDeclaredMethod("connect", Java.Lang.Class.FromType(typeof(BluetoothDevice)));
        connect.Invoke((Java.Lang.Object)proxy, result);
    }

    public void OnServiceDisconnected([GeneratedEnum] ProfileType profile)
    {
    }
}

Following code in e.g. OnCreate function:

btListener btReceiver = new btListener();
BluetoothAdapter.DefaultAdapter.GetProfileProxy(this, btReceiver, ProfileType.A2dp);

Just looked at the date.. but I'm posting answer anyway - maybe it's still going to help somebody

flota113
  • 79
  • 2
  • Curious about this, it looks like the `connect` method isn't meant to be used in apps, is there another way to go about this? – Tyler Hartwig Apr 07 '20 at 03:57
0

You may check this blog post. adapter.BondedDevices property in this link will return a list of paired devices.

Sreeraj
  • 2,306
  • 2
  • 18
  • 31
-1

There is a native java sample program and its analog in the Xamarin examples at : http://docs.xamarin.com/samples/BluetoothChat/

Tim
  • 20,184
  • 24
  • 117
  • 214
  • We did that only with SPP. SPP connects and works fine. What we can't do is connect via A2DP, but we need that. – Maxim V. Pavlov Feb 14 '14 at 15:47
  • Please change your question to add the additional limitation. i am not sure what that means relative to your requirements. – Tim Feb 14 '14 at 15:48
  • Sorry, Tim, my bad, I should have mentioned we were only able to connect via SPP and no other profile. We are sure device supports both since we can connect manually. – Maxim V. Pavlov Feb 14 '14 at 16:04