我编写和应用程序通过NFC连接Android系统,在谷歌Nexus 4和5上测试,一切正常,但谷歌Nexus 7(2012)不起作用。你能帮助我吗。 来自Java应用程序的代码:
if (cardTerminal.isCardPresent()) {
try {
Card card = cardTerminal.connect("direct");
ApduTagReaderWriter readerWriter = new ApduTagReaderWriter(new AcsDirectChannelTag(TagType.ISO_DEP,
null, card));
try {
IsoDepTamaCommunicator tamaCommunicator = new IsoDepTamaCommunicator(readerWriter, readerWriter);
String data = tamaCommunicator.connectAsInitiator();
}
}
}
这是IsoDepTamaCommunicator和inListPassiveTargetResp.getNumberOfTargets()在Nexus 7中返回0,但不与nexus 4,5一起返回。
public class IsoDepTamaCommunicator extends AbstractTamaCommunicator {
private static final String EMPTY_STRING = "";
private boolean IsConnected = false;
private static final byte[] CLA_INS_P1_P2 = { 0x00, (byte)0xA4, 0x04, 0x00 };
private static final byte[] AID_ANDROID = { (byte)0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
public IsoDepTamaCommunicator(ByteArrayReader reader, ByteArrayWriter writer) {
super(reader, writer);
}
public String connectAsInitiator() throws IOException {
InListPassiveTargetResp inListPassiveTargetResp = sendMessage(new InListPassiveTargetReq((byte) 1, (byte) 0,
new byte[0]));
if (inListPassiveTargetResp.getNumberOfTargets() > 0) {
System.out.println("TargetData: " + NfcUtils.convertBinToASCII(inListPassiveTargetResp.getTargetData()));
if (inListPassiveTargetResp.isIsoDepSupported()) {
System.out.println("IsoDep Supported");
byte[] selectAidApdu = createSelectAidApdu(AID_ANDROID);
DataExchangeResp resp = sendMessage(new DataExchangeReq(inListPassiveTargetResp.getTargetId(),
false, selectAidApdu, 0, selectAidApdu.length));
String dataIn = new String(resp.getDataOut());
if (dataIn.startsWith("test:")) {
dataIn = dataIn.substring(5, dataIn.length());
IsConnected = true;
return dataIn;
}
} else {
System.out.println("IsoDep NOT Supported");
return EMPTY_STRING;
}
}
return EMPTY_STRING;
}
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;
}
}
在Android设备上发送消息功能是:
public class MyHostApduService extends HostApduService {
@Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
String message = "test:message";
return message.getBytes();
}
@Override
public void onDeactivated(int reason) {
}
}
并在androidManifest.xml中添加了下一个服务:
<service
android:name=".MyHostApduService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="@xml/apduservice" />
</service>
一切正常,适用于Google Nexus 4和5,无法与Google Nexus 7配合使用。 在所有测试设备上安装Android 5.0版