Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用android nfc与iso 14443卡通信_Android_Eclipse_Android Intent_Nfc_Apdu - Fatal编程技术网

使用android nfc与iso 14443卡通信

使用android nfc与iso 14443卡通信,android,eclipse,android-intent,nfc,apdu,Android,Eclipse,Android Intent,Nfc,Apdu,我一直在尝试编写一个基本的android应用程序,它可以将APDU发送到ISO 14443卡并接收APDU的响应。我在智能卡中已经有ID为F000000A的小程序。到目前为止,我已经编写/修改了其他代码,但我知道我做了一些错误的事情,因为我的应用程序在我的手机上崩溃,因为我试图启动它。下面是我的代码,仅供参考。我的Isodep收发器在不同的类中,我的Isodep适配器也在不同的类中 ISODEP适配器: NfcReader: 舱单: 您没有发布名为kitkat.com.example.kaudi

我一直在尝试编写一个基本的android应用程序,它可以将APDU发送到ISO 14443卡并接收APDU的响应。我在智能卡中已经有ID为F000000A的小程序。到目前为止,我已经编写/修改了其他代码,但我知道我做了一些错误的事情,因为我的应用程序在我的手机上崩溃,因为我试图启动它。下面是我的代码,仅供参考。我的Isodep收发器在不同的类中,我的Isodep适配器也在不同的类中

ISODEP适配器:

NfcReader:

舱单:


您没有发布名为kitkat.com.example.kaudi.MainActivity的活动的任何代码。由于异常正好表明了这一点—您的类路径上没有名为MainActivity的活动—我假设您实际上想要在清单中声明kitkat.com.example.kaudi.NfcReader。

请发布代码—将代码粘贴到您的问题中,而不是链接—实际上您甚至没有链接它们。是否也发布错误跟踪?当你的应用程序崩溃时,你会发现。@MichaelRoland您好。.感谢您的回复…我已经这样做了…我以前认为它看起来太乱了:另外请注意,特别是如果你使用Java卡,但也使用许多其他智能卡,如果您尝试使用少于5字节的辅助数据,并且尝试交换根据ISO 7816-4格式不正确的APDU,您可能会遇到问题。是的……您是对的……现在我的应用程序没有崩溃……但不幸的是,我目前没有卡可以尝试……请在从运行应用程序后,我还有一个问题eclipse…如何发送后续命令..是否来自apdutool.bat??我很感激,我不明白你的意思。您可以使用IsoDep.transceive从Android应用程序向卡发送命令。。。方法。很抱歉,但我不太明白…在构建和运行应用程序之后…是否可以添加代码并发送APDU…或者我是否在应用程序中创建一些界面,在其中键入并发送APDU…抱歉我的愚蠢…抱歉,但我无法帮你在应用程序中做出基本的设计决策。您通常会根据自己的需要进行设计。如果您需要手动键入apdu,那么您肯定需要为此设计一个UI。不过,从可用性的角度来看,让用户在手持设备卡的同时键入APDUs并不是一个好主意。
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;
    }
}
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);
        }
    }
}
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());
    }
}
<?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>
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