1

Hello fellow programmers!

I am really new to Android, 2 weeks old.

I am trying to do some programming with NFC. I have Nexus 5 and I am trying to read and display UID of the MIFARE Classic 1k. I know all the protocol compatibilities issues with the Broadcom chip, so we can skip that and go straight to fact that you can read UID with no problem.

I want to catch the intent and show a toast with read UID. So far I made it work by putting performIntent into onCreate method. By making the intent to restart my activity I am able to handle that intent and show its UID via toast and that all works. Here is my humble code: MainActivity.java

package sanjin.com.nfc;

import android.content.Intent;
import android.nfc.NfcAdapter;
import android.app.Activity;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;



public class MainActivity extends Activity  {
    NfcAdapter mNfcAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        Boolean nfcEnabled = mNfcAdapter.isEnabled();
        if (nfcEnabled){
            Toast.makeText(MainActivity.this,
                    R.string.turned_on,
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this,
                    R.string.turned_off,
                    Toast.LENGTH_SHORT).show();
        }
        performIntent(getIntent());
    }



    private String serialId = "";


    @Override
    public void onResume() {
        super.onResume();
        performIntent(getIntent());
    }

    private void performIntent(Intent intent) {
        String action = intent.getAction();
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
            try {

                byte[] tagId = tag.getId();
                serialId = toHexString(tagId);
                Log.d("[ReadCardTools]", "Serial Number: " + serialId);
                Toast.makeText(this, serialId,Toast.LENGTH_SHORT).show();
            } catch (NullPointerException ex) {
                ex.printStackTrace();
                serialId = "ERROR";
            }
        }
    }

    public static String toHexString(byte[] bytes) {
        char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for ( int j = 0; j < bytes.length; j++ ) {
            v = bytes[j] & 0xFF;
            hexChars[j*2] = hexArray[v/16];
            hexChars[j*2 + 1] = hexArray[v%16];
        }
        return new String(hexChars);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

And manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sanjin.com.nfc">

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />
    <application
        android:label="@string/app_name"
        android:launchMode="singleTask">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Now, I want this intent to just show the toast without starting the activity again. I realize its working because when the activity restarts, intent gets handled and show me my UID.

I just want the incoming intent to show the toast containing its UID.

Sorry if this was asked before, I tried finding it but no luck.

Thanks!

4

1 回答 1

0
public class MainActivity extends AppCompatActivity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    NfcAdapter nfcAdapter;
    TextView uidView;


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

        uidView = (TextView)findViewById(R.id.mTextView);

        nfcAdapter = NfcAdapter.getDefaultAdapter(MainActivity.this);

        Boolean nfcEnabled = nfcAdapter.isEnabled();
        if (nfcEnabled){
            Toast.makeText(MainActivity.this,
                    R.string.turned_on,
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this,
                    R.string.turned_off,
                    Toast.LENGTH_SHORT).show();
        }
        Intent intent = getIntent();
        resolveIntent(intent);

    }

    protected void resolveIntent(Intent intent) {
        Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (nfcTag == null) {
            Log.w(LOG_TAG, "Unable to obtain NFC tag from intent!");
        } else {
            String tagId = bytesToHex(nfcTag.getId());
            uidView.setText(tagId);
            //Toast.makeText(getApplicationContext(), tagId, Toast.LENGTH_LONG).show();

        }
    }
    public static String bytesToHex(byte[] bytes) {
        StringBuilder ret = new StringBuilder();
        if (bytes != null) {
            for (Byte b : bytes) {
                ret.append(String.format("%02X", b.intValue() & 0xFF));
            }
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i <= ret.length() - 2; i = i + 2) {
            result.append(new StringBuilder(ret.substring(i, i + 2)).reverse());
        }
        String card = result.reverse().toString();

        if(card.length() == 8){
            card = "0" + Long.parseLong(card, 16) + "\n\n";
        }
        if(card.length() == 9){
            card = Long.parseLong(card, 16) + "\n\n";
        }
        return card;
    }

}
于 2019-08-10T02:17:21.227 回答