转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/71811288
本文已授权微信公众号 fanfan程序媛 独家发布 扫一扫文章底部的二维码或在微信搜索 fanfan程序媛 即可关注
上一篇说了下A2DP的一些基本操作,这篇分析下系统应用、系统源码是如何操作A2DP的。尤其是其连接过程,基于Android4.3源码。Andorid手机一般都是做为A2DP Audio Source端。
1 连接过程 |
媒体音频也就是A2DP,首先连接的蓝牙设备需要支持A2DP协议(并且做为A2DP Audio Sink端),并且需要与该设备进行配对,如何进行蓝牙配对这里就不细说了,可以参照我的其他文章。主要分析下其连接过程。
对于系统自带应用Settings中已配对的蓝牙设备界面(如下图所示):
其对应文件路径:
packages/apps/Settings/src/com/android/settings/bluetooth/DeviceProfilesSettings.Java
点击媒体音频进行连接,调用onPreferenceChange。
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mDeviceNamePref) { //重命名
mCachedDevice.setName((String) newValue);
} else if (preference instanceof CheckBoxPreference) {
//check box
LocalBluetoothProfile prof = getProfileOf(preference); //获取对应的profile
onProfileClicked(prof, (CheckBoxPreference) preference);
return false; // checkbox will update from onDeviceAttributesChanged() callback
} else {
return false;
}
return true;
}
接着看onProfileClicked()函数处理
private void onProfileClicked(LocalBluetoothProfile profile, CheckBoxPreference profilePref) {
BluetoothDevice device = mCachedDevice.getDevice(); //获取配对的蓝牙设备
int status = profile.getConnectionStatus(device); //获取profile的连接状态
boolean isConnected =
status == BluetoothProfile.STATE_CONNECTED;
if (isConnected) { //如果是连接状态则断开连接
askDisconnect(getActivity(), profile);
} else {