android -- 蓝牙 bluetooth (五)接电话与听音乐
1.蓝牙耳机接听电话
- /* 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.在蓝牙列表中连接蓝牙耳机
- 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);//没别的了,只能看到这里
- }
- // 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;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。