Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:42:10+00:00 2026-06-18T05:42:10+00:00

Is it possible to read then write and then again read back the same

  • 0

Is it possible to read then write and then again read back the same data earlier we wrote on a NFC tag without removing it from the field(mobile) in Android? If Anybody has done this then please share over here….

public class MainActivity extends Activity
{
    NfcAdapter adapter;
    PendingIntent pendingIntent;
    IntentFilter writeTagFilters[];
    Tag mytag;
    private TextView mTextViewData;
    Context ctx;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ctx=this;

        mTextViewData = (TextView)findViewById(R.id.mtextView);

        Button btnWrite = (Button) findViewById(R.id.write_button);
        final EditText mEditText = (EditText)findViewById(R.id.edit_message);

        adapter = NfcAdapter.getDefaultAdapter(this);

        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        IntentFilter tagDetected1 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

        try 
        {
            tagDetected1.addDataType("text/nfc-service-tag");
        } 
        catch (MalformedMimeTypeException e) 
        {
            throw new RuntimeException("Could not add MIME type.", e);
        }
        writeTagFilters = new IntentFilter[] { tagDetected1 };

        btnWrite.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {

                    if(mytag==null)
                    {
                        Toast.makeText(ctx, ctx.getString(R.string.error_detected), Toast.LENGTH_LONG ).show();
                    }
                    else
                    {
                        writeTag(mEditText.getText().toString(),mytag);

                    }

            }
        });

    }

    private void writeTag(String text,Tag tag) 
    {
        // record to launch Play Store if app is not installed
        // record that contains our custom "retro console" game data, using custom MIME_TYPE
        String mimeType = "text/nfc-service-tag";
        byte[] payload = text.getBytes(Charset.forName("US-ASCII"));
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
        NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
        NdefMessage message = new NdefMessage(new NdefRecord[] { record });
        try 
        {
            // see if tag is already NDEF formatted
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) 
            {
                ndef.connect();
                if (!ndef.isWritable())
                {
                    Toast.makeText(ctx, "Read-only tag.", Toast.LENGTH_LONG ).show();
                }
                // work out how much space we need for the data
                int size = message.toByteArray().length;
                if (ndef.getMaxSize() < size) 
                {
                    Toast.makeText(ctx, "Tag doesn't have enough free space.", Toast.LENGTH_LONG).show();
                }
                ndef.writeNdefMessage(message);
                Toast.makeText(ctx, ctx.getString(R.string.ok_writing), Toast.LENGTH_LONG ).show();
                ndef.close();
            } 
            else 
            {
                // attempt to format tag
                NdefFormatable format = NdefFormatable.get(tag);
                if (format != null) 
                {
                    try 
                    {
                        format.connect();
                        format.format(message);
                        Toast.makeText(ctx, ctx.getString(R.string.ok_writing), Toast.LENGTH_LONG ).show();
                    } 
                    catch (IOException e) 
                    {
                        Toast.makeText(ctx,"Unable to format tag to NDEF.", Toast.LENGTH_LONG).show();
                    }
                }
                else 
                {
                    Toast.makeText(ctx,"Tag doesn't appear to support NDEF format.", Toast.LENGTH_LONG).show();
                }
            }
        }
        catch(Exception e)
        {

        }
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        String action = intent.getAction();
        if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED))
        {
            NdefMessage[] msgs = getNdefMessagesFromIntent(intent);
            final NdefMessage msg = (NdefMessage)msgs[0];
            String payload = new String(msg.getRecords()[0].getPayload());
            mTextViewData.setText(payload);

            mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);    

            Toast.makeText(this, "In onNewIntent()", Toast.LENGTH_LONG ).show();
        }

        else if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) 
        {
            Toast.makeText(this, "This NFC tag has no NDEF data.", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (getIntent().getAction() != null) 
        {
            // tag received when app is not running and not in the foreground:
            if (getIntent().getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) 
            {
                NdefMessage[] msgs = getNdefMessagesFromIntent(getIntent());
                NdefRecord record = msgs[0].getRecords()[0];
                byte[] payload = record.getPayload();

                String payloadString = new String(payload);
                mTextViewData.setText(payloadString);

                Toast.makeText(this, "In onResume()", Toast.LENGTH_LONG ).show();

            }
        }
        adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
    }

    @Override
    public void onPause()
    {
        super.onPause();
        adapter.disableForegroundDispatch(this);
    }

    NdefMessage[] getNdefMessagesFromIntent(Intent intent) 
    {
        // Parse the intent
        NdefMessage[] msgs = null;
        String action = intent.getAction();
        if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) 
        {
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            if (rawMsgs != null){
                msgs = new NdefMessage[rawMsgs.length];
                for (int i = 0; i < rawMsgs.length; i++) 
                {
                    msgs[i] = (NdefMessage) rawMsgs[i];
                }

            } 
            else 
            {
                // Unknown tag type
                byte[] empty = new byte[] {};
                NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
                NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
                msgs = new NdefMessage[] { msg };
            }
        } 
        else 
        {
            //Log.e(TAG, "Unknown intent.");
            finish();
        }
        return msgs;
    }    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:42:11+00:00Added an answer on June 18, 2026 at 5:42 am

    You can call ndef.getNdefMessage() right after you do ndef.writeNdefMessage(message) to check the result. It will actually read the NDEF message back from the tag. However, in general, when ndef.writeNdefMessage(message) does not throw an exception, it is safe to assume the message was written successfully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that it is possible read and write data from mongodb via hadoop.
This is how I write to a stream then read from it using 1
Within a VB.NET program, I want to read files from a filesystem, then write
Possible Duplicate: Read Post Data submitted to ASP.Net Form I have a google checkout
Is possible to read from a txt file in loop like this? files names:
Is it possible to read alphanumeric characters only from the given input line ignoring
Is it possible to read intermittently-sent data through a named pipe using redirection to
Is it possible to read WPF ResourceDictionaries from WinForms? If yes, how?
is it possible to read mongodb data with hadoop connector but save output as
Is it possible to read from a file which is opened using open system

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.