1. What is NFC?
Close-range wireless communication technology, this technology evolved from contactless radio frequency identification (RFID), was jointly developed and developed by Philips Semiconductor (now NXP Semiconductor), Nokia and Sony. Its basis is RFID and interconnection technology. Near Field Communication (NFC) is a short-range and high-frequency radio technology that operates within a distance of 20 cm at a frequency of 13.56MHz. Its transmission speeds are 106 Kbit/second, 212 Kbit/second or 424 Kbit/second. At present, near field communication has been passed and become the ISO/IEC IS 18092 international standard, ECMA-340 standard and ETSI TS 102 190 standard. NFC adopts two reading modes: active and passive.
There are mainly the following NFC communication modes (information source):
1. Card reader mode (Reader/writer mode):
Used as a contactless card reader, such as reading related information from posters or exhibition information electronic tags. Data exchange between NFC phones can also be realized, which will bring many conveniences to file sharing in the enterprise environment or multi-player gaming applications.
2. Point-to-point mode (P2Pmode):
This mode is similar to infrared and can be used for data exchange, except that the transmission distance is shorter, the transmission creation speed is faster, the transmission speed is faster, and the power consumption is low (Bluetooth is also similar). Wireless linking of two devices with NFC functions can enable point-to-point data transmission, such as downloading music, exchanging pictures, or synchronizing device address book. Therefore, through NFC, multiple devices such as digital cameras, PDAs, computers and mobile phones can exchange information or services.
3. Card mode:
This model is actually equivalent to an IC card using RFID technology, which can replace a large number of IC cards (including credit cards) when using it, such as shopping mall card swiping, bus cards, access control, tickets, tickets, etc. In this way, there is a great advantage, that is, the card is powered through the RF domain of the contactless card reader, and can work even if the host device (such as a mobile phone) is out of power.
2. How to use and integrate into the project?
1. First declare NFC in manifests and add corresponding permissions;
<uses-feature android:name="" android:required="true" /> <uses-permission android:name="" />
2. Declare the identification of NFC tag in the Activity tag;
<activity android:name="."> <intent-filter> <action android:name=".TAG_DISCOVERED" /> <category android:name="" /> <data android:mimeType="*/*" /> </intent-filter> </activity>
3. Encapsulate NFC reading and writing to facilitate call;
public class NfcUtils { //nfc public static NfcAdapter mNfcAdapter; public static IntentFilter[] mIntentFilter = null; public static PendingIntent mPendingIntent = null; public static String[][] mTechList = null; /** * Constructor, used to initialize nfc */ public NfcUtils(Activity activity) { mNfcAdapter = NfcCheck(activity); NfcInit(activity); } /** * Check whether NFC is on */ public static NfcAdapter NfcCheck(Activity activity) { NfcAdapter mNfcAdapter = (activity); if (mNfcAdapter == null) { return null; } else { if (!()) { Intent setNfc = new Intent(Settings.ACTION_NFC_SETTINGS); (setNfc); } } return mNfcAdapter; } /** * Initialize nfc settings */ public static void NfcInit(Activity activity) { mPendingIntent = (activity, 0, new Intent(activity, ()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); IntentFilter filter2 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); try { ("*/*"); } catch (e) { (); } mIntentFilter = new IntentFilter[]{filter, filter2}; mTechList = null; } /** * Read NFC data */ public static String readNFCFromTag(Intent intent) throws UnsupportedEncodingException { Parcelable[] rawArray = (NfcAdapter.EXTRA_NDEF_MESSAGES); if (rawArray != null) { NdefMessage mNdefMsg = (NdefMessage) rawArray[0]; NdefRecord mNdefRecord = ()[0]; if (mNdefRecord != null) { String readResult = new String((), "UTF-8"); return readResult; } } return ""; } /** * Write data to nfc */ public static void writeNFCToTag(String data, Intent intent) throws IOException, FormatException { Tag tag = (NfcAdapter.EXTRA_TAG); Ndef ndef = (tag); (); NdefRecord ndefRecord = (null, data); NdefRecord[] records = {ndefRecord}; NdefMessage ndefMessage = new NdefMessage(records); (ndefMessage); } /** * Read nfcID */ public static String readNFCId(Intent intent) throws UnsupportedEncodingException { Tag tag = (NfcAdapter.EXTRA_TAG); String id = ByteArrayToHexString(()); return id; } /** * Convert byte array to string */ private static String ByteArrayToHexString(byte[] inarray) { int i, j, in; String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; String out = ""; for (j = 0; j < ; ++j) { in = (int) inarray[j] & 0xff; i = (in >> 4) & 0x0f; out += hex[i]; i = in & 0x0f; out += hex[i]; } return out; } }
4. The foreground scheduling system for the use and use of tags in NFCActivity code;
@Override public void initData() { //NFC initialization settings NfcUtils nfcUtils = new NfcUtils(this); }
@Override protected void onResume() { (); //Open the front desk scheduling system (this, , , ); }
@Override protected void onPause() { (); //Closing the front desk dispatching system (this); }
@Override protected void onNewIntent(Intent intent) { (intent); // When the activity receives the NFC tag, run this method //Call tool methods to read NFC data String str = (intent); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.