根据前一篇的思路大致走一遍.
先从应用层开始,Settings工程的蓝牙部分,这部分还是挺熟悉的,以前在某知名公司负责ROM开发时熟悉了.当然不同系统版本,代码可能有点不同,
设置蓝牙主界面BluetoothSettings.java,但是这里大概分析开启蓝牙流程和扫描蓝牙流程.
开启蓝牙流程:
直接进入BluetoothEnabler这个类查看.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Show toast message if Bluetooth is not allowed in airplane mode
if (isChecked &&
!WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off
buttonView.setChecked(false);
}
if (mLocalAdapter != null) {
mLocalAdapter.setBluetoothEnabled(isChecked);
}
mSwitch.setEnabled(false);
}
一句:
mLocalAdapter.setBluetoothEnabled(isChecked);
找到这个点时开始点,然后看一下LocalBluetoothAdapter类
public void setBluetoothEnabled(boolean enabled) {
boolean success = enabled
? mAdapter.enable()
: mAdapter.disable();
if (success) {
setBluetoothStateInt(enabled
? BluetoothAdapter.STATE_TURNING_ON
: BluetoothAdapter.STATE_TURNING_OFF);
} else {
if (Utils.V) {
Log.v(TAG, "setBluetoothEnabled call, manager didn't return " +
"success for enabled: " + enabled);
}
syncBluetoothState();
}
}
注意:
boolean success = enabled
? mAdapter.enable()
: mAdapter.disable();
开始对蓝牙设备进行了使能操作,打开蓝牙设备mAdapter是BluetoothAdapter的一个对象,而BluetoothAdapter类在framework里面了
public boolean enable() {
if (isEnabled() == true){
if (DBG) Log.d(TAG, "enable(