android蓝牙开机自动回连,android Bluetooth开机启动流程分析

用到的代码文件

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);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值