android -- 蓝牙 bluetooth (五)接电话与听音乐
前段时间似乎所有的事情都赶在一起,回家、集体出游、出差,折腾了近一个月,终于算暂时清静了,但清静只是暂时,估计马上又要出差了,所以赶紧把蓝牙这一 部分的文章了结下,按之前提到的目录,本文是关于蓝牙接打电话和听音乐的流程分析,对应蓝牙HFP/A2DP的profile,由于这部分也算是蓝牙的经 典功能,所以代码流程并不是很复杂,当然不复杂仅是对于代码调用流程而言,对于HFP/A2DP协议相关的东东还没有精力去看,其难易程序也无法评价。下 面从两个点HFP与A2DP来展开本文的代码跟踪:
正文开始之前,先说点题外话,在android系统中蓝牙耳机和听筒两者的音频通道是不一样的,使用蓝牙耳机接听电话和听音乐不仅涉及到本文下面提到的流
程,更要牵扯的音频通道的切换,这是一个相对比较复杂的过程,android的音频系统相关内容可不算少,个人感觉多少了下解相关知识可能有助于我们更好
的蓝牙这部分功能,不过本文的主题当然还是下面两个。
1.蓝牙耳机接听电话
这个就对应HFP(Hands-freeProfile),Free
your
Hand,蓝牙的初衷之一。先来看这个功能的场景,手机来电,手机与蓝牙耳机已连接,这时会优先触发蓝牙接听电话的代码流程,起步代码在
phonesrccomandroidphone
CallScreen.java的connectBluetoothAudio()
/disconnectBluetoothAudio(),只看连接部分好了,注意下面代码里的注释,
- /* package */ void connectBluetoothAudio() {
- if (VDBG) log("connectBluetoothAudio()...");
- if (mBluetoothHeadset != null) {
- // TODO(BT) check return
- mBluetoothHeadset.connectAudio();
- }
- // Watch out: The bluetooth connection doesn't happen instantly;
- // the connectAudio() call returns instantly but does its real
- // work in another thread. The mBluetoothConnectionPending flag
- // is just a little trickery to ensure that the onscreen UI updates
- // instantly. (See isBluetoothAudioConnectedOrPending() above.)
- mBluetoothConnectionPending = true;
- mBluetoothConnectionRequestTime = SystemClock.elapsedRealtime();
- public boolean connectAudio() {
- HeadsetService service = getService();
- if (service == null) return false;
- return service.connectAudio();
- }
- boolean connectAudio() {
- // TODO(BT) BLUETOOTH or BLUETOOTH_ADMIN permission
- enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- if (!mStateMachine.isConnected()) {
- return false;
- }
- if (mStateMachine.isAudioOn()) {
- return false;
- }
- mStateMachine.sendMessage(HeadsetStateMachine.CONNECT_AUDIO);
- return true;
- }
- static jboolean connectAudioNative(JNIEnv *env, jobject object, jbyteArray address) {
- jbyte *addr;
- bt_status_t status;
- if (!sBluetoothHfpInterface) return JNI_FALSE;
- addr = env->GetByteArrayElements(address, NULL);
- if (!addr) {
- jniThrowIOException(env, EINVAL);
- return JNI_FALSE;
- }
- //连接在这里
- if ( (status = sBluetoothHfpInterface->connect_audio((bt_bdaddr_t *)addr)) !=
- BT_STATUS_SUCCESS) {
- ALOGE("Failed HF audio connection, status: %d", status);
- }
- env->ReleaseByteArrayElements(address, addr, 0);
- return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
- }
- static bt_status_t connect_audio( bt_bdaddr_t *bd_addr )
- {
- CHECK_BTHF_INIT();
- if (is_connected(bd_addr))
- {
- BTA_AgAudioOpen(btif_hf_cb.handle);
- /* Inform the application that the audio connection has been initiated successfully */
- btif_transfer_context(btif_in_hf_generic_evt, BTIF_HFP_CB_AUDIO_CONNECTING,
- (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
- return BT_STATUS_SUCCESS;
- }
- return BT_STATUS_FAIL;
- }
2.在蓝牙列表中连接蓝牙耳机
A2dp的连接过程,在蓝牙搜索结果列表连接一个蓝牙耳机,既然是从设备列表开始,所以起步代码自然是这个了
- DevicePickerFragment.java (settingssrccomandroidsettingsluetooth) 3884 2013-6-26
- void onClicked() {
- int bondState = mCachedDevice.getBondState();
- if (mCachedDevice.isConnected()) {
- askDisconnect();
- } else if (bondState == BluetoothDevice.BOND_BONDED) {
- mCachedDevice.connect(true);
- } .......
- }
- void connect(boolean connectAllProfiles) {
- if (!ensurePaired()) { //要先确保配对
- return;
- }
- mConnectAttempted = SystemClock.elapsedRealtime();
- connectWithoutResettingTimer(connectAllProfiles);//没别的了,只能看到这里
- }
- // Try to initialize the profiles if they were not.
- ...........
- // Reset the only-show-one-error-dialog tracking variable
- mIsConnectingErrorPossible = true;
- int preferredProfiles = 0;
- for (LocalBluetoothProfile profile : mProfiles) {
- if (connectAllProfiles ? profile.isConnectable() : profile.isAutoConnectable()) {
- if (profile.isPreferred(mDevice)) {
- ++preferredProfiles;
- connectInt(profile);//连接在这里,
- }
- }
- }
- .............
- public boolean connect(BluetoothDevice device) {
- if (mService == null) return false;
- List<BluetoothDevice> sinks = getConnectedDevices();
- if (sinks != null) {
- for (BluetoothDevice sink : sinks) {
- mService.disconnect(sink);
- }}
- return mService.connect(device);
- }
- public boolean connect(BluetoothDevice device) {
- if (mService != null && isEnabled() &&
- isValidDevice(device)) {
- try {
- return mService.connect(device);
- } catch (RemoteException e) {
- Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
- return false;
- }
- }...........
- return false;
- Binder跳转
- public boolean connect(BluetoothDevice device) {
- A2dpService service = getService();
- if (service == null) return false;
- return service.connect(device);
- }
- }
- static jboolean connectA2dpNative(JNIEnv *env, jobject object, jbyteArray address) {
- jbyte *addr;
- bt_bdaddr_t * btAddr;
- bt_status_t status;
- ALOGI("%s: sBluetoothA2dpInterface: %p", __FUNCTION__, sBluetoothA2dpInterface);
- if (!sBluetoothA2dpInterface) return JNI_FALSE;
- addr = env->GetByteArrayElements(address, NULL);
- btAddr = (bt_bdaddr_t *) addr;
- if (!addr) {
- jniThrowIOException(env, EINVAL);
- return JNI_FALSE;
- }
- if ((status = sBluetoothA2dpInterface->connect((bt_bdaddr_t *)addr)) != BT_STATUS_SUCCESS) {
- ALOGE("Failed HF connection, status: %d", status);
- }
- env->ReleaseByteArrayElements(address, addr, 0);
- return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
最后感谢在前面文章中网友的热心回复与纠正,学习路上一起分享是快乐的。谢谢!
- 7楼 q261525978 2014-01-08 10:02发表 [回复]
- 你好,请问配对的时候,配对方式是根据什么来选择的?比如手机跟手机配对使用的是随机生成pin码,手机跟一些耳机配对要输入pin码。关于源码里面的配对,我反着追踪,到JNI里面一个函数ssp_request_callback(),一直找不到哪里调用了这个函数
- 6楼 ping550 2014-01-07 16:12发表 [回复]
- 你好,请教一个问题,蓝牙打电话时(蓝牙耳机接电话),语音数据在android内核层是如何传输的,我想知道在内核层的接口函数,求指教,谢谢!
- 5楼 syngalon螳螳 2013-11-04 15:36发表 [回复]
- 在xref 的4.2代码中, DevicePickerFragment.java跟你讲的不一致,没有onClicked方法,有onDevicePreferenceClick方法。逻辑不一样呢