简体   繁体   English

与Android NFC通信到ISO 14443卡

[英]Communicating to an iso 14443 card with android nfc

I have been trying to write a basic android app that could send APDUs to an ISO 14443 card and receive response APDUs. 我一直在尝试编写一个基本的Android应用,该应用可以将APDU发送到ISO 14443卡并接收响应APDU。 I already have my applet in the smart card with ID F000000A. 我的小程序已经在ID为F000000A的智能卡中了。 So far I have written/modified other codes but I know im doing something wrong as my app crashes on my phone as I try to launch it. 到目前为止,我已经编写/修改了其他代码,但是当我尝试启动应用程序时,我的应用程序在手机上崩溃时,我知道自己做错了什么。 Below is my code just to mention. 下面是我要提及的代码。 I have my Isodep transceiver in a different class and also my isodep adapter in a different class. 我的Isodep收发器处于不同的类别,而我的isodep适配器处于不同的类别。

IsodepAdapter : IsodepAdapter

package kitkat.com.example.kaudi;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class IsoDepAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<String> messages = new ArrayList<String>(100);
    private int messageCounter;

    public IsoDepAdapter(LayoutInflater layoutInflater) {
        this.layoutInflater = layoutInflater;
    }

    public void addMessage(String message) {
        messageCounter++;
        messages.add("Message [" + messageCounter + "]: " + message);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return messages == null ? 0 : messages.size();
    }

    @Override
    public Object getItem(int position) {
        return messages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        TextView view = (TextView)convertView.findViewById(android.R.id.text1);
        view.setText((CharSequence)getItem(position));
        return convertView;
    }
}

Isodeptransceive: Isodeptransceive:

package kitkat.com.example.kaudi;

import java.io.IOException;
import android.nfc.tech.IsoDep;

public class IsoDepTransceiver implements Runnable {
    public interface OnMessageReceived {
        void onMessage(byte[] message);
        void onError(Exception exception);
    }

    private IsoDep isoDep;
    private OnMessageReceived onMessageReceived;

    public IsoDepTransceiver(IsoDep isoDep, OnMessageReceived onMessageReceived) {
        this.isoDep = isoDep;
        this.onMessageReceived = onMessageReceived;
    }

    private static final byte[] CLA_INS_P1_P2 = { 0x00, (byte)0xA4, 0x04, 0x00 };
    private static final byte[] AID_ANDROID = { (byte)0xF0, 0x00, 0x00, 0x0A };

    private byte[] createSelectAidApdu(byte[] aid) {
        byte[] result = new byte[6 + aid.length];
        System.arraycopy(CLA_INS_P1_P2, 0, result, 0, CLA_INS_P1_P2.length);
        result[4] = (byte)aid.length;
        System.arraycopy(aid, 0, result, 5, aid.length);
        result[result.length - 1] = 0;
        return result;
    }

    @Override
    public void run() {
        int messageCounter = 0;
        try {
            isoDep.connect();
            byte[] response = isoDep.transceive(createSelectAidApdu(AID_ANDROID));
            while (isoDep.isConnected() && !Thread.interrupted()) {
                String message = "Message from IsoDep " + messageCounter++;
                response = isoDep.transceive(message.getBytes());
                onMessageReceived.onMessage(response);
            }
            isoDep.close();
        }
        catch (IOException e) {
            onMessageReceived.onError(e);
        }
    }
}

NfcReader: NfcReader:

package kitkat.com.example.kaudi;

import kitkat.com.example.kaudi.IsoDepTransceiver.OnMessageReceived;
import android.app.Activity;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.ReaderCallback;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class NfcReader extends Activity implements OnMessageReceived, ReaderCallback {
    private static String TAG = NfcReader.class.getSimpleName();
    private NfcAdapter nfcAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView result = (TextView) findViewById(R.id.refTextView);

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        nfcAdapter.disableReaderMode(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A, null);
    }

    public void onTagDiscovered(Tag tag) {
        Log.d(TAG, "HCEfound"); 
        IsoDep isoDep = IsoDep.get(tag);
        IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
        transceiver.run();    
    }


    @Override
    public void onMessage(final byte[] message) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                String readFromHce = new String(message);
                TextView result = (TextView) findViewById(R.id.refTextView);
                result.setText(readFromHce);
            }
        });
    }

    @Override
    public void onError(Exception exception) {
        onMessage(exception.getMessage().getBytes());
    }
}

Manifest: 表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kitkat.com.example.kaudi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.NFC"/>
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="kitkat.com.example.kaudi.MainActivity"
            android:label="@string/app_name" >
            <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"/>
                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
             </intent-filter>
             <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                        android:resource="@xml/filter_nfc"/>
        </activity>
    </application>
</manifest>

LogCat (error) LogCat(错误)

05-03 19:54:58.713: E/AndroidRuntime(5247): FATAL EXCEPTION: main
05-03 19:54:58.713: E/AndroidRuntime(5247): Process: kitkat.com.example.kaudi, PID: 5247
05-03 19:54:58.713: E/AndroidRuntime(5247): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kitkat.com.example.kaudi/kitkat.com.example.kaudi.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "kitkat.com.example.kaudi.MainActivity" on path: DexPathList[[zip file "/data/app/kitkat.com.example.kaudi-2.apk"],nativeLibraryDirectories=[/data/app-lib/kitkat.com.example.kaudi-2, /vendor/lib, /system/lib]]
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.os.Looper.loop(Looper.java:136)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.reflect.Method.invoke(Method.java:515)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at dalvik.system.NativeStart.main(Native Method)
05-03 19:54:58.713: E/AndroidRuntime(5247): Caused by: java.lang.ClassNotFoundException: Didn't find class "kitkat.com.example.kaudi.MainActivity" on path: DexPathList[[zip file "/data/app/kitkat.com.example.kaudi-2.apk"],nativeLibraryDirectories=[/data/app-lib/kitkat.com.example.kaudi-2, /vendor/lib, /system/lib]]
05-03 19:54:58.713: E/AndroidRuntime(5247):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-03 19:54:58.713: E/AndroidRuntime(5247):     ... 11 more

You did not post any code for an activity named kitkat.com.example.kaudi.MainActivity . 您没有为名为kitkat.com.example.kaudi.MainActivity的活动发布任何代码。 As the exception indicates exactly this -- there is no activity named MainActivity on your class path -- I assume that you actually wanted to declare kitkat.com.example.kaudi.NfcReader in your manifest. 异常恰恰表明了这一点–类路径上没有名为MainActivity活动–我假设您实际上想在清单中声明kitkat.com.example.kaudi.NfcReader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 支持NFC的Android手机和ISO / IEC 14443-4智能卡之间的6A82 APDU通信问题 - 6A82 APDU communication issue between NFC enabled Android phone and ISO/IEC 14443-4 Smart Card ISO 14443 A型卡使用Android读/写 - ISO 14443 Type A Card Read/Write using Android 与ISO / IEC14443-3a(Layer3交换)进行通信以进行NFC交换Mifare Plus卡 - Communicate with ISO/IEC14443-3a (Layer3 exchange) for NFC exchange Mifare Plus card 从 android nfc 中的 NFC Type B (ISO 14443-3B) 读取数据; 得到 NoClassDefFoundError - Reading data from NFC Type B (ISO 14443-3B) in android nfc; getting NoClassDefFoundError 通过Android 2.3.3 NFC读取PayWave(ISO14443-B格式)跟踪信息 - Reading PayWave (ISO14443-B format) track information through Android 2.3.3 NFC 通过Android NFC读取静态ePassport(ISO 14443-4,MRTD)标签ID - Read static ePassport (ISO 14443-4, MRTD) tag id via Android NFC Android NFC(从ISO-14443 B型标签到手机的ATQB) - Android NFC (ATQB from ISO-14443 Type B tag to phone) Android:14443 A型卡的HCE - Android: HCE of 14443 type A card NfcA收发命令文档NFC-A(ISO 14443-3A) - NfcA transceive commands documentation NFC-A (ISO 14443-3A) Android应用程序将标签发送到无接触式阅读器ISO 14443 A或B设备 - Android Application send tag to contacless reader ISO 14443 A or B device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM