简体   繁体   中英

Reading a nfc message from tag when user scans an nfc

I need to open my app and read the nfc message in the same time when user scans the nfc tags..

Currently i am able to open my when user taps or scans the nfc but i couldnt read the data in the nfc at the same time .

@TargetApi(Build.VERSION_CODES.GINGERBREAD)

@SuppressLint("NewApi") public class MainActivity extends Activity {

public static final String TAG = "NfcDemo";
private TextView mTextView;
// private NfcAdapter mNfcAdapter;
protected PendingIntent nfcPendingIntent;
NfcAdapter nfcAdapter;
IntentFilter[] readTagFilters;
PendingIntent pendingIntent;
Tag detectedTag;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView) findViewById(R.id.textView1);
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter tagDetected = new IntentFilter(
            NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter filter2 = new IntentFilter(
            NfcAdapter.ACTION_NDEF_DISCOVERED);
    readTagFilters = new IntentFilter[] { tagDetected, filter2 };
    Log.d(TAG, "ok" + getIntent().getAction());


}

public void enableForegroundMode() {
    Log.d(TAG, "enableForegroundMode");
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    Log.d("done ", "in " + tagDetected.getAction(0));
    IntentFilter[] writeTagFilters = new IntentFilter[] { tagDetected };

}
@Override
protected void onResume() {
    Log.d(TAG, "onResume");
    super.onResume();
    nfcAdapter.enableForegroundDispatch(this, pendingIntent,readTagFilters, null);
    Intent ndefDetected = new Intent(NfcAdapter.ACTION_TAG_DISCOVERED);
    Tag tag = ndefDetected.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    Parcelable[] rawMsgs = ndefDetected .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    if (rawMsgs != null) {
        Log.d(TAG, "onResume1");

    } else {
        Log.d(TAG, "onResume2");
    }
}
@Override
protected void onPause() {
    Log.d(TAG, "onPause");
    super.onPause();
    if (NfcAdapter.getDefaultAdapter(this) != null)
        NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    setIntent(intent);
    Log.d(TAG, "NewIntent is ...." + intent.getAction() + "....."
            + NfcAdapter.ACTION_NDEF_DISCOVERED);
    getTagMsg(intent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}

public void getTagMsg(Intent intent) {
    Log.d(TAG, "getTagContent" + intent.getAction() + "....."
            + NfcAdapter.ACTION_NDEF_DISCOVERED);
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

        Log.d(TAG, "New Intent/1");
        NdefMessage[] messages = null;
        Parcelable[] rawMsgs = intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            messages = new NdefMessage[rawMsgs.length];
            Log.d(TAG, "New Intent/2:" + messages);
            for (int i = 0; i < rawMsgs.length; i++) {
                messages[i] = (NdefMessage) rawMsgs[i];
            }
        }
        if (messages[0] != null) {
            String result = "";
            byte[] payload = messages[0].getRecords()[0].getPayload();
            // this assumes that we get back am SOH followed by host/code
            for (int b = 0; b < payload.length; b++) { // skip SOH
                result += (char) payload[b];
            }
            Log.d(TAG, "New Intent/3:" + result);
            Toast.makeText(getApplicationContext(),
                    "Tag Contains " + result, Toast.LENGTH_SHORT).show();
        }
    }

}

}

You are not really doing anything when your activity is started. You should call getTagMsg on the tag you grab from the Intent in your activity's onCreate . Towards the end of the method, simply call getTagMsg(detectedTag) .

You should probably have a helper method that starts from an Intent , grabs the parceable extra, and reads the contents of the tag from it. You could then use this method both in onCreate and onNewIntent .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM