如何在xamarin上获取蓝牙a2dp关联事件?

问题描述

我有一个xamarin项目(API 28,即将成为29),并且我需要捕获蓝牙a2dp设备的事件。

我有一个带有以下意图过滤器的广播接收器:

IntentFilter bluetoothFilter = new IntentFilter();
bluetoothFilter.AddAction(BluetoothDevice.ActionAclConnected);
bluetoothFilter.AddAction(BluetoothDevice.ActionAclDisconnectRequested);
bluetoothFilter.AddAction(BluetoothDevice.ActionAclDisconnected);
var btReciever = new BluetoothReceiver();
this.RegisterReceiver(btReciever,bluetoothFilter);

在清单上,我获得了以下许可:

<uses-permission android:name="android.permission.BLUETOOTH" />

在Receiver.OnReceive中,我得到了以下代码:

public override void OnReceive(Context context,Intent intent) {
    String action = intent.Action;
    BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);

我有一个开关:

switch (action)
{
    case BluetoothDevice.ActionFound:
        Android.Util.Log.Debug(TAG,"Device Found");
        //Device found
        break;
    case BluetoothDevice.ActionAclConnected:
        Android.Util.Log.Debug(TAG,"Device Connected");

        BluetoothAdapter.DefaultAdapter.GetProfileProxy(context,btsListener,ProfileType.A2dp);

        Thread.Sleep(1500);

        var manager = context.GetSystemService(Context.AudioService) as AudioManager;
        var devices = manager.GetDevices(GetDevicesTargets.Outputs);

        Android.Util.Log.Debug(TAG,"devices=" + devices.Length);
        foreach (var dev in devices)
        {
            Android.Util.Log.Debug(TAG,"dev: id={0} name={1} type={2}",dev.Id,dev.ProductName,dev.Type);
            if (dev.ProductName == device.Name)
            {

                 if (dev.Type.ToString().Contains("a2dp"))
                 {
                    BluetoothAdapter.DefaultAdapter.GetProfileProxy(context,ProfileType.A2dp);
                 }
                Android.Util.Log.Debug(TAG,"Found output device");
            }
         }
         //Device is now connected
         break;
         ...
}

我有一个实现IBluetoothProfileServiceListener接口的侦听器,如下所示:

var btsListener = new BTServiceListener();
class BTServiceListener : AppCompatActivity,IBluetoothProfileServiceListener
{
    public void OnServiceConnected([GeneratedEnum] ProfileType profile,IBluetoothProfile proxy)
    {
        if (profile == ProfileType.A2dp)
        {
            Android.Util.Log.Debug(TAG,"A2dp");
        }    
    }
    ...
}

我需要在蓝牙a2dp(和更高版本的耳机)的onConnect上捕获事件,但是我不知道该怎么做。

接收器中的这段代码显示了蓝牙onConnect事件(交换机中的BluetoothDevice.ActionAclConnected),然后我检查了设备列表,还没有连接的设备,然后我等待了1500ms(我需要某种方式来改进此方法,这样就不能这样了),让audioService将实际的a2dp设备添加到列表中,并且在for循环中,我通过其名称找到了另一个设备,并且我确定它是正确的。但是,除了检查名称是否包含a2dp(请参见循环)之外,我无法通过编程方式找出连接的设备类型(远程,耳机,a2bp ...)。

研究之后,我发现以下一行:

BluetoothAdapter.DefaultAdapter.GetProfileProxy(context,ProfileType.A2dp);

这使用上下文,侦听器和所需的设备类型(请参阅侦听器:BTServiceListener),问题是,我不知道侦听器中的代理是否与广播接收器中的设备是同一设备onConnect,我不知道如何使用该功能。

所以我的问题:

  1. 我应该如何以及何时使用BluetoothAdapter.DefaultAdapter.GetProfileProxy函数,并确保在侦听器和onConnect函数中使用相同的设备?

  2. 如何在不使线程进入睡眠状态的情况下从管理器获取所有设备?因为,如果没有Thread.Sleep,则实际设备不在列表中,因为先调用onConnect函数,然后再将设备添加到audioService中。

    Thread.Sleep(1500); //

    var manager = context.GetSystemService(Context.AudioService)as AudioManager; var devices = manager.GetDevices(GetDevicesTargets.Outputs);

  3. 如何区分设备类型?因为,我觉得我的String.Contains(string)方法不是路要走

很长的问题,很抱歉,谢谢您的帮助和时间。

如果您还有其他需要,请告诉我。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...