androidbluetoothxamarin.androidxamarina2dp

How you use BluetoothA2dp class in Xamarin


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?


Solution

  • 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)
    {
    
    }