Android Simulator development and testing NFC application examples detailed _android

Source: Internet
Author: User

Support NFC from Android2.3. But NFC apps can only be tested and developed on Android phones (or tablets), and Android phones must have NFC chips. And if you test NFC transfer files, you need at least two mobile phones that support NFC. Of course, if you test read-write NFC tags, you'll need some NFC tags or posts. And NFC is not running on the emulator. So from this point of view, NFC development requires more equipment, more trouble. This also Bluetooth, sensor is the same. are not developed and tested on the Android emulator. I don't know why Google doesn't solve the problem.

But this problem is not unresolved, and it is not complicated. Since the simulator does not provide such functionality. We can simulate NFC capabilities (in fact, Bluetooth, sensors can be simulated, many readers have used some sensor simulation software, NFC simulation and this similar). And the requirement is seamless docking with the real NFC environment. That is, Android apps developed using the simulated NFC feature can be used directly in a real NFC environment without the need to modify a line of code or even recompile.

So how do we solve this problem? To know how to simulate NFC, you need to first understand how NFC works on a macro level. This is an example of NFC tags. NFC data transfer is similar to this one. When the NFC tag is near the phone, the Android system has an application called NFC (in the <android source code root directory >/PAKCAGES/APPS/NFC directory), which sends an activity Action, The action invokes a window registered in the system to process NFC requests (if not, calls the default processing window in NFC applications). The key here is the activity Action. Since NFC applications emit an activity action, the program used to simulate NFC can also send an activity action, so that the window that handles NFC requests (activity created by the user) is not shared? As long as you add a flag when you send an activity action, you can tell whether the activity action is an NFC System application or a program that simulates NFC. This can be handled according to different circumstances in the NFC window that handles the request. To be more transparent, you can write an activity class (such as Nfcactivity), which is processed according to both of these situations. And because either way, you need to provide data to write NFC tags or receive data from NFC tags. So you can provide some callback methods in this nfcactivity, and call these methods when you need to provide or receive data. Finally, when NFC is required, the user-written window class only needs to inherit from the Nfcactivity class and implement the appropriate callback method. For example, the following is an implementation method that can receive data from NFC tags and write new data.

Copy Code code as follows:

public class Nfcmantestactivity extends nfcactivity
{
Private Nfcman Mnfcman;
Private EditText Nfctagtext;
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_nfcman_test);
Nfctagtext = (edittext) Findviewbyid (R.ID.EDITTEXT_NFC);
Mnfcman = new Nfcman (this);
}
When the NFC tag is called near the phone, data is used to receive information from the NFC tag,
The return value will be written back to the NFC tab
@Override
Public String onnear (nfcdata data)
{
Returns the text to be written to the NFC label
Return Nfctagtext.gettext (). toString ();
}
This method is called after the data is successfully written to the NFC label
@Override
public void onnfcsuccess ()
{
Show tips for successfully writing data
Toast.maketext (This, "Write data successfully", Toast.length_long). Show ();
}
This method is invoked after the write data has failed
@Override
public void onnfcfailed (int error, String errormsg)
{
Display encoding and information for write data exceptions
Toast.maketext (This, "Error:" + error + "\ n" + "msg:" + errormsg,
Toast.length_long). Show ();
}
}


As you can see from the code for the Nfcmantestactivity class, there are three callback methods. Processing of Read and write NFC tag data, write success and write failed three events.

Onnear

Onnfcsuccess

Onnfcfailed

From this we can see that the shadow of NFC is completely hidden. Here's how the NFC simulation system works with Figure 1.

Fig. 1 Implementation principle of NFC simulator

As you can see from Figure 1, you need a set of virtual NFC tags, which are simulated on your PC with software. For a virtual NFC tag, an interaction is made via the socket with the emulated NFC handler installed in the NFC device, and the program sends the broadcast Action, which, after receiving the broadcast, continues to invoke the activity The action invokes the window that handles the NFC request. This is just like the NFC System program, except that there is one more process of sending broadcast. Because the analog NFC handler is separate from the library where the NFC request is processed, it can only be notified through the broadcast nfcactivity. NFC systems, however, are driven by NFC to see if real NFC tags are near. For windows that deal with NFC requests, the same activity Action is handled by both virtual NFC tags and real NFC tags. So the activity that deals with NFC requests can share a single set of code.

I call this simulation NFC application Nfcman (NFC), and see how the application simulates NFC tags, as shown in Figure 2.

Figure 2 NFC Emulator main interface

The above is the emulated NFC device in Figure 2. As long as the Android phone (which does not need to support NFC) or the analog NFC application running on the Android emulator (shown in Figure 3) is connected to the NFC simulator based on the IP in the lower-right corner of Figure 2, a phone icon is displayed.

Figure 3
The following is a virtual NFC tag in Figure 2, and clicking on the "New NFC tag" in the lower-right corner will create a new NFC tag. Double-clicking the NFC tab displays the label's information, as shown in Figure 4. This information includes the label name, maximum capacity, and label content (default is null). By dragging the virtual NFC tab to the virtual NFC device above, it is the equivalent of bringing the NFC tag closer to the device. It then passes the corresponding data through the socket to the Android application shown in Figure 3. The application then sends the broadcast. Finally, the Android application receiving the broadcast will invoke the activity of the NFC request through the activity action. Onnear, Onnfcsuccess, onnfcfailed three callback methods are called according to the situation.

                                                             Figure 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.