NFC-V写标签+ onClickListener

时间:2014-02-08 23:24:28

标签: android nfc iso-15693

我想在单击按钮后在标签(ISO 15963)上写入数据。当活动打开时,我可以在标签上写入数据。我只是不知道如何在点击按钮后开始与标签配合应用程序。有没有办法在不调用新活动的情况下这样做?非常感谢你!

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();
}

}

1 个答案:

答案 0 :(得分:1)

您可以将标记存储在活动的字段中。这样做可以在单击按钮时执行标记操作的逻辑。通过这些修改,它应该可以工作:

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;
        }
    }
}