我一直在嘗試編寫一個基本的Android應用程序,它可以將APDU發送到ISO 14443卡並接收響應APDU。我已經在ID爲F000000A的智能卡中有我的小程序。到目前爲止,我已經編寫/修改了其他代碼,但我知道即使我嘗試啓動它時,我的應用在我的手機上崩潰,我也會做一些錯誤的事情。下面是我的代碼,只是提及。我有我的Isodep收發器在不同的類,我的isodep適配器在不同的類。與Android nfc通信到ISO 14443卡
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:
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:
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>
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
請張貼您的代碼(將代碼粘貼到您的問題中,而不是鏈接(實際上您甚至沒有*鏈接*它們)圖片,併發布應用程序崩潰時出現的錯誤(stacktrace?) –
@MichaelRoland嗨。 。謝謝回覆...我已經這樣做了......我以前認爲它看起來太亂了:) – user3312054
還要注意(特別是如果您使用的是Java Card,但也有許多其他智能卡),您可能會遇到如果您嘗試使用少於5個字節的AID,並且您嘗試根據ISO 7816-4交換未正確形成的「APDU」,則會出現問題。 –