日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android nfc读写cpu卡,react-native调用nfc读写cpu卡、m1卡-android篇

發布時間:2023/12/9 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android nfc读写cpu卡,react-native调用nfc读写cpu卡、m1卡-android篇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前對nfc對cpu卡的操作這塊,走了許多彎路,所以寫下這篇文章。一方面做下總結,另一方面希望能幫助到一些人。

本文目的在于讓讀者了解如何在android的程序開發中對接cpu卡、m1卡,對于nfc和cpu卡、m1卡本身的技術原理不做探討,讀者可自行百度。

android對cpu卡、m1卡的識別

1、設置權限

2、設置感興趣的技術列表

public NfcModel(Activity activity, Class extends TagTechnology>...techs){

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {

techLists = new String[techs.length][];

for(int i=0; i < techs.length; ++i){

techLists[i] = new String[]{ techs[i].getName() };

}

this.techs = techs;

try{

filters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*") };

}catch (IntentFilter.MalformedMimeTypeException e){

throw new RuntimeException(e);

}

nfcAdapter = NfcAdapter.getDefaultAdapter(activity);

pendingIntent = PendingIntent.getActivity(

activity,

0,

new Intent( activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

onNewIntent(activity.getIntent());

}

}

3、讀取數據

public void load(Parcelable parcelable) {

final Tag tag = (Tag) parcelable;

if(listener!=null){

/**

* 這里一定要關閉一下,防止因程序原因導致未關閉

*/

close();

for(Class extends TagTechnology> t : techs){

try {

Method method = t.getMethod("get",Tag.class);

TagTechnology result = (TagTechnology) method.invoke(null,tag);

if(result==null){

continue;

}

technology = result;

listener.onNfcEvent(result);

} catch (Exception e) {

if(NfcUtil.debug){

throw new RuntimeException("回調nfc事件發生錯誤",e);

}else{

//生產環境

e.printStackTrace();

}

}

}

}

}

4、在activity中調用

public class NfcTestActivity extends AppCompatActivity implements NfcListener {

private NfcModel nfcModel;

private TextView result;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_nfc);

result = (TextView) findViewById(R.id.result);

//

nfcModel = new NfcModel( this, IsoDep.class, MifareClassic.class);

nfcModel.setListener(this);

}

@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

nfcModel.onNewIntent(intent);

}

@Override

protected void onResume() {

super.onResume();

nfcModel.onResume(this);

}

@Override

protected void onPause() {

super.onPause();

nfcModel.onPause(this);

}

@Override

protected void onDestroy() {

super.onDestroy();

nfcModel.destroy();

}

@Override

public void onNfcEvent(TagTechnology tag) {

try{

tag.connect();

if(tag instanceof IsoDep){

DepTagAdapter adapter = new DefaultDepTagAdapter((IsoDep) tag);

String[] results = adapter.send(new String[]{"00a40000023f00"});

result.setText("cpu:"+results[0]);

}else{

MifareOneTagAdapter adapter = new DefaultMifareOneTagAdapter((MifareClassic) tag);

adapter.authenticateSectorWithKeyA(0, HexUtil.decodeHex("A0A1A2A3A4A5"));

byte[] bytes = adapter.readBlock(0);

result.setText("m1:"+HexUtil.encodeHexStr(bytes));

}

}catch (IOException e){

Toast.makeText(this,"請重新貼卡",Toast.LENGTH_SHORT);

}catch (NfcException e){

result.setText("error:"+e.getMessage());

}

}

}

貼卡調用app

1、使用權限

android:name="android.hardware.nfc"

android:required="true" />

2、在需要貼卡后直接調用的activity中增加如下配置:

···

android:name=".MainActivity">

android:name="android.nfc.action.TECH_DISCOVERED"

android:resource="@xml/nfc_tech_filter" />

···

nfc_tech_filter內容如下:

android.nfc.tech.IsoDep

android.nfc.tech.MifareClassic

進行react-native封裝

···

protected DepTagAdapter cpu;

protected MifareOneTagAdapter m1;

public static final String CPU = "cpu";

protected NfcModel model;

public NfcModule(ReactApplicationContext reactContext) {

super(reactContext);

reactContext.addLifecycleEventListener(this);

reactContext.addActivityEventListener(this);

}

@Override

public String getName() {

return "NfcModule";

}

@ReactMethod

public void close(){

if(cpu !=null){

cpu.close();

cpu = null;

}

if(m1!=null){

m1.close();

m1 = null;

}

}

@ReactMethod

public void isAvailable(Callback callback){

callback.invoke(NfcUtil.isAvailable(getCurrentActivity()));

}

/**

* m1卡寫卡流程

*

* 參數格式為

*

* [

*

* {

* cmd:'write',

* data:'',

* keyb:'',

* sector:0

* },

* {

* cmd:'inc',

* data: 1,

* keyb:'',

* sector:1,

* },

* {

* cmd:'dec',

* data: 1,

* keyb:'',

* sector:1,

* },

* {

* cmd:'transfer',

* data: 1,

* keyb:'',

* sector:1,

* }

*

*

* ]

*

*

*

*/

@ReactMethod

public void write(ReadableArray command, Promise promise){

if(m1 ==null){

promise.reject("io","closed");

return;

}

}

/**

* 恢復流程

* @param command

* @param promise

*

*

* 參數格式為

*

* [

*

*

* 恢復流程中的拷貝

* {

* cmd:'copy',

* src:1, //blockIndex

* dest:2 //blockIndex

* keya:''

* },

*

* 恢復流程中的恢復

* {

* cmd:'restore',

* src:1, //blockIndex

* dest:2 //blockIndex

* keya:''

* },

*

* 恢復流程中的設置

*

* {

* cmd:'set',

* dest:2, //blockIndex

* data:'',

* keya:'',

* },

* ]

*/

@ReactMethod

public void restore(ReadableArray command, Promise promise){

if(m1 ==null){

promise.reject("io","closed");

return;

}

}

@ReactMethod

public void readBlock(ReadableArray command, Promise promise){

//傳輸進來的格式為: [ "keya0", null, "keya1",... ]

if(m1 ==null){

promise.reject("io","closed");

return;

}

try {

m1.connect();

WritableArray arr = Arguments.createArray();

for(int i=0 , c = command.size(); i < c; ++i){

try{

if(command.isNull(i)){

arr.pushNull();

continue;

}

String cmd = command.getString(i);

m1.authenticateSectorWithKeyA(i, HexUtil.decodeHex(cmd));

byte[] bytes = m1.readBlock(i);

arr.pushString( HexUtil.encodeHexStr(bytes) );

}catch (IOException e){

promise.reject("io",e);

return;

}

}

promise.resolve(arr);

} catch (IOException e) {

promise.reject("io",e);

}

}

@ReactMethod

public void apdu(ReadableArray command, Promise promise){

if(cpu ==null){

promise.reject("io","closed");

return;

}

try {

cpu.connect();

WritableArray arr = Arguments.createArray();

for(int i=0 , c = command.size(); i < c; ++i){

try{

String cmd = command.getString(i);

if(cmd.contains(",")){

String[] args = cmd.split(",");

NfcResponse response = null;

for(String arg : args){

response = cpu.send(arg);

}

//只用最后一個為主

String result = response.getStr();

arr.pushString(result);

}else{

NfcResponse response = cpu.send(command.getString(i));

String result = response.getStr();

arr.pushString(result);

}

}catch (IOException e){

promise.reject("io",e);

return;

}catch (NfcException e){

promise.reject("nfc",e);

return;

}

}

promise.resolve(arr);

} catch (IOException e) {

promise.reject("io",e);

}

}

@Override

public void onHostResume() {

if(model==null){

model = new NfcModel(getCurrentActivity(), IsoDep.class, MifareClassic.class);

model.setListener(this);

}

model.onResume(getCurrentActivity());

}

@Override

public void onHostPause() {

if(model!=null)

model.onPause(getCurrentActivity());

}

@Override

public void onHostDestroy() {

if(model!=null){

model.destroy();

model = null;

}

}

@Override

public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {

}

@Override

public void onNewIntent(Intent intent) {

if(model!=null)

model.onNewIntent(intent);

}

@Override

public void onNfcEvent(TagTechnology tag) {

if(tag instanceof IsoDep){

cpu = new DefaultDepTagAdapter( (IsoDep)tag);

//通知感知到了

notifyEvent("nfcTag","cpu");

}else if(tag instanceof MifareClassic){

m1 = new DefaultMifareOneTagAdapter( (MifareClassic)tag ) ;

notifyEvent("nfcTag","m1");

}

}

protected void notifyEvent(String eventName,@Nullable Object data){

getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,data);

}

···

源碼

參考文獻

總結

以上是生活随笔為你收集整理的android nfc读写cpu卡,react-native调用nfc读写cpu卡、m1卡-android篇的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。