从前台调度中读取标签NFC ID

问题描述 投票:1回答:1

我正在尝试读取NFC标签ID。我知道我无法读取数据,因为它是一个安全的标签(MBTA票价卡)。我想阅读标签唯一ID和吐司那个值。我已成功获得NFC意图工作,因此当扫描标签时,我的应用程序会尝试处理它。但是应用程序不会显示标签为id的toast。

    package com.example.nfctest;

import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.NfcF;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {
     private NfcAdapter mAdapter;
        private PendingIntent mPendingIntent;
        private IntentFilter[] mFilters;
        private String[][] mTechLists;
        private int mCount = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = NfcAdapter.getDefaultAdapter(this);

        // Create a generic PendingIntent that will be deliver to this activity. The NFC stack
        // will fill in the intent with the details of the discovered tag before delivering to
        // this activity.
        mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Setup an intent filter for all MIME based dispatches



    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {

    };

    // Setup a tech list for all NfcF tags
    mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}
public void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

}

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
    CharSequence text = ("Discovered tag " + ++mCount + " with intent: " + intent);
    int duration = Toast.LENGTH_SHORT;

    Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);


    Toast toast = Toast.makeText(MainActivity.this,myTag.getId().toString() , duration);
    toast.show();
}


@Override
public void onPause() {
    super.onPause();
    //mAdapter.disableForegroundDispatch(this);
    throw new RuntimeException("onPause not implemented to fix build");
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

它不再崩溃,只是不用ID来祝酒。

android nfc
1个回答
0
投票

我强烈推荐this sample application中的这个this link。不要像过去那样通过尝试一切来轻松操作来给自己带来痛苦。

您可能在onResume和onNewIntent中的操作放置中做错了。但是,在我链接的示例中,它在onResume和onNewIntent中都有对新标记和ndef的控制。如果你真的想继续自己的项目,试试这个。

© www.soinside.com 2019 - 2024. All rights reserved.