【发布时间】:2015-04-16 08:19:25
【问题描述】:
我正在尝试使用 HCE 将图像从一台设备发送到另一台设备(一台设备处于卡仿真模式,另一台处于读卡器模式)。我可以发送字符串但不能发送图像。在读者方面,我总是将 TAG 设为null。
非常感谢您的帮助,在此先感谢。
HCE 端(标签端/发送者):
private final static byte[] SELECT_APP = new byte[] { (byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x07, (byte)0xd2, (byte)0x76, (byte)0x00, (byte)0x00, (byte)0x85, (byte)0x01, (byte)0x01, (byte)0x00, };
private final static byte[] SELECT_CC_FILE = new byte[] { (byte)0x00, (byte)0xa4, (byte)0x00, (byte)0x0c, (byte)0x02, (byte)0xe1, (byte)0x03, };
private final static byte[] SELECT_NDEF_FILE = new byte[] { (byte)0x00, (byte)0xa4, (byte)0x00, (byte)0x0c, (byte)0x02, (byte)0xe1, (byte)0x04, };
private final static byte[] SUCCESS_SW = new byte[] { (byte)0x90, (byte)0x00, };
private final static byte[] FAILURE_SW = new byte[] { (byte)0x6a, (byte)0x82, };
private final static byte[] CC_FILE = new byte[] {
0x00, 0x0f, // CCLEN
0x20, // Mapping Version
0x00, 0x3b, // Maximum R-APDU data size
0x00, 0x34, // Maximum C-APDU data size
0x04, 0x06, // Tag & Length
(byte)0xe1, 0x04, // NDEF File Identifier
0x00, 0x32, // Maximum NDEF size
0x00, // NDEF file read access granted
(byte)0xff, // NDEF File write access denied
};
public void onCreate() {
super.onCreate();
mAppSelected = false;
mCcSelected = false;
mNdefSelected = false;
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.transferimage1);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] byteArray = stream.toByteArray();
NdefRecord picRecord = NdefRecord.createMime("image/jpeg", byteArray);
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] { picRecord });
int nlen = ndefMessage.getByteArrayLength(); // <- this is 164906
mNdefRecordFile = new byte[nlen + 2];
mNdefRecordFile[0] = (byte)((nlen & 0xff00) / 256);
mNdefRecordFile[1] = (byte)(nlen & 0xff);
System.arraycopy(ndefMessage.toByteArray(), 0, mNdefRecordFile, 2, ndefMessage.getByteArrayLength());
}
@Override
public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
if (Arrays.equals(SELECT_APP, commandApdu)) {
mAppSelected = true;
mCcSelected = false;
mNdefSelected = false;
return SUCCESS_SW;
} else if (mAppSelected && Arrays.equals(SELECT_CC_FILE, commandApdu)) {
mCcSelected = true;
mNdefSelected = false;
return SUCCESS_SW;
} else if (mAppSelected && Arrays.equals(SELECT_NDEF_FILE, commandApdu)) {
mCcSelected = false;
mNdefSelected = true;
return SUCCESS_SW;
} else if (commandApdu[0] == (byte)0x00 && commandApdu[1] == (byte)0xb0) {
int offset = (0x00ff & commandApdu[2]) * 256 + (0x00ff & commandApdu[3]);
int le = 0x00ff & commandApdu[4];
byte[] responseApdu = new byte[le + SUCCESS_SW.length];
if (mCcSelected && offset == 0 && le == CC_FILE.length) {
System.arraycopy(CC_FILE, offset, responseApdu, 0, le);
System.arraycopy(SUCCESS_SW, 0, responseApdu, le, SUCCESS_SW.length);
return responseApdu;
} else if (mNdefSelected) {
if (offset + le <= mNdefRecordFile.length) {
System.arraycopy(mNdefRecordFile, offset, responseApdu, 0, le);
System.arraycopy(SUCCESS_SW, 0, responseApdu, le, SUCCESS_SW.length);
return responseApdu;
}
}
}
return FAILURE_SW;
}
@Override
public void onDeactivated(int reason) {
mAppSelected = false;
mCcSelected = false;
mNdefSelected = false;
}
阅读器模式应用(阅读器端/接收器):
@Override
public void onTagDiscovered(Tag tag) {
Ndef ndef = Ndef.get(tag); // <- this returns null
NdefMessage message;
if (ndef != null) {
message = ndef.getCachedNdefMessage(); // <- cannot get NDEF message as ndef is null
} else {
return;
}
}
【问题讨论】:
-
如果您的 TAG 为空,您可能会收到空指针异常,请发布 Log Cat 跟踪。
-
您只能使用 NFC。 HCE 用于其他目的。
-
您使用什么代码在 HCE 端“发送”图像?您使用什么代码在阅读器端“接收”图像?
-
@MichaelRoland 我已经更新了代码!任何帮助将不胜感激。提前谢谢!
-
SELECT_*、*_FILE、*_SW 字段的值是多少?