如何使用Android应用程序读取nfc标记

时间:2014-03-27 08:29:28

标签: android nfc

大家好我正在使用android应用程序,我需要扫描设备的nfc标签。我是nfc的新手,在阅读了很多教程之后,我找到了一些方法来检查nfc是否在手机中启用但是我没有得到如何阅读nfc标签。

这是我的oncreat

protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(MainActivity.this);
        pref = getApplicationContext().getSharedPreferences("Cordinate", 0);
        editor = pref.edit();
        nfc1 = (ImageView) findViewById(R.id.checknfc1);
        mDisplay = (TextView) findViewById(R.id.display);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password1);
        login = (TextView) findViewById(R.id.btnRegister);
        context = getApplicationContext();

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        imei = telephonyManager.getDeviceId();


        gcm = GoogleCloudMessaging.getInstance(this);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // Toast.makeText(MainActivity.this, "i  am workin",
                // Toast.LENGTH_LONG).show();

                GPSService gpstest =  new GPSService(imei,MainActivity.this);
                Thread data2 = new Thread(gpstest);
                data2.start();
                gps = new GPSTracker(MainActivity.this);
                if(gps.canGetLocation()){

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    // \n is for new line
                    //Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();   
                }
                else
                {

                    Toast.makeText(getApplicationContext(), "GPS is not working", Toast.LENGTH_LONG).show(); 
                }
                username1 = username.getText().toString();
                password2 = password.getText().toString();
                username.setText("");
                password.setText("");
                new RegisterBackground().execute();

            }
        });


        nfc1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                if (mNfcAdapter != null) {
                   // "Read an NFC tag"
                    Toast.makeText(MainActivity.this, "Read an NFC tag", Toast.LENGTH_LONG).show();
                     // create an intent with tag data and deliver to this activity
                    mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
                        new Intent(MainActivity.this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

                    // set an intent filter for all MIME data
                    IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

                    onNewIntent(getIntent());
                    try {
                        ndefIntent.addDataType("*/*");
                        mIntentFilters = new IntentFilter[] { ndefIntent };
                    } catch (Exception e) {
                        Log.e("TagDispatch", e.toString());
                    }
                } else {
                    Toast.makeText(MainActivity.this, "not able to read NFC tag", Toast.LENGTH_LONG).show();
                }

                //Toast.makeText(MainActivity.this, datanfc, Toast.LENGTH_LONG).show();


            }
        });

    }

我的onNewIntent

public void onNewIntent(Intent intent) {
    String action = intent.getAction();
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

   String nfctag =tag.toString();

   Toast.makeText(MainActivity.this, nfctag, Toast.LENGTH_LONG).show();

}

1 个答案:

答案 0 :(得分:0)

有关详细信息http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

,请参阅此文档

基本上,首先您应该在ACTION_TECH_DISCOVERED

中标记要标记的标记类型

然后相应地更新清单,

<activity>
...
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
    android:resource="@xml/nfc_tech_filter" />
...
</activity>

然后从扫描的标签中获取数据

public void onResume() {
    super.onResume();
    ...
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        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];
            }
        }
    }
    //process the msgs array
}