Android framework provide good support for HFP.
- AG role: BluetoothHeadset. Most phone act as AG role, and the phone app will call BluetoothHeadset API to act as AG role. This profile is enabled by default.
- HF role: 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 to ture.
As how to act as an HF role:
- Connect to the HFP profile, get an 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);
- Connect to the remote device:
mBluetoothHeadsetClient.connect(remoteDevice)
- Monitor the connection status, call changes, ag events:
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);
- Trigger an call, accept an call or send vendor AT command:
mBluetoothHeadsetClient.dail
mBluetoothHeadsetClient.acceptCall
mBluetoothHeadsetClient.sendVendorAtCommand
Android provide high level APIs, and you don't need to send a AT comand to accept the call.