Android Bluetooth 框架简读 <3>

上一篇从设置开始的,后面的扫描,连接的套路基本上很蓝牙enable差不多,下面从Phone大概介绍.

前面说过HSP,HFP的操作基本上在应用层开始的,另外蓝牙的audio部分也是从这里开始.

所以大致概括 : 有RFComm通讯连接,AT通讯处理,SCO连接以及对应Audio设置配置

展开Phone工程:

从工程的BluetoothHeadsetService.java类开始,这是一个服务,开机即启动伺候着.但是蓝牙开始启动和连接是由设置settings完成的,那么状态变化是如何通知Phone这边的呢?

很简单,通过广播(Phone去创建各种连接前提是settings那边已经设置好蓝牙,蓝牙已经connected):

IntentFilter filter = new IntentFilter(
                BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(AudioManager.VOLUME_CHANGED_ACTION);
        filter.addAction(BluetoothDevice.ACTION_UUID);
        registerReceiver(mBluetoothReceiver, filter);

注册了三个广播,蓝牙状态变化,声量调节变化,还有一个ACTION_UUID,

接下来看看如何处理这些消息的:

else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                           BluetoothAdapter.ERROR)) {
                case BluetoothAdapter.STATE_ON:
                    mAg.start(mIncomingConnectionHandler);
                    mBtHandsfree.onBluetoothEnabled();
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    mBtHandsfree.onBluetoothDisabled();
                    mAg.stop();
                    if (currDevice != null) {
                        try {
                            mBinder.disconnect(currDevice);
                        } catch (RemoteException e) {}
                    }
                    break;
                }


跟随后面就创建一个线程:

public IncomingScoAcceptThread() {
			BluetoothServerSocket serverSocket = null;
			try {
				serverSocket = BluetoothAdapter.listenUsingScoOn();
			} catch (IOException e) {
				Log.e(TAG, "Could not create BluetoothServerSocket");
				stopped = true;
			}
			mIncomingServerSocket = serverSocket;
		}

		@Override
		public void run() {
			while (!stopped) {
				try {
					mIncomingSco = mIncomingServerSocket.accept();
				} catch (IOException e) {
					Log.e(TAG,
							"BluetoothServerSocket could not accept connection");
				}

				if (mIncomingSco != null) {
					connectSco();
				}
			}
		}

连接sco服务.

然后RFComm连接呢?回到最上面的广播处理位置:

else if (action.equals(BluetoothDevice.ACTION_UUID)) {
                if (device.equals(mDeviceSdpQuery) && device.equals(currDevice)) {
                    // We have got SDP records for the device we are interested in.
                    getSdpRecordsAndConnect(device);
                }
            }

这个广播action 是每次发现设备的时候都会触发,从而进来处理,跟踪getSdpRecordsAndConnect方法:

mConnectThread = new RfcommConnectThread(device, channel, type);

RfcommConnectThread也是一个线程来完成连接操作,当RFCOMM连接成功建立后,BluetoothHeadsetDevice 会收到RFCOMM_CONNECTED消息,然后处理这个消息如下:

case RFCOMM_CONNECTED:
                if (DBG) log("Rfcomm connected");
                mConnectThread = null;
                HeadsetBase headset = (HeadsetBase)msg.obj;
                setState(device, BluetoothProfile.STATE_CONNECTED);

                mRemoteHeadsets.get(device).mHeadset = headset;
                mBtHandsfree.connectHeadset(headset, mRemoteHeadsets.get(device).mHeadsetType);
                break;
            }

然后看看connectHeadset做了什么:

synchronized void connectHeadset(HeadsetBase headset,
			int headsetType) {
		mHeadset = headset;
		mHeadsetType = headsetType;
		if (mHeadsetType == TYPE_HEADSET) {
			initializeHeadsetAtParser();
		} else {
			initializeHandsfreeAtParser();
		}

		// Headset vendor-specific commands
		registerAllVendorSpecificCommands();

		headset.startEventThread();
		configAudioParameters();

		if (inDebug()) {
			startDebug();
		}

		if (isIncallAudio()) {
			audioOn();
		} else if (mCM.getFirstActiveRingingCall().isRinging()) {
			// need to update HS with RING when single ringing call exist
			mBluetoothPhoneState.ring();
		}
	}


<1> : initializeHeadsetAtParser是注册处理HSP的AT命令的.

initializeHandsfreeAtParser是注册处理HFP的AT命令的,个人感觉HFP的蓝牙相对普遍一下,即那种支持听音乐接听电话的,也可以从这个方法里面的程序也能够看出来.

这个AT操作,比如按蓝牙耳机上面的volume建,这里就会获得对应的AT命令,在这里就可以处理.
<2> : 后面是蓝牙的sco audio部分的audio配置,这些参数设置,最终会进入AudioPolicyService中,在被调用setBluetoothScoOn方法时,Audio部分会根据这里的蓝牙Audio配置信息,将线路切换到sco语音上面.

... ...

最后给一张图:













你可能感兴趣的:(Android Bluetooth 框架简读 <3>)