如何在Android中以编程方式使用蓝牙HFP配置文件?

问题描述 投票:0回答:1

在这里,我尝试使用经典蓝牙连接两个 Android 设备,并通过 HFP 配置文件转接呼叫。

如果设备A有来电,我需要通知设备B并从设备B端接受/拒绝,甚至需要从设备B端通话。

我在蓝牙配置中从源端进行了更改,以便为设备 B 中的 HFP 配置文件启用 A2DP 接收器和 HF 角色(禁用 AG 角色)。

我对 AT 命令的工作原理感到困惑。我必须通过输出流传递 AT 命令(蓝牙经典连接)。

仅传递 AT 命令(根据 HFP 文档)来接受呼叫就足够了,还是我必须根据收到的 AT 命令在设备 B 端处理呼叫?我正在为此工作创建一个应用程序。

如果通过 AT 命令接受呼叫,呼叫也会自动通过连接进行传输,或者我是否必须从应用程序级别手动执行某些操作?

android bluetooth android-source android-bluetooth hfp
1个回答
3
投票

Android框架对HFP提供了良好的支持。

  1. AG角色:蓝牙耳机。大多数手机充当AG角色,手机应用程序会调用BluetoothHeadset API来充当AG角色。该配置文件默认启用。
  2. HF 角色:BluetoothHeadsetClient。该API是隐藏的,第三方应用无法使用。对于大多数手机,没有应用程序可以处理 HF 角色。所以你需要开发一个APP来做到这一点。而安卓汽车,它可以充当高频角色与手机连接。 AOSP CarSystemUI 有一个示例。默认情况下禁用此配置文件。您应该通过覆盖 profile_supported_hfpclient 来启用此配置文件。

至于如何扮演HF角色:

  • 连接到 HFP 配置文件,获取蓝牙耳机客户端:

    private BluetoothHeadsetClient mBluetoothHeadsetClient;
    private final ServiceListener mHfpServiceListener = new ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.HEADSET_CLIENT) {
                mBluetoothHeadsetClient = (BluetoothHeadsetClient) proxy;
            }
        }

        @Override
        public void onServiceDisconnected(int profile) {
            if (profile == BluetoothProfile.HEADSET_CLIENT) {
                mBluetoothHeadsetClient = null;
            }
        }
    };

   mAdapter.getProfileProxy(context.getApplicationContext(), mHfpServiceListener,
     BluetoothProfile.HEADSET_CLIENT);

  • 连接远程设备:
   mBluetoothHeadsetClient.connect(remoteDevice)
  • 监控连接状态、呼叫变化、ag事件:
   IntentFilter filter = new IntentFilter();
   filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
   filter.addAction(BluetoothHeadsetClient.ACTION_AG_EVENT);
        mContext.registerReceiver(this, filter);
   filter.addAction(BluetoothHeadsetClient.ACTION_CALL_CHANGED);
        mContext.registerReceiver(this, filter);
  • 触发呼叫、接听呼叫或发送厂商 AT 命令:
   mBluetoothHeadsetClient.dail
   mBluetoothHeadsetClient.acceptCall
   mBluetoothHeadsetClient.sendVendorAtCommand

Android提供高级API,您无需发送AT命令即可接受呼叫。

© www.soinside.com 2019 - 2024. All rights reserved.