蓝牙耳机图标显示代码路径在:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/
在PhoneStatusBarPolicy.java中的函数
private final void updateBluetooth(Intent intent) {
inticonId = R.drawable.stat_sys_data_bluetooth;
String contentDescription = null;
String action = intent.getAction();
if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.ERROR);
mBluetoothEnabled = state == BluetoothAdapter.STATE_ON;
}else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
BluetoothAdapter.STATE_DISCONNECTED);
if (state == BluetoothAdapter.STATE_CONNECTED) {
iconId = R.drawable.stat_sys_data_bluetooth_connected;
contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);
}else {
contentDescription = mContext.getString(
R.string.accessibility_bluetooth_disconnected);
}
}else {
return;
}
mService.setIcon("bluetooth",iconId, 0, contentDescription);
mService.setIconVisibility("bluetooth", mBluetoothEnabled);
}
通过 updateBluetooth这个函数可以看到,蓝牙耳机的图标是根据广播 ACTION_CONNECTION_STATE_CHANGED来判断的。
下面我们来看一看广播ACTION_CONNECTION_STATE_CHANGED是如何发送的,
代码路径在:
packages/apps/Bluetooth/src/com/android/bluetooth/btservice
在AdapterProperties.java中的函数
void sendConnectionStateChange(BluetoothDevicedevice, int profile, int state, int prevState) {
......
synchronized (mObject) {
updateProfileConnectionState(profile,state, prevState);
if (updateCountersAndCheckForConnectionStateChange(state, prevState)) {
setConnectionState(state);
Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
convertToAdapterState(state));
intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE,
convertToAdapterState(prevState));
......
}
}
}
通过函数 sendConnectionStateChange来看广播的发送是由 updateCountersAndCheckForConnectionStateChange()函数的返回值决定的。
private booleanupdateCountersAndCheckForConnectionStateChange(int state, int prevState) {
switch (prevState) {
case BluetoothProfile.STATE_CONNECTING:
mProfilesConnecting--;
break;
case BluetoothProfile.STATE_CONNECTED:
mProfilesConnected--;
break;
case BluetoothProfile.STATE_DISCONNECTING:
mProfilesDisconnecting--;
break;
}
switch (state) {
case BluetoothProfile.STATE_CONNECTING:
mProfilesConnecting++;
return (mProfilesConnected == 0 && mProfilesConnecting == 1);
case BluetoothProfile.STATE_CONNECTED:
mProfilesConnected++;
return (mProfilesConnected == 1);
case BluetoothProfile.STATE_DISCONNECTING:
mProfilesDisconnecting++;
return (mProfilesConnected == 0 && mProfilesDisconnecting == 1);
case BluetoothProfile.STATE_DISCONNECTED:
return (mProfilesConnected == 0 && mProfilesConnecting == 0);
default:
return true;
}
}
通过updateCountersAndCheckForConnectionStateChange来看,mProfilesConnecting、mProfilesConnected、mProfilesDisconnecting这三个变量满足一定的条件函数返回才为TRUE。而这三个变量会根据连蓝耳机上次状态和当前状态的进行加减。
蓝牙耳机图标显示过程:
蓝牙耳机的连接分为Headset Profile 和A2DP Profile两个Profile,连接的时候只要有一个连接上就会返回TRUE,发送广播,从而显示蓝牙耳机图标,而断开连接的时候只有两个都断开了才会返回TRUE,发送广播,显示蓝牙普通图标。