6

I am trying to controll Bluetooth connection to a device using the A2DP profile. In native Java Development for Android, devs make use of BluetoothA2dp class to make a connection.

There is a class called the same in Xamarin - BluetoothA2dp. But I can't seem to understand how to initialize an instance of it, since it has no constructor.

How can I create a connection with the help of that class port?

Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174

2 Answers2

2

You don't need to use the BluetoothA2dp class directly. As per the Android documentation...

BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP Service via IPC. Use getProfileProxy(Context, BluetoothProfile.ServiceListener, int) to get the BluetoothA2dp proxy object.

You should use BluetoothAdapter.GetProfileProxy to initiate a connection to the A2DP proxy object.

BluetoothAdapter.DefaultAdapter.GetProfileProxy(this, serviceListener, ProfileType.A2dp);

The serviceListener argument in the method call above needs to be an instance of a class which implements IBluetoothProfileServiceListener, in which you can then access the proxy object through the OnServiceConnected method.

public void OnServiceConnected(ProfileType profile, IBluetoothProfile proxy)
{

}
Tom Bowers
  • 4,951
  • 3
  • 30
  • 43
  • Hi, thank you. Well, we've "signed up for" `OnServiceConnected` and when we manually connect A2DP profile (to a remote device) from a system bluetooth connection activity, the event isn't getting fired. At least in theory, when should the method `OnServiceConnected` be getting executed? – Maxim V. Pavlov Apr 03 '14 at 12:59
  • `OnServiceConnected` is called when the AD2P proxy object is connected to the service, not when devices are connected. So it will be called soon after the call to `GetProfileProxy`. I've tested a quick activity with the code I posted above and it calls the method as expected. I'd need to see some code to advise really. – Tom Bowers Apr 03 '14 at 13:54
  • Although a dev trying this is saying that on his machine OnServiceConnected isn't beig called, I think this answers the question about how to use this class, so I am marking it as a correct one. Thank you. – Maxim V. Pavlov Apr 05 '14 at 10:03
-1

The class "BluetoothA2DP" has been provided in Xamarin version 5.0 to 5.2, refer to link

Since "BluetoothA2DP" is sealed class, it cannot be inherited. You can only used it through its instances.

You need to override its "GetConnectionState" or "GetDevicesMatchingConnectionStates" methods to connect particular device.

Probably, the best you can do is try to extend the functionality of "BluetoothA2DP" using your own extension method.

Suppose your "BluetoothA2DP" class be like :

public sealed class BluetoothA2dp : Java.Lang.Object,   IBluetoothProfile, IDisposable {
    public ProfileState GetConnectionState (BluetoothDevice device)  {
        return some_device_configs;
    }
}

Then your own class that extends "BluetoothA2DP" class functionality as :

public static class MyClassExtender
{
    public static void ExtendedTest(this BluetoothA2DP instance)
    {
        instance.GetConnectionState();        
    }
}

Then use ExtendedTest() method to utilize "BluetoothA2DP" class.

Hope this should work :)

You can refer here for full API Docs.

Mazzu
  • 2,799
  • 20
  • 30
  • Hi. Thanks. Ok, I've added an extension method as you demonstarated. If I had an isntance of the BluetoothA2DP I could be calling ExtendedTest() from it. But since I don't have an instance of the class, I can't access the ExtendedTest() method using the BluetoothA2dp as a static class. How can I call the ExtendedTest() correctly? – Maxim V. Pavlov Apr 02 '14 at 16:39
  • You need to create instance of "MyClassExtender" class and through it call the "ExtendedTest()" – Mazzu Apr 03 '14 at 07:02
  • 1
    This advice is incorrect. You can't 'override' `GetConnectionState` as it's not an abstract or virtual method, and the class is sealed anyway, so can't be inherited from. Also, `MyClassExtender` above is a static class, so can't be instantiated as you've suggested. The syntax above is for creating an extension method to the `BluetoothA2DP` class, of which you would need an instance. See my answer for how to get access to the A2DP proxy object. – Tom Bowers Apr 03 '14 at 09:11