This article is about preparing materials, mainly from du Niang. I would like to thank the original author. After preparing materials for understanding, start with the third section.
I. Mobile phone NFC Architecture
Currently, mobile phones are mainly in the S2C (SigIn-SigOut-Connection) architecture, mainly to prevent NFC tags (I .e. NFC cards) and NFC devices (I .e. NFC hosts and accessors) from sensing, it was intercepted by other interested people from the middle and from the side. For example:
Ii. Android NFC Architecture
3. Start and initialize NFC Service
OK. Now, start to enter the topic.
NFC Service is essentially a system APP, similar to Phone. Note that ~ Android: persistent = "true" is set in/packages/app/Nfc/AndroidManifest. xml ". Therefore, after AM (ActivityManager) is started, AM starts the NFC service. At this time, it enters the NFC service Startup Process.
According to the sequence of JAVA class instantiation, the system will first create an NfcServiceHandler, which plays a major role after the service is fully started. It will be detailed later.
The onCreate function will be executed later. At this time, the TagService, NfcAdapterService, and NfcAdapterExService internal classes will be instantiated first, and these three classes are respectively ~ Several aidl Implementations under/platform/frameworks/base/core/java/android/nfc. The NfcAdpaterService is registered to ServiceManager after the NfcService is started and exposed to some basic methods of other processes, such as enabling and disabling NFC devices.
Then NativeNfcManager is instantiated. At this time, JNI is loaded, and a monitor is created in the JNI layer (in fact, several semaphores, mutex, and condition variables are used in the future, because android nfc operations are asynchronous operations and multithreading, in order to ensure uniqueness and order, as well as to block threads) and an nfc_jni_native_data native_data will store the NativeNfcManager instance. In fact, NativeNfcManager is used to interact with JNI and do practical work at the application layer.
Then HandoverManager will be created (mainly for processing Handover messages, which can theoretically process wifi and bluetooth. In fact, it is mainly used to establish bluetooth connections. Currently, Android uses P2p file transfer to establish bluetooth connections through handover, then transmit data via Bluetooth), NfcDispatcher and P2pLinkManager. It can be seen from the names that NfcDispather is responsible for message delivery, and P2pLinkManager is used to process P2p links. P2pLinkManager implements two interfaces, Handler. callback and P2pEventListener. callback, so we can see that the class constructor will pass itself into the class when instantiating P2pEventManager and Handler. Of course, this Handler shares a MessageQueue and a logoff with the NfcServiceHandler, but he does not know how to handle the two handlemessages (I hope the people familiar with the matter will explain ). In addition, this step can be switched with the previous step (instantiate NativeNfcManager.
After that, the security management module and Access control will be started. The content is very simple, and it is not the focus. The NfcAdpater is then registered to ServiceManager, and then the scanner is registered to handle system events, such as screen lock and App installation.
In fact, most of our operations are still at the application layer and can reach JNI at most. However, even in JNI, we only allocate space for the two variables in the memory, without operating the hardware, we don't even know whether the phone is installed with an NFC chip. The initialization and real enable of the NFC device are only executed in the last sentence, new enabledisabletask(cmd.exe cute (TASK_BOOT.
At the beginning of this sentence, an asynchronous Task is created, and then the start operation is started. In other words, a thread is started and the device is initialized.