Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize NFC read and write function by android

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how android to achieve NFC read and write function, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is NFC?

Short distance wireless communication technology, which evolved from contactless radio frequency identification (RFID), was jointly developed by Philips Semiconductor (now NXP Semiconductor), Nokia and Sony, based on RFID and interconnection technology. Near field communication (Near Field Communication,NFC) is a short-range, high-frequency radio technology that operates within a distance of 20 centimeters at the 13.56MHz frequency. Its transmission speed is 106 Kbit/ seconds, 212 Kbit/ seconds or 424 Kbit/ seconds. At present, NFC has become ISO/IEC IS 18092 international standard, ECMA-340 standard and ETSI TS 102190 standard. NFC uses both active and passive read modes.

There are mainly the following NFC communication modes (sources of information):

1. Reader mode (Reader/writer mode):

Used as a contactless card reader, such as reading relevant information from posters or electronic tags for exhibition information. It can also realize the data exchange between NFC mobile phones, which will bring a lot of convenience for file sharing in the enterprise environment, or for multi-player game applications.

two。 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 lower (similar to Bluetooth). Wirelessly link two NFC-enabled devices to achieve point-to-point data transmission, such as downloading music, exchanging pictures, or synchronizing the address book of the device. Therefore, through NFC, multiple devices such as digital cameras, PDA, computers and mobile phones can exchange data or services.

3. Card Mode (Cardemulation):

This model is actually equivalent to an IC card using RFID technology, which can replace a large number of IC cards (including credit cards), such as shopping malls, bus cards, access control, tickets, tickets and so on. In this way, there is a great advantage that the card is powered by the RF domain of the contactless card reader, even if the host device (such as mobile phone) has no power.

Second, how to use and integrate into the project?

1. First declare NFC and add the corresponding permissions in manifests

2. Declare that the NFC tag is recognized in the Activity tag

3. Encapsulate the read and write of NFC, making it easy to 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 to initialize nfc * / public NfcUtils (Activity activity) {mNfcAdapter = NfcCheck (activity); NfcInit (activity) } / * check whether NFC is open * / public static NfcAdapter NfcCheck (Activity activity) {NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter (activity); if (mNfcAdapter = = null) {return null;} else {if (! mNfcAdapter.isEnabled ()) {Intent setNfc = new Intent (Settings.ACTION_NFC_SETTINGS) Activity.startActivity (setNfc);}} return mNfcAdapter;} / * * initialize nfc settings * / public static void NfcInit (Activity activity) {mPendingIntent = PendingIntent.getActivity (activity, 0, new Intent (activity, activity.getClass ()) .addFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP), 0) IntentFilter filter = new IntentFilter (NfcAdapter.ACTION_NDEF_DISCOVERED); IntentFilter filter2 = new IntentFilter (NfcAdapter.ACTION_TAG_DISCOVERED); try {filter.addDataType ("* / *");} catch (IntentFilter.MalformedMimeTypeException e) {e.printStackTrace ();} mIntentFilter = new IntentFilter [] {filter, filter2}; mTechList = null } / * read NFC data * / public static String readNFCFromTag (Intent intent) throws UnsupportedEncodingException {Parcelable [] rawArray = intent.getParcelableArrayExtra (NfcAdapter.EXTRA_NDEF_MESSAGES); if (rawArray! = null) {NdefMessage mNdefMsg = (NdefMessage) rawArray [0]; NdefRecord mNdefRecord = mNdefMsg.getRecords () [0] If (mNdefRecord! = null) {String readResult = new String (mNdefRecord.getPayload (), "UTF-8"); return readResult;}} return "" } / * write data to nfc * / public static void writeNFCToTag (String data, Intent intent) throws IOException, FormatException {Tag tag = intent.getParcelableExtra (NfcAdapter.EXTRA_TAG); Ndef ndef = Ndef.get (tag); ndef.connect (); NdefRecord ndefRecord = NdefRecord.createTextRecord (null, data); NdefRecord [] records = {ndefRecord} NdefMessage ndefMessage = new NdefMessage (records); ndef.writeNdefMessage (ndefMessage);} / * * read nfcID * / public static String readNFCId (Intent intent) throws UnsupportedEncodingException {Tag tag = intent.getParcelableExtra (NfcAdapter.EXTRA_TAG); String id = ByteArrayToHexString (tag.getId ()); 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)

< inarray.length; ++j) { in = (int) inarray[j] & 0xff; i = (in >

> 4) & 0x0f; out + = hex [I]; I = in & 0x0f; out + = hex [I];} return out;}}

4. Foreground scheduling system using tags in NFCActivity code

@ Override public void initData () {/ / nfc initialization setting NfcUtils nfcUtils = new NfcUtils (this);} @ Override protected void onResume () {super.onResume (); / / enable foreground scheduling system NfcUtils.mNfcAdapter.enableForegroundDispatch (this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);} @ Override protected void onPause () {super.onPause () / / close the foreground dispatching system NfcUtils.mNfcAdapter.disableForegroundDispatch (this);} @ Override protected void onNewIntent (Intent intent) {super.onNewIntent (intent); / / when the Activity receives the NFC tag, run the method / / call the tool method to read the NFC data String str = NfcUtils.rendFromTag (intent) } these are all the contents of the article "how to achieve NFC read and write function in android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report