赞
踩
用到的代码文件
frameworks/base/services/core/java/
com/android/server/BluetoothManagerService.java
com/android/server/SystemServiceManager.java
com/android/server/BluetoothService.java
com/android/server/SystemService.java
frameworks/base/services/java/
com/android/server/SystemServer.java
frameworks/base/core/java/
android/os/ServiceManager.java
com/android/internal/os/BinderInternal.java
android/provider/Settings.java
在Settings类中定义的内部类:
Global.java
NameValueCache.java
代码执行流程图
蓝牙开机启动流程
下面的代码是以android N(api 24)为基础。
本文基于高通(Qualcomm)平台基线。
com.android.server.SystemServer.java中开启一系列的服务。
下面进入SystemServer类中的startOtherServices()方法。
private SystemServiceManager mSystemServiceManager;
/**
* Starts a miscellaneous grab bag of stuff that has yet to be refactored
* and organized.
*/
private void startOtherServices() {
.......
// Skip Bluetooth if we have an emulator kernel
// TODO: Use a more reliable check to see if this
// product should
// support Bluetooth - see bug 988521
if (isEmulator) {
Slog.i(TAG, "No Bluetooth Service (emulator)");
} else if (mFactoryTestMode
== FactoryTest.FACTORY_TEST_LOW_LEVEL) {
Slog.i(TAG, "No Bluetooth Service (factory test)");
} else if (!context.getPackageManager().hasSystemFeature
(PackageManager.FEATURE_BLUETOOTH)) {
Slog.i(TAG,
"No Bluetooth Service (Bluetooth Hardware Not Present)");
} else if (disableBluetooth) {
Slog.i(TAG, "Bluetooth Service disabled by config");
} else {
mSystemServiceManager.startService(BluetoothService.class);
}
.......
}
下面进入SystemServiceManager类中的startService()方法。
// Services that should receive lifecycle events.
private final ArrayList mServices =
new ArrayList();
public T startService(
Class serviceClass) {
try {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService "
+ name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend "
+ SystemService.class.getName());
}
final T service;
try {
Constructor constructor = serviceClass
.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service "
+ name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service "
+ name
+ ": service must have a public constructor"
+ " with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service "
+ name
+ ": service must have a public constructor"
+ " with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service "
+ name
+ ": service constructor threw an exception",
ex);
}
// Register it.
mServices.add(service);
// Start it.
try {
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service "
+ name
+ ": onStart threw an exception", ex);
}
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
下面进入BluetoothService类的onStart()方法。
可以看见这是一个空方法,因此在SystemServer启动的onStart阶段不是真正意义上的启动蓝牙。
@Override
public void onStart() {
}
下面继续看SystemServer类中的startOtherServices()方法。
发现有下面有下面两句代码。
private void startOtherServices() {
......
mSystemServiceManager
.startBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
mSystemServiceManager
.startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
......
}
下面看下SystemServiceManager类中的startBootPhase()方法。
可以看见遍历mServices对象,分别调用其中的SystemService类的onBootPhase()方法。
我们的BluetoothService也是继承自SystemService类。
public void startBootPhase(final int phase) {
if (phase <= mCurrentPhase) {
throw new IllegalArgumentException(
"Next phase must be larger than previous");
}
mCurrentPhase = phase;
Slog.i(TAG, "Starting phase " + mCurrentPhase);
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "OnBootPhase "
+ phase);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。