Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1087191
  • 博文数量: 282
  • 博客积分: 10865
  • 博客等级: 上将
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-12 12:35
文章存档

2017年(1)

2016年(3)

2015年(10)

2014年(12)

2013年(5)

2012年(10)

2011年(29)

2010年(3)

2008年(13)

2007年(92)

2006年(104)

我的朋友

分类: Java

2014-11-26 21:20:39

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

}



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{

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

阅读(1573) | 评论(0) | 转发(0) |
0

上一篇:Android NfcV

下一篇:pboc emv tag

给主人留下些什么吧!~~