Android simulator development and testing NFC application example details

NFC has been supported since Android 2.3. However, NFC applications can only be tested and developed on Android phones (or tablets), and Android phones must also have NFC chips. Moreover, if testing NFC to transfer files, at least two NFC enabled mobile phones are required. Of course, if you test reading and writing NFC tags, you also need some NFC tags or posts. And NFC cannot run on the simulator. Therefore, from this point, NFC development needs more equipment and is more troublesome. This is also the same with Bluetooth and sensors. Can't be developed and tested on the Android simulator. I don't know why Google doesn't solve this problem.

However, this problem is not insurmountable and not complex. Since the simulator does not provide such a function. We can simulate NFC functions (in fact, Bluetooth and sensors can be simulated. Many readers may have used some sensor simulation software. NFC simulation is similar to this). And the requirements are seamless with the real NFC environment. In other words, Android applications developed using simulated NFC functions can be directly used in a real NFC environment without modifying a line of code or even recompiling.

So how to solve this problem? To know how to simulate NFC, you need to understand the working principle of NFC from a macro perspective. Here we use the NFC tag as an example. NFC data transmission is similar to this. When the NFC tag is close to the mobile phone, there is a system application called NFC in the Android system (in the < Android source code Root Directory > / pakcages / apps / NFC directory). The application will send an activity action, which will call a window registered in the system to process NFC requests (if not, call the default processing window in the NFC application). The key here is activity action. Since the NFC application will send an activity action, the program used to simulate NFC will also send an activity action. Can't you share the window used to process NFC requests (the activity established by the user)? As long as a flag is added when sending the activity action, you can distinguish whether the activity action is sent by the NFC system application or the program simulating NFC. In this way, it can be processed according to different situations in the NFC window for processing requests. To be more transparent, you can write an activity class (such as nfcactivity), which handles these two situations. In either case, it is necessary to provide data written to the NFC tag or receive data in the NFC tag. Therefore, you can provide some callback methods in the nfcactivity. When you need to provide or receive data, you can call these methods. Finally, when you need to use the NFC function, the window class written by the user only needs to inherit from the nfcactivity class and implement the corresponding callback method. For example, the following is an implementation method that can receive NFC tag data and write new data.

onNear

onNFCSuccess

onNFCFailed

It can be seen from this that the shadow of NFC is completely hidden. The principle of this NFC simulation system is described below with figure 1.

Fig. 1 implementation principle of NFC simulator

As can be seen from Figure 1, there needs to be a set of virtual NFC tags, which are simulated by software on PC. For the virtual NFC tag, interact with the simulated NFC handler installed in the NFC device through the socket, and then the program will send a broadcast action. Finally, after receiving the broadcast, it will continue to call the activity action and call the window for processing NFC requests. This is the same as the NFC system program, except that there is an additional process of sending broadcast in the middle. Because the simulated NFC handler is separated from the library where the nfcactivity processing NFC requests is located, it can only be notified through broadcast. The NFC system program senses whether the real NFC tag is close through the NFC driver. For the window processing NFC requests, both the virtual NFC tag and the real NFC tag process the same activity action. Therefore, the activities that process NFC requests can share a set of code.

I call this NFC simulation application nfcman (NFC man). Let's take a look at how the application simulates NFC tags. The simulation interface is shown in Figure 2.

Figure 2 main interface of NFC simulator

In Figure 2, the above is an analog NFC device. As long as the Android phone (does not need to support NFC) or the NFC simulation application running on the Android simulator (as shown in Figure 3) is connected to the NFC simulator according to the IP in the lower right corner of Figure 2, a mobile phone icon will be displayed.

Figure 3 below figure 2 is a virtual NFC tag. Click "new NFC tag" in the lower right corner to create a new NFC tag. Double click the NFC tag to display the information of the tag, as shown in Figure 4. This information includes label name, maximum capacity and label content (empty by default). As long as the virtual NFC tag is dragged to the virtual NFC device above, it is equivalent to bringing the NFC tag close to the device. Then, the corresponding data will be transmitted to the Android application shown in Figure 3 through the socket. The application will then send a broadcast. Finally, the Android application that receives the broadcast will call the activity that processes the NFC request through the activity action. Three callback methods, onnear, onnfcsuccess, and onnfcfailed, will be called as appropriate.

Figure 4

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>