How to Write NDEF Records to NFC Tag
Writing NDEF records to an NFC tag requires utilizing the enableReaderMode API, which offers superior performance and reliability compared to the Intent-based system. By handling the reading and writing process rather than relying on the system's default behavior, the risk of failed writes and corrupted cards is significantly reduced.
Key Benefits of Using enableReaderMode API:
Implementation Example:
The following code sample demonstrates the writing of NDEF records using the enableReaderMode API:
public class NFCActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback { @Override public void onTagDiscovered(Tag tag) { Ndef mNdef = Ndef.get(tag); if (mNdef != null) { // Create and add the NDEF record to a NDEF message try { mNdef.connect(); NdefMessage mMsg = new NdefMessage(NdefRecord.createTextRecord("en", "English String")); mNdef.writeNdefMessage(mMsg); // Success handling code (e.g., notification sound or UI feedback) } catch (Exception e) { // Error handling (e.g., toast message or log error) } finally { // Release resources and close the connection to the tag mNdef.close(); } } } }
By utilizing these techniques, developers can enhance the reliability and efficiency of NFC writing operations, ensuring seamless user experiences.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3