Question

I would like to write data on the tag (ISO 15963) after the button is clicked. I can write data on the tag when activity is open. I just don't know how to do the application start to cooperate with tag after click button. Is there any way to do this without calling a new activity? Thank you a lot!

CODE:

public class WrBlock extends Activity {

private Button wbutton;
private NfcAdapter myNfcAdapter;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private PendingIntent mPendingIntent;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blockw);

    wbutton = (Button) findViewById(R.id.button2);

    myNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
            getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter intnfcv = new IntentFilter(
            NfcAdapter.ACTION_TECH_DISCOVERED);
    mFilters = new IntentFilter[] { intnfcv, };
    mTechLists = new String[][] { new String[] { NfcV.class.getName() },
            new String[] { NdefFormatable.class.getName() } };

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

            // HERE I WANT TO WRITE DATA

        }
    });
}

public void onNewIntent(Intent intent) {
    // onResume gets called after this to handle the intent
    setIntent(intent);
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent()
                .getAction())) {
            Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
            showMessage(null, "Technologies available in this tag="
                    + Arrays.toString(tag.getTechList()));
            Tag detectedTag = getIntent().getParcelableExtra(
                    NfcAdapter.EXTRA_TAG);
            NfcV nfcv = NfcV.get(detectedTag);
            try {
                nfcv.connect();
                if (nfcv.isConnected()) {
                    showMessage(null, "Connected to the tag");
                    showMessage(null,
                            "\nTag DSF: " + Byte.toString(nfcv.getDsfId()));
                    byte[] buffer;
                    buffer = nfcv.transceive(new byte[] {0x00, 0x21, (byte) 0,0x31, 0x31, 0x31, 0x31});
                    showMessage(null, "\nByte block 10:" + buffer);

                    nfcv.close();
                    wrmode = false;
                    showMessage(null, "Successfully writing");
                } else
                    showMessage(null, "Not connected to the tag");
            } catch (IOException e) {
                showMessage(null, "Error");
                wrmode = false;
            }
        } else {
            showMessage(null, "ELSE ");
            wrmode = false;
    }
}

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

@Override
public void onResume() {
    super.onResume();
    if (myNfcAdapter != null) {
        myNfcAdapter.enableForegroundDispatch(this, mPendingIntent,
                mFilters, mTechLists);
    }
}

    public void showMessage(View v, CharSequence x) {
    Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
}

}

Was it helpful?

Solution

You can store the Tag in a field of the Activity. Doing this you can execute the logic of tag operations when the button is clicked. With these modifications, it should work:

public class MainActivity extends Activity {

    private Tag mTag;
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private PendingIntent mPendingIntent;
    private Button wbutton;
    private NfcAdapter myNfcAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wbutton = (Button) findViewById(R.id.button1);
        wbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeTag(mTag);
            }
        });
        configureNfc();
    }

    private void configureNfc(){
        myNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter intnfcv = new IntentFilter(
                NfcAdapter.ACTION_TECH_DISCOVERED);
        mFilters = new IntentFilter[] { 
                intnfcv};
        mTechLists = new String[][] { new String[] {
                NfcV.class.getName() }};
    }

    private synchronized void writeTag(Tag tag){
        if(mTag !=null){
            try {
                new WriteVecinityTagTask().execute().get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            mTag=null;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (myNfcAdapter != null) {
            myNfcAdapter.enableForegroundDispatch(this, mPendingIntent,
                    mFilters, mTechLists);
        }
    }

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

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
            mTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
        }
    }

    private class WriteVecinityTagTask extends AsyncTask<Void, Void, Boolean>{

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            String message = (result) ? "WRITE OK" : "ERROR";
            Toast.makeText(MainActivity.this, message , Toast.LENGTH_SHORT).show();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            try{
                NfcV vecinityTag = NfcV.get(mTag);
                vecinityTag.connect();
                vecinityTag.transceive(
                        new byte[] {0x00, 0x21, (byte) 0,0x31, 0x31, 0x31, 0x31}
                        );
                vecinityTag.close();
            }catch(IOException e){
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top