[转载]Android O Nfc Enable详解(上)

https://blog.csdn.net/shaoyuan1314520/article/details/79522174

摘要】随着Google于去年八月二十二日正式发布Android 8.0版本 Oreo,各种新的功能和变化也在等待着开发者的学习和研究。整体来说,这次的改动还是很大的,比如Camera的重新实现,HIDL机制的引入等。下面主要基于Android O的实现来分析NFC是如何Enable的。

这里写图片描述

上图是从Settings中启动NFC到JNI层的关系

这里首先说一下NFC的核心处理服务NfcService是如何初始化的,不同于蓝牙Service只在enable的时候才会启动,NfcService在手机初始化的过程中就会被启动,原因在于NfcApplication的Manifest里声明了android:persistent=”true” 这个属性。

熟悉Android启动流程的朋友应该知道,init过程中,zygote进程会start server

//zygote进程start system-server
service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server
 
 
  • 1
  • 2

在SystemServer启动之后,首先会启动BootstrapServices和CoreService,之后会调用
startOtherServices

// Start services.
        try {
            traceBeginAndSlog("StartServices");
            startBootstrapServices();
            startCoreServices();
            startOtherServices();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这个方法里面,一系列系统服务的systemReady方法会被调用,例如PowerManagerService,PackageManagerService以及DisplayManagerService等,当然,上面提到的ActivityManagerService的systemReady方法也会被调用,在这里第三方的code将会被运行

  // We now tell the activity manager it is okay to run third party
        // code.  It will call back into us once it has gotten to the state
        // where third party code can really run (but before it has actually
        // started launching the initial applications), for us to complete our
        // initialization.
        mActivityManagerService.systemReady(() -> {
            Slog.i(TAG, "Making services ready");
            traceBeginAndSlog("StartActivityManagerReadyPhase");
            mSystemServiceManager.startBootPhase(
                    SystemService.PHASE_ACTIVITY_MANAGER_READY);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在AMS的systemReady中,会调用startPersistentApps,最终在这个方法里,persistent app会被start,在NfcApplication start的onCreate中,NfcService会被初始化。

private void startPersistentApps(int matchFlags) {
        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL) return;
    <span class="hljs-keyword">synchronized</span> (<span class="hljs-keyword">this</span>) {
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">final</span> List&lt;ApplicationInfo&gt; apps = AppGlobals.getPackageManager()
                    .getPersistentApplications(STOCK_PM_FLAGS | matchFlags).getList();<div class="hljs-button {2}" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul></pre> 
        //NfcApplication的onCreate中
        if (UserHandle.myUserId() == 0 && isMainProcess) {
            mNfcService = new NfcService(this);
 
 
  • 1
  • 2
  • 3

需要注意一点,因为NfcApplication有android:persistent=”true”这个属性,就会导致com.android.nfc这个进程是常驻的,没办法被kill掉(AMS监听到意外挂掉的应用是persistent的,会尝试重启这个应用)

OK,言归正传,当用户在Settings中switch nfc的开关,首先会调用NfcAdapter的enable
这是一个bind调用,在之前NfcService的初始化过程中,ServiceManager会把NfcAdapterService加入到其队列里面,它继承了INfcAdapter.Stub

mNfcAdapter = new NfcAdapterService();

final class NfcAdapterService extends INfcAdapter.Stub {
@Override
public boolean enable() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
int val = mDeviceHost.GetDefaultSE();
Log.i(TAG, "getDefaultSE " + val);

// Make sure this is only called when object construction is complete.
ServiceManager.addService(SERVICE_NAME, mNfcAdapter);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

因此会走到上述代码中,而在NfcService#NfcAdapterService中,nfc的enable的执行是通过一个异步Task完成的

new EnableDisableTask().execute(TASK_ENABLE);
 
 
  • 1

这里说明一点,在Android的上古时代(Android 1.6),AsyncTask是多任务并发执行的,但是
出现了很多线程同步的问题,为了避免这些错误,在3,2之后,又改成了默认单任务执行。
所以,同一时刻,只能有一个enable或者disable的操作在队列中中执行,其他的task都在队列中等待。

AsyncTask顺利执行,会走到enableInternal中,这个是上层实际开始enable NFC的地方,我们看看这里面发生了哪些操作。

首先,在进行device的initialize之前,会生成一个watchDog线程作为监听,默认的timeout值是90秒,如果初始化异常,会调用自身的abort方法将自身的进程kill掉重新启动

WatchDogThread watchDog = new WatchDogThread("enableInternal", timeout);
            watchDog.start();

//一旦触发watchDog,会调用doAbort方法kill掉com.android.nfc进程,重启
mDeviceHost.doAbort(getName());

  • 1
  • 2
  • 3
  • 4
  • 5

接下来会执行mDeviceHost.initialize(),DeviceHost是一个对外的Interface,包含了很多的NFC方法,NativeNfcManager实现了这个接口。
在NativeNfcManager的方法里,我们可以看到,预先加载了一个name为nqnfc_nci_jni的动态链接库。通过查看Nfc根目录下的nci/jni子目录的Android.mk我们可以看到,所有jni目录下面的文件都会被编译进一个so库,这个库就是nqnfc_nci_jni

//如果动态链接库的name不是以lib开头的,系统会给name自动加上lib作为名称的头部
LOCAL_MODULE := libnqnfc_nci_jni

static {
System.loadLibrary(“nqnfc_nci_jni”);
}

private native boolean doInitialize();

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

通过JNI,最终会走到nfcManager_doInitialize,这个方法比较耗时,一般会在1s~3s之间,是底层的逻辑初始化,包括NFA(NFC For Android)以及GKI(General Kernel Interface)等,也是Nfc Enable最重要的部分,关于这块,在下一篇文章中我们再具体讨论吧~

注:本人水平有限,欢迎各位大牛批评指正!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值