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

How to use bluetooth HFP profile in android programmatically?

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

如果Device A有来电,我需要从Device B端通知Device B和accept/decline,甚至需要从Device B端通话

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

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

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

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

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

  1. AG 角色:BluetoothHeadset。大多数phone充当AG角色,phone应用程序将调用BluetoothHeadsetAPI充当AG角色。默认情况下启用此配置文件。
  2. HF 角色:BluetoothHeadsetClient. This API is hided, cannot be used by third apps. For most phone, no app will handle as HF role. So you need to develep an APP to do this. And android cars, it can act as HF role to connect with an phone. There is an example from AOSP CarSystemUI. This profile is disabled by default. You should enable this profile by overlay profile_supported_hfpclient 到 ture。

HF角色的扮演方法:

  • 连接到 HFP 配置文件,获取 BluetoothHeadsetClient:

    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命令即可接听电话