国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

安卓 NFC 主機卡模擬

2019-11-09 17:37:08
字體:
來源:轉載
供稿:網友

安卓 NFC 卡模擬

StackOverFlow:Android HCE: are there rules for AID?

主機卡模擬Demo

第一步

新建類CardEmulatorService 繼承 HostApduService,實現方法public byte[] PRocessCommandApdu(byte[] commandApdu, Bundle extras)

@Overridepublic byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {    byte[] ret = null;    Log.d("cyb", "processCommandApdu commandApdu=" + Hex.toHexString(commandApdu));    Toast.makeText(getapplicationContext(), Hex.toHexString(commandApdu), Toast.LENGTH_SHORT).show();    String MF_NAME = "1PAY.SYS.DDF01";    byte[] selectMF = Hex.hexToBytes("00A404000E" + Hex.toHexString(PbocCmd.MF_Name) + "00");    Log.d("cyb", "processCommandApdu selectMF=" + Hex.toHexString(selectMF));    if(Arrays.equals(commandApdu, selectMF)){ // 選擇MF        ret = Hex.hexToBytes("6F15840E315041592E5359532E4444463031A5038801019000");    }else if(Arrays.equals(commandApdu, Hex.hexToBytes("00A40000023F0100"))){ // 選擇應用        ret = Hex.hexToBytes("6F088400A5049F0801029000");    }else if(Arrays.equals(commandApdu, Hex.hexToBytes("00A4000002000400"))){ // 選擇文件        ret = Hex.hexToBytes("9000");    }else if(Arrays.equals(commandApdu, Hex.hexToBytes("00B0000000"))){ // 讀文件        ret = Hex.hexToBytes("4F545142475535394352555245304D369000");    }    Log.d("cyb", "processCommandApdu ret=" + Hex.toHexString(ret));    return ret;}

第二步

在res/xml 文件夾新建aid_list.xml

<?xml version="1.0" encoding="utf-8"?><host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"    android:description="@string/service_name"    android:requireDeviceUnlock="false">    <aid-group android:description="@string/card_title" android:category="other">        <!--應用名稱 1PAY.SYS.DDF01 對應的二進制-->        <aid-filter android:name="315041592E5359532E4444463031"/>    </aid-group></host-apdu-service>

第三步

在AndroidManifest.xml中配置Service

<uses-permission android:name="android.permission.NFC" /><uses-feature    android:name="android.hardware.nfc"    android:required="true" /><uses-feature    android:name="android.hardware.nfc.hce"    android:required="true" />            <!-- Service for handling communication with NFC terminal. -->    <service        android:name=".CardEmulatorService"        android:exported="true"        android:permission="android.permission.BIND_NFC_SERVICE">        <!-- Intent filter indicating that we support card emulation. -->        <intent-filter>            <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />            <category android:name="android.intent.category.DEFAULT" />        </intent-filter>        <!-- Required XML configuration file, listing the AIDs that we are emulating cards             for. This defines what protocols our card emulation service supports. -->        <meta-data            android:name="android.nfc.cardemulation.host_apdu_service"            android:resource="@xml/aid_list" />    </service>

讀卡器代碼

println("++++++++++++++ start ++++++++++++++++");println("開始讀文件...");isoDep.connect();Log.d("cyb", "Hex.toHexString(PbocCmd.MF_Name)=" + Hex.toHexString(PbocCmd.MF_Name));//                byte[] select_mf = PbocCmd.selectByName(isoDep, PbocCmd.MF_Name);byte[] select_mf = PbocCmd.selectByName(isoDep, PbocCmd.MF_Name);println("選擇MF返回值=" + Hex.toHexString(select_mf));if (IsoDepUtils.isNoError(select_mf)) {    // 選擇應用    byte[] appId = {0x3F, 0x01};    byte[] response = PbocCmd.selectById(isoDep, appId);    if (IsoDepUtils.isNoError(response)) {        println("選擇文件=" + Hex.toHexString(appId));        println("選擇文件返回值=" + Hex.toHexString(response));        println("卡文件名稱=" + new SelectResult(response).getName());        byte[] fileId = {0x00, 0x04};        // 選擇文件        byte[] response_f = PbocCmd.selectById(isoDep, fileId);        if (IsoDepUtils.isNoError(response_f)) {            println("選擇文件=" + Hex.toHexString(fileId));            println("選擇文件返回值=" + Hex.toHexString(response_f));            println("卡文件名稱=" + new SelectResult(response_f).getName());            byte[] read = PbocCmd.readBinary(isoDep);            if(IsoDepUtils.isNoError(read)){                println(Hex.toHexString(read));                println(new String(read, "utf-8"));            }        }    }}println("++++++++++++++ end ++++++++++++++++/n/n/n/n/n");isoDep.close();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗田县| 南宁市| 建水县| 聂拉木县| 天峨县| 永顺县| 民县| 广灵县| 叶城县| 昌宁县| 青浦区| 石门县| 棋牌| 台州市| 龙海市| 武胜县| 房山区| 新昌县| 远安县| 金昌市| 禄劝| 乌拉特前旗| 西充县| 八宿县| 板桥市| 西贡区| 汨罗市| 定陶县| 上高县| 阿荣旗| 双桥区| 华宁县| 沈丘县| 密云县| 渝北区| 五家渠市| 洛扎县| 石首市| 英超| 越西县| 张家界市|