天天看点

android 4.2蓝牙bluetooth 第5节:接打电话

前段时间似乎所有的事情都赶在一起,回家、集体出游、出差,折腾了近一个月,终于算暂时清静了,但清静只是暂时,估计马上又要出差了,所以赶紧把蓝牙这一部分的文章了结下,按之前提到的目录,本文是关于蓝牙接打电话和听音乐的流程分析,对应蓝牙HFP/A2DP的profile,由于这部分也算是蓝牙的经典功能,所以代码流程并不是很复杂,当然不复杂仅是对于代码调用流程而言,对于HFP/A2DP协议相关的东东还没有精力去看,其难易程序也无法评价。下面从两个点HFP与A2DP来展开本文的代码跟踪:

        正文开始之前,先说点题外话,在android系统中蓝牙耳机和听筒两者的音频通道是不一样的,使用蓝牙耳机接听电话和听音乐不仅涉及到本文下面提到的流程,更要牵扯的音频通道的切换,这是一个相对比较复杂的过程,android的音频系统相关内容可不算少,个人感觉多少了下解相关知识可能有助于我们更好的蓝牙这部分功能,不过本文的主题当然还是下面两个。

      1.蓝牙耳机接听电话

        这个就对应HFP( Hands-freeProfile),Free your Hand,蓝牙的初衷之一。先来看这个功能的场景,手机来电,手机与蓝牙耳机已连接,这时会优先触发蓝牙接听电话的代码流程,起步代码在phone\src\com\android\phone\nCallScreen.java的connectBluetoothAudio() /disconnectBluetoothAudio(),只看连接部分好了,注意下面代码里的注释, [java] view plain copy print ?

  1.  void connectBluetoothAudio() {  
  2.   if (VDBG) log("connectBluetoothAudio()...");  
  3.   if (mBluetoothHeadset != null) {  
  4.       // TODO(BT) check return   
  5.       mBluetoothHeadset.connectAudio();  
  6.   }  
  7.   // Watch out: The bluetooth connection doesn't happen instantly;   
  8.   // the connectAudio() call returns instantly but does its real   
  9.   // work in another thread.  The mBluetoothConnectionPending flag   
  10.   // is just a little trickery to ensure that the onscreen UI updates   
  11.   // instantly. (See isBluetoothAudioConnectedOrPending() above.)   
  12.   mBluetoothConnectionPending = true;  
  13.   mBluetoothConnectionRequestTime = SystemClock.elapsedRealtime();  
/* 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();
    }
           

         接下来就跳到蓝牙应用的管辖范围,代码在packages/apps/Bluetooth/src/com/android/bluetooth/hfp/HeadsetService.java, [java] view plain copy print ?

  1. public boolean connectAudio() {  
  2.     HeadsetService service = getService();  
  3.     if (service == null) return false;  
  4.     return service.connectAudio();  
  5. }  
public boolean connectAudio() {
            HeadsetService service = getService();
            if (service == null) return false;
            return service.connectAudio();
        }
           

        很明显下一个目标是HeadsetService,直接看具体实现,这部分代码跳转都比较清晰,下面代码会先判断当前状态是否正确,关于HeadsetStateMachine几个状态可以参持这个/packages/apps/Bluetooth/src/com/android/bluetooth/hfp/HeadsetStateMachine.java的最前的代码注释。 [java] view plain copy print ?

  1. boolean connectAudio() {  
  2.      // TODO(BT) BLUETOOTH or BLUETOOTH_ADMIN permission   
  3.      enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");  
  4.      if (!mStateMachine.isConnected()) {  
  5.          return false;  
  6.      }  
  7.      if (mStateMachine.isAudioOn()) {  
  8.          return false;  
  9.      }  
  10.      mStateMachine.sendMessage(HeadsetStateMachine.CONNECT_AUDIO);  
  11.      return true;  
  12.  }  
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;
    }
           

       走进HeadsetStateMachine状态机,找到CONNECT_AUDIO分支,就看带Native的方法connectAudioNative(getByteAddress(mCurrentDevice)); [java] view plain copy print ?

  1. static jboolean connectAudioNative(JNIEnv *env, jobject object, jbyteArray address) {  
  2.     jbyte *addr;  
  3.     bt_status_t status;  
  4.     if (!sBluetoothHfpInterface) return JNI_FALSE;  
  5.     addr = env->GetByteArrayElements(address, NULL);  
  6.     if (!addr) {  
  7.         jniThrowIOException(env, EINVAL);  
  8.         return JNI_FALSE;  
  9.     }  
  10. //连接在这里   
  11.     if ( (status = sBluetoothHfpInterface->connect_audio((bt_bdaddr_t *)addr)) !=    
  12.          BT_STATUS_SUCCESS) {  
  13.         ALOGE("Failed HF audio connection, status: %d", status);  
  14.     }  
  15.     env->ReleaseByteArrayElements(address, addr, 0);  
  16.     return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;  
  17. }  
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;
}
           

       上面代码还可以进一步跟到下面/external/bluetooth/bluedroid/btif/src/btif_hf.c,到了这里其实流程已经结束了,对于这里消息流转估计要放到以后再写了 [java] view plain copy print ?

  1. static bt_status_t connect_audio( bt_bdaddr_t *bd_addr )  
  2. {  
  3.     CHECK_BTHF_INIT();  
  4.     if (is_connected(bd_addr))  
  5.     {  
  6.         BTA_AgAudioOpen(btif_hf_cb.handle);  
  7.         btif_transfer_context(btif_in_hf_generic_evt, BTIF_HFP_CB_AUDIO_CONNECTING,  
  8.                               (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);  
  9.         return BT_STATUS_SUCCESS;  
  10.     }  
  11.     return BT_STATUS_FAIL;  
  12. }  
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的连接过程,在蓝牙搜索结果列表连接一个蓝牙耳机,既然是从设备列表开始,所以起步代码自然是这个了 [java] view plain copy print ?

  1. DevicePickerFragment.java (settings\src\com\android\settings\bluetooth)     3884     2013-6-26  
  2.     void onClicked() {  
  3.       int bondState = mCachedDevice.getBondState();  
  4.       if (mCachedDevice.isConnected()) {  
  5.           askDisconnect();  
  6.       } else if (bondState == BluetoothDevice.BOND_BONDED) {  
  7.           mCachedDevice.connect(true);  
  8.       } .......  
  9.   }  
  10.     void connect(boolean connectAllProfiles) {  
  11.       if (!ensurePaired()) {  //要先确保配对   
  12.           return;  
  13.       }  
  14.       mConnectAttempted = SystemClock.elapsedRealtime();  
  15.       connectWithoutResettingTimer(connectAllProfiles);//没别的了,只能看到这里   
  16.   }  
DevicePickerFragment.java (settings\src\com\android\settings\bluetooth)     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);//没别的了,只能看到这里
    }
           

      代码路径这里packages/apps/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java,具体代码看下面

[java] view plain copy print ?

  1. // Try to initialize the profiles if they were not.   
  2.      ...........  
  3.       // Reset the only-show-one-error-dialog tracking variable   
  4.       mIsConnectingErrorPossible = true;  
  5.       int preferredProfiles = 0;  
  6.       for (LocalBluetoothProfile profile : mProfiles) {  
  7.           if (connectAllProfiles ? profile.isConnectable() : profile.isAutoConnectable()) {  
  8.               if (profile.isPreferred(mDevice)) {  
  9.                   ++preferredProfiles;  
  10.                   connectInt(profile);//连接在这里,   
  11.               }  
  12.           }  
  13.       }  
  14.      .............  
// 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);//连接在这里,
                }
            }
        }
       .............
           

       connectInt的实现很简单,直接跳过看里面的profile.connect(mDevice),这里的profile是指A2dpProfile,所以connet()方法的具体实现在 [java] view plain copy print ?

  1. public boolean connect(BluetoothDevice device) {  
  2.     if (mService == null) return false;  
  3.     List<BluetoothDevice> sinks = getConnectedDevices();  
  4.     if (sinks != null) {  
  5.         for (BluetoothDevice sink : sinks) {  
  6.             mService.disconnect(sink);  
  7.     }}  
  8.     return mService.connect(device);  
  9. }  
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);
    }
           

        下面是 BluetoothA2dp.java (frameworks\base\core\java\android\bluetooth)  ,为什么是这样看下这个private BluetoothA2dp mService;就知道了 [java] view plain copy print ?

  1. public boolean connect(BluetoothDevice device) {  
  2.       if (mService != null && isEnabled() &&  
  3.           isValidDevice(device)) {  
  4.           try {  
  5.               return mService.connect(device);  
  6.           } catch (RemoteException e) {  
  7.               Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));  
  8.               return false;  
  9.           }  
  10.       }...........  
  11.       return false;  
  12.       Binder跳转  
  13.       public boolean connect(BluetoothDevice device) {  
  14.           A2dpService service = getService();  
  15.           if (service == null) return false;  
  16.           return service.connect(device);  
  17.       }  
  18.   }  
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);
        }
       
    }
           

        之后的跳转和第一部分蓝牙接听电话跳转过程类似,就不重复了,最后会来到packages/apps/Bluetooth/jni/com_android_bluetooth_a2dp.cpp的connectA2dpNative,同样到下面的代码,我们能看到的开放的代码也就是这些,再下面要看vendor的具体实现了。 [java] view plain copy print ?

  1. static jboolean connectA2dpNative(JNIEnv *env, jobject object, jbyteArray address) {  
  2.   jbyte *addr;  
  3.   bt_bdaddr_t * btAddr;  
  4.   bt_status_t status;  
  5.   ALOGI("%s: sBluetoothA2dpInterface: %p", __FUNCTION__, sBluetoothA2dpInterface);  
  6.   if (!sBluetoothA2dpInterface) return JNI_FALSE;  
  7.   addr = env->GetByteArrayElements(address, NULL);  
  8.   btAddr = (bt_bdaddr_t *) addr;  
  9.   if (!addr) {  
  10.       jniThrowIOException(env, EINVAL);  
  11.       return JNI_FALSE;  
  12.   }  
  13.   if ((status = sBluetoothA2dpInterface->connect((bt_bdaddr_t *)addr)) != BT_STATUS_SUCCESS) {  
  14.       ALOGE("Failed HF connection, status: %d", status);  
  15.   }  
  16.   env->ReleaseByteArrayElements(address, addr, 0);  
  17.   return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;  
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;
}
           

       那到此为止,本文关于蓝牙耳机与蓝牙接听电话的流程分析也就结束了,同时蓝牙这一系列的文章也暂时结束,当然后续依然会关注蓝牙。本系列的第一篇文章标题是 入门,现在想想,这五篇文章下来也不过是刚刚入门而已,协议部分更是没怎么涉及呢,对于蓝牙BT需要深入研究的地方还有很多,仅希望这五篇文章可以帮你快速了解android蓝牙代码流程,回顾以前四篇文章请点击链接:         android -- 蓝牙 bluetooth (一) 入门

        android -- 蓝牙 bluetooth (二) 打开蓝牙

        android -- 蓝牙 bluetooth (三)搜索蓝牙         android -- 蓝牙 bluetooth (四)OPP文件传输