简体   繁体   中英

how to read a card ISODEP NFC

i have to write a simple app to read a ISO B card

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tag_viewer);
    mTagContent = (LinearLayout) findViewById(R.id.list);
    mTitle = (TextView) findViewById(R.id.title);
    resolveIntent(getIntent());
}



void resolveIntent(Intent intent) {
    // Parse the intent
    String action = intent.getAction();
    Log.e(TAG, "toto ");
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
    try{
            myTag.connect();
            if (myTag.isConnected()){
                byte[] response = myTag.transceive(command);
                //logText(new String(response));
                Log.e(TAG, "Result " + response);
            }
            myTag.close();

        }catch (IOException e) {
               e.printStackTrace();
        }

    } else {
        Log.e(TAG, "Unknown intent " + intent);
        finish();
        return;
    }
}

my problem is i don't success to enter in the test if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)). I systematicaly have "Unknow intent".

concerning the manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
>
       <activity android:name="TagViewer"
        android:theme="@android:style/Theme.NoTitleBar"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
            <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
    </activity>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

thanks for your help.

Your Problem seems to be, that you're calling resolveIntent in the onCreate Method.

This should do the Trick:

    @Override
protected void onNewIntent(Intent pIntent) {
    super.onNewIntent(pIntent);

    resolveIntent(pIntent);

}

To get the Tag Object, I'm just using

Tag tagFromIntent = pIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        NfcA tag = NfcA.get(tagFromIntent);

in my onNewIntent-Method

Unfortunately I couldn't verify this for NfcB.

You need to create a NfcAdapter object

 private NfcAdapter mNfcAdapter;

than associate the object with your NFC in the onCreate method

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

do some checks before start the application, to be sure that the phone has the NFC

if (mNfcAdapter == null) { 
  // Stop here, we definitely need NFC
  Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
  finish();
  return;
}

on the onResume method enable the Dispatch

@Override
public void onResume() {
  super.onResume();
  Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
  PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
  mNfcAdapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}

on the onPause method disable the Dispatch

@Override
protected void onPause() {
  mNfcAdapter.disableForegroundDispatch(activity);
}

finally override the onNewIntent method to handle the intent

@Override
protected void onNewIntent(Intent intent) {
  //handle intent here
}

in your case call "resolveIntent(intent)" inside the onNewIntent method than follow this post to handle the IsoDep tag: Read data from NFC tag (IsoDep)

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