6

我正在尝试使用 A2DP 配置文件控制与设备的蓝牙连接。在 Android 的原生 Java 开发中,开发人员使用 BluetoothA2dp 类来建立连接。

Xamarin 中有一个名为相同的类 - BluetoothA2dp。但我似乎无法理解如何初始化它的实例,因为它没有构造函数。

如何在该类端口的帮助下创建连接?

4

2 回答 2

2

您不需要直接使用 BluetoothA2dp 类。根据Android文档...

BluetoothA2dp 是一个代理对象,用于通过 IPC 控制蓝牙 A2DP 服务。使用 getProfileProxy(Context, BluetoothProfile.ServiceListener, int) 获取 BluetoothA2dp 代理对象。

您应该使用BluetoothAdapter.GetProfileProxy来启动到 A2DP 代理对象的连接。

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

上面方法调用中的serviceListener参数需要是实现的类的实例,IBluetoothProfileServiceListener然后您可以在其中通过 OnServiceConnected 方法访问代理对象。

public void OnServiceConnected(ProfileType profile, IBluetoothProfile proxy)
{

}
于 2014-04-03T09:03:29.943 回答
-1

Xamarin 5.0 到 5.2 版本中提供了“BluetoothA2DP”类,请参阅链接

由于“BluetoothA2DP”是密封类,不能被继承。您只能通过其实例使用它。

您需要覆盖其“GetConnectionState”“GetDevicesMatchingConnectionStates”方法来连接特定设备。

也许,您能做的最好的事情就是尝试使用您自己的扩展方法 来扩展“BluetoothA2DP”的功能。

假设您的“BluetoothA2DP”类如下:

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

然后您自己的类将“BluetoothA2DP”类功能扩展为:

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

然后使用 ExtendedTest() 方法来利用“BluetoothA2DP”类。

希望这应该工作:)

您可以在此处参考完整的 API 文档。

于 2014-04-02T10:30:39.503 回答