Android:NFC讀取校園卡

來源:互聯網
上載者:User

Android:NFC讀取校園卡

主程式:

package com.nfclab.stuCard;import java.io.IOException;import android.app.Activity;import android.app.PendingIntent;import android.content.Intent;import android.content.IntentFilter;import android.nfc.NdefMessage;import android.nfc.NdefRecord;import android.nfc.NfcAdapter;import android.nfc.Tag;import android.nfc.tech.Ndef;import android.nfc.tech.NdefFormatable;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class stuCardActivity extends Activity {       private NfcAdapter mNfcAdapter;    private PendingIntent mPendingIntent;    private IntentFilter[] mFilters;    private String[][] mTechLists;    private String studentId="";    private String studentName=" ";        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final EditText studentIdEditText = (EditText)this.findViewById(R.id.studentIdEditText);        final EditText studentNameEditText = (EditText)this.findViewById(R.id.studentNameEditText);        Button writeStudentButton = (Button)this.findViewById(R.id.writeStudentButton);        writeStudentButton.setOnClickListener(new android.view.View.OnClickListener()         {            public void onClick(View view) {                studentId = studentIdEditText.getText().toString();                studentName = studentNameEditText.getText().toString();                 TextView messageText = (TextView)findViewById(R.id.messageText);                 messageText.setText("Touch NFC Tag to write \n");                 messageText.append("Student id:" + studentId + "\nStudent Name: " + studentName );            }        });            Button exitButton = (Button)findViewById(R.id.exitButton);         exitButton.setOnClickListener(new android.view.View.OnClickListener()         {            public void onClick(View v) {                finish();            }        });            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);           mPendingIntent = PendingIntent.getActivity(this, 0,                 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);             IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);             //ndef.addDataScheme("http");             mFilters = new IntentFilter[] {              ndef,             };           mTechLists = new String[][] { new String[] { Ndef.class.getName() },                   new String[] { NdefFormatable.class.getName() }};    }        @Override    public void onResume() {        super.onResume();        if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,                mTechLists);    }    @Override    public void onNewIntent(Intent intent) {        Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);                  String externalType = "nfclab.com:transport";        String payload = studentId+":"+studentName;        NdefRecord extRecord1 = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, externalType.getBytes(), new byte[0], payload.getBytes());        NdefMessage newMessage = new NdefMessage(new NdefRecord[] { extRecord1});        writeNdefMessageToTag(newMessage, tag);       }    @Override    public void onPause() {        super.onPause();        mNfcAdapter.disableForegroundDispatch(this);    }        boolean writeNdefMessageToTag(NdefMessage message, Tag detectedTag) {        int size = message.toByteArray().length;        try {            Ndef ndef = Ndef.get(detectedTag);            if (ndef != null) {                ndef.connect();                if (!ndef.isWritable()) {                    Toast.makeText(this, "Tag is read-only.", Toast.LENGTH_SHORT).show();                    return false;                }                if (ndef.getMaxSize() < size) {                    Toast.makeText(this, "The data cannot written to tag, Tag capacity is " + ndef.getMaxSize() + " bytes, message is " + size + " bytes.", Toast.LENGTH_SHORT).show();                    return false;                }                ndef.writeNdefMessage(message);                ndef.close();                                Toast.makeText(this, "Message is written tag.", Toast.LENGTH_SHORT).show();                return true;            } else {                NdefFormatable ndefFormat = NdefFormatable.get(detectedTag);                if (ndefFormat != null) {                    try {                        ndefFormat.connect();                        ndefFormat.format(message);                        ndefFormat.close();                        Toast.makeText(this, "The data is written to the tag ", Toast.LENGTH_SHORT).show();                        return true;                    } catch (IOException e) {                         Toast.makeText(this, "Failed to format tag", Toast.LENGTH_SHORT).show();                        return false;                    }                } else {                     Toast.makeText(this, "NDEF is not supported", Toast.LENGTH_SHORT).show();                    return false;                }            }        } catch (Exception e) {            Toast.makeText(this, "Write opreation is failed", Toast.LENGTH_SHORT).show();        }        return false;    }}
    

布局檔案:

                                            

設定檔:

                                                                                            


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.