android 蓝牙hfp client实现简介

首先确认配置文件是否开启hfp profile功能。根据设备的角色(hfp client / hfp server)来配置hfp profile.
profile 配置文件路径:
alps/packages/apps/Bluetooth/res/values/config.xml
alps/device/${customer}/${ProjectName}/overlay/packages/apps/Bluetooth/res/values/config.xml


e.g. 设备的角色定义为hfp client,需做如下配置:
false
true

接下来看一下hfp client的实现,以accept call为例
//3步获取hfp client service
1)BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

2)mAdapter.getProfileProxy(getApplicationContext(),new MServerListener(), BluetoothProfile.HEADSET_CLIENT);

3)public class MServerListener implements ServiceListener {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET_CLIENT) {
mclient=(BluetoothHeadsetClient)proxy;
....... 
}
}

//app获取hfp client service 的proxy之后,就可以直接调用相应的API进行连接控制与call 控制。e.g.
mclient.connect( device) //Connects to remote device.
mclient.connectAudio() //Initiates a connection of audio channel, set up SCO channel
mclient.acceptCall(device,0);
mclient.rejectCall(device);
mclient.terminateCall(device,0);
 

android hfp 对外API:
//frameworks/base/core/java/android/bluetooth/
BluetoothHeadsetClientCall.aidl
BluetoothHeadsetClientCall.java
BluetoothHeadsetClient.java
BluetoothHeadset.java
IBluetoothHeadset.aidl
IBluetoothHeadsetClient.aidl
IBluetoothHeadsetPhone.aidl

e.g.

BluetoothHeadsetClient.acceptCall()

//android hfp client services:
packages\apps\bluetooth\src\com\android\bluetooth\hfpclient
HeadsetClientHalConstants.java
HeadsetClientService.java
HeadsetClientStateMachine.java

e.g. send msg to state machine and handle these msg
boolean acceptCall(BluetoothDevice device, int flag) {
enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
int connectionState = mStateMachine.getConnectionState(device);
if (connectionState != BluetoothProfile.STATE_CONNECTED &&
connectionState != BluetoothProfile.STATE_CONNECTING) {
return false;
}
Message msg = mStateMachine.obtainMessage(HeadsetClientStateMachine.ACCEPT_CALL);
msg.arg1 = flag;
mStateMachine.sendMessage(msg);
return true;
}

private void acceptCall(int flag, boolean retry) {
......
if (handleCallActionNative(action, 0)) {
addQueuedAction(ACCEPT_CALL, action);
} else {
Log.e(TAG, "ERROR: Couldn't accept a call, action:" + action);
}
}

//enter native layter
packages/apps/Bluetooth/jni
com_android_bluetooth_hfpclient.cpp
com_android_bluetooth_hfp.cpp
e.g.
static jboolean handleCallActionNative(JNIEnv *env, jobject object, jint action, jint index) {
bt_status_t status;
if (!sBluetoothHfpClientInterface) return JNI_FALSE;
if ( (status = sBluetoothHfpClientInterface->handle_call_action((bthf_client_call_action_t)action, (int)index)) != BT_STATUS_SUCCESS) {
ALOGE("Failed to enter private mode, status: %d", status);
}
return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
}

//get bt stack profile interface
static void initializeNative(JNIEnv *env, jobject object) {
......
if ( (btInf = getBluetoothInterface()) == NULL) {
ALOGE("Bluetooth module is not loaded");
return;
}
sBluetoothHfpClientInterface = (bthf_client_interface_t *)
btInf->get_profile_interface(BT_PROFILE_HANDSFREE_CLIENT_ID);
if (sBluetoothHfpClientInterface == NULL) {
ALOGE("Failed to get Bluetooth HFP Client Interface");
return;
}
}

//enter bt stack

external/bluetooth/bluedroid/bta/hf_client

bta_hf_client_act.c
bta_hf_client_api.c
bta_hf_client_at.c
bta_hf_client_at.h
bta_hf_client_cmd.c
bta_hf_client_int.h
bta_hf_client_main.c
bta_hf_client_rfc.c
bta_hf_client_sco.c
bta_hf_client_sdp.c

e.g. send AT cmd ATA to accept this call

handle_call_action(bthf_client_call_action_t action, int idx){
......
case BTHF_CLIENT_CALL_ACTION_ATA:
BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_ATA, 0, 0, NULL);
break;
}

以上就是android 蓝牙hfp client实现简介的全文介绍,希望对您学习Android应用开发有所帮助.

你可能感兴趣的:(android,java)