use of android.nfc.tech.IsoDep in project Android-Developers-Samples by johnjohndoe.
the class LoyaltyCardReader method onTagDiscovered.
/**
* Callback when a new tag is discovered by the system.
*
* <p>Communication with the card should take place here.
*
* @param tag Discovered tag
*/
@Override
public void onTagDiscovered(Tag tag) {
Log.i(TAG, "New tag discovered");
// Android's Host-based Card Emulation (HCE) feature implements the ISO-DEP (ISO 14443-4)
// protocol.
//
// In order to communicate with a device using HCE, the discovered tag should be processed
// using the IsoDep class.
IsoDep isoDep = IsoDep.get(tag);
if (isoDep != null) {
try {
// Connect to the remote NFC device
isoDep.connect();
// Build SELECT AID command for our loyalty card service.
// This command tells the remote device which service we wish to communicate with.
Log.i(TAG, "Requesting remote AID: " + SAMPLE_LOYALTY_CARD_AID);
byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);
// Send command to remote device
Log.i(TAG, "Sending: " + ByteArrayToHexString(command));
byte[] result = isoDep.transceive(command);
// If AID is successfully selected, 0x9000 is returned as the status word (last 2
// bytes of the result) by convention. Everything before the status word is
// optional payload, which is used here to hold the account number.
int resultLength = result.length;
byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] };
byte[] payload = Arrays.copyOf(result, resultLength - 2);
if (Arrays.equals(SELECT_OK_SW, statusWord)) {
// The remote NFC device will immediately respond with its stored account number
String accountNumber = new String(payload, "UTF-8");
Log.i(TAG, "Received: " + accountNumber);
// Inform CardReaderFragment of received account number
mAccountCallback.get().onAccountReceived(accountNumber);
}
} catch (IOException e) {
Log.e(TAG, "Error communicating with card: " + e.toString());
}
}
}
use of android.nfc.tech.IsoDep in project NeuCardReader by liuyanyi.
the class CardReader method readCard.
public static CardInfo readCard(Activity activity, Intent intent) {
// TODO 拆分成多个方法
CardInfo card = new CardInfo(activity);
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tagFromIntent == null)
return null;
card.setHardwareId(Util.byteToHex(tagFromIntent.getId()));
String action = intent.getAction();
if (!"android.nfc.action.TAG_DISCOVERED".equals(action) && !"android.nfc.action.TECH_DISCOVERED".equals(action) && !"android.nfc.action.NDEF_DISCOVERED".equals(action)) {
return null;
}
IsoDep isoDep = IsoDep.get((Tag) intent.getParcelableExtra("android.nfc.extra.TAG"));
if (isoDep == null) {
// a(this.NfcMainfare);
return null;
}
try {
isoDep.connect();
// a(this.NfcMainfare);
// extraID();
isoDep.transceive(g.a(fInts.a));
byte[] transceive = isoDep.transceive(g.a(fInts.c));
if (transceive != null && g.c(transceive)) {
transceive = isoDep.transceive(g.a(fInts.d));
if (transceive == null || !g.c(transceive)) {
Util.aToast(activity.getString(R.string.read_failed), activity);
} else {
byte[] a;
transceive = isoDep.transceive(g.a(fInts.j));
// 名字
if (transceive != null && g.c(transceive)) {
a = g.a(transceive);
g.a(a, 0, transceive, 0, transceive.length - 2);
action = g.a(a, 0, a.length, "GB18030").trim();
// Log.d("名字", action);
if (Util.g(action)) {
card.setStudentName(action);
// Log.d("名字", action);
// aToast(action);
// Log.d("名字","r1");
}
}
transceive = isoDep.transceive(g.a(fInts.h));
// 卡号
if (transceive != null && g.c(transceive)) {
a = g.a(transceive);
g.a(a, 0, transceive, 0, transceive.length - 2);
action = g.a(a, 0, a.length, "GB18030").trim();
// Log.d("卡号", action);
card.setStudentId(action);
}
transceive = isoDep.transceive(g.a(fInts.personId));
// 身份证
if (transceive != null && g.c(transceive)) {
a = g.a(transceive);
g.a(a, 0, transceive, 0, transceive.length - 2);
action = g.a(a, 0, a.length, "GB18030").trim();
// Log.d("卡号", action);
card.setPersonId(action);
}
transceive = isoDep.transceive(g.a(fInts.g));
// 学院
if (transceive != null && g.c(transceive)) {
a = g.a(transceive);
g.a(a, 0, transceive, 0, transceive.length - 2);
action = g.a(a, 0, a.length, "GB18030").trim();
// Log.d("***", action);
if (Util.d(action) || Util.a(action) || action.contains("000000")) {
// bVar.d(action);
// Log.d("学院******", action);
} else {
// bVar.c(action);
// Log.d("学院***", action);
card.setStudentDept(action);
}
}
int intValue;
transceive = isoDep.transceive(g.a(fInts.e));
// 余额
if (transceive != null && g.c(transceive)) {
transceive = isoDep.transceive(g.a(fInts.n));
if (transceive != null && g.c(transceive)) {
transceive = g.a(transceive);
intValue = Integer.valueOf(g.a(transceive, transceive.length), 16);
action = (intValue / 100) + "." + (intValue % 100);
// bVar.e(action);
// Log.d("余额", action);
card.setCardBalance(action);
}
}
ReadTrade(isoDep, card);
// 测试部分,用于遍历卡内储存地址
//
// for(int j=0;j<fInts.tt.length;j++)
// // transceive = isoDep.transceive(g.a(fInts.tt[j]));
// for(int ii=0x961600;ii<0x961700;ii=ii+0x1){
// String sss;
// if(ii<=0xF)
// sss="00B0"+"00000"+Integer.toHexString(ii).toUpperCase();
// else if(ii<0xFF)
// sss="00B0"+"0000"+Integer.toHexString(ii).toUpperCase();
// else if(ii<0xFFF)
// sss="00B0"+"000"+Integer.toHexString(ii).toUpperCase();
// else if(ii<0xFFFF)
// sss="00B0"+"00"+Integer.toHexString(ii).toUpperCase();
// else if(ii<0xFFFFF)
// sss="00B0"+"0"+Integer.toHexString(ii).toUpperCase();
// else
// sss="00B0"+""+Integer.toHexString(ii).toUpperCase();
//
//
// Log.d("===", "============="+j+","+sss+"=================");
//
// transceive = isoDep.transceive(g.a(fInts.tt[j]));
// //测试
// if (transceive != null && g.c(transceive)) {
// transceive = isoDep.transceive(g.a(sss));
// if (transceive != null && g.c(transceive)) {
// // Log.d("测试", byteToHex(transceive));
// transceive = g.a(transceive);
// // System.out.println(transceive);
// // for(byte e : transceive) {
// // System.out.print(e + " ");
// // }
// String str2=new String(transceive);
// // System.out.println("\n打印2:"+str2);
// // Log.d("测试", byteToHex(transceive));
// // Log.d("测试", transceive.toString().trim());
// Log.d("测试", new String(transceive,"GB18030").trim());
// // Log.d("测试", new String(transceive,"UTF-8").trim());
// // Log.d("测试", new String(transceive,"ISO-8859-1").trim());
//
// try {
// intValue = Integer.valueOf(g.a(transceive, transceive.length), 16).intValue();
// action = (intValue / 100) + "." + (intValue % 100);
// // bVar.e(action);
// Log.d("测试", action);
// }catch (NumberFormatException e){
// Log.d("测试", "boom");
// }
//
// // card.setCardBalance(action);
// }else{
// // Log.d("测试", "无2");
// }
// // a(isoDep);
// }else{
// // Log.d("测试", "无1");
// }
// // Log.d("===", "======================================");
//
// }
//
// for(int j=0;j<fInts.tt.length;j++) {
// transceive = isoDep.transceive(g.a(fInts.tt[j]));
// if (transceive != null && g.c(transceive)) {
// a = g.ReadTrade(transceive);
// Log.d("测试0,"+j, String.valueOf(a));
// Log.d("测试0.5,"+j, g.a(transceive, transceive.length));
// g.a(ReadTrade, 0, transceive, 0, transceive.length - 2);
// Log.d("测试1,"+j, String.valueOf(a));
// action = g.a(ReadTrade, 0, ReadTrade.length, "UTF-8").trim();
// // Log.d("***", action);
// Log.d("测试2,"+j, action);
// Log.d("eiiiiii", "======================================");
// if (Util.d(action) || a(action) || action.contains("000000")) {
// // bVar.d(action);
// // Log.d("学院******", action);
// } else {
// // bVar.c(action);
// // Log.d("学院***", action);
// // card.setStudentDept(action);
// }
// } else {
// Log.d("测试", "无,"+j);
// Log.d("eiiiiii", "======================================");
// }
//
// }
//
//
// //未知3,同交易记录
// transceive = isoDep.transceive(g.a(fInts.f));
// if (transceive != null && g.c(transceive)) {
// transceive = isoDep.transceive(g.a(fInts.n));
// if (transceive != null && g.c(transceive)) {
// transceive = g.a(transceive);
// intValue = Integer.valueOf(g.a(transceive, transceive.length), 16);
// action = (intValue / 100) + "." + (intValue % 100);
// // bVar.f(action);
// Log.d("未知3***", action);
//
// }
// ReadTrade(isoDep);
// }
// // Log.d("***", String.valueOf(Float.parseFloat(this.w) + Float.parseFloat(this.x)) + "元");
}
}
try {
isoDep.close();
} catch (IOException e) {
Log.d("ERR", "close fail");
e.printStackTrace();
}
} catch (IOException e2) {
String str = "****";
Log.d("ERR", "ERROR");
action = e2.getLocalizedMessage() == null ? "" : e2.getLocalizedMessage();
Log.e(str, action);
try {
isoDep.close();
} catch (IOException e22) {
Log.d("ERR", "close fail");
e22.printStackTrace();
}
}
return card;
}
Aggregations