簡體   English   中英

如何讀取卡ISODEP NFC

[英]how to read a card ISODEP NFC

我必須編寫一個簡單的應用程序才能讀取ISO B卡

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;
    }
}

我的問題是,如果(NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)),我無法成功進入測試。 我系統地具有“未知意圖”。

關於清單:

<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" />

謝謝你的幫助。

您的問題似乎是,您在onCreate方法中調用resolveIntent。

這應該做的把戲:

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

    resolveIntent(pIntent);

}

要獲取標簽對象,我只是在使用

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

        NfcA tag = NfcA.get(tagFromIntent);

在我的onNewIntent-Method中

不幸的是,我無法為NfcB驗證這一點。

您需要創建一個NfcAdapter對象

 private NfcAdapter mNfcAdapter;

而不是在onCreate方法中將對象與NFC關聯

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

在啟動應用程序之前進行一些檢查,以確保手機具有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;
}

在onResume方法上啟用分派

@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);
}

在onPause方法上禁用調度

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

最后重寫onNewIntent方法來處理意圖

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

您在onNewIntent方法內部調用“ resolveIntent(intent)”,而不是按照本文處理IsoDep標簽: 從NFC標簽(IsoDep)讀取數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM