Android app 获取NFC系列号(ID)

1.在Manifest添加权限:

2.主Activity实现,其中CardId为NFC系列号

import android.app.PendingIntent;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.nfc.NfcAdapter;import android.nfc.NfcManager;import android.nfc.Tag;import android.os.Bundle;import android.provider.Settings;import android.view.WindowManager;import android.widget.Toast;import androidx.annotation.Nullable;import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;public class NFCTestActivity extends AppCompatActivity {    private NfcAdapter mNfcAdapter;    private PendingIntent mPendingIntent;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);        setContentView(R.layout.nfc_layout);                initNFC();    }    private void initNFC(){        NfcManager nfcManager = (NfcManager) getSystemService(Context.NFC_SERVICE);        mNfcAdapter = nfcManager.getDefaultAdapter();        if(mNfcAdapter == null){            Toast.makeText(this, "设备不支持NFC功能!", Toast.LENGTH_LONG).show();        }        if(!mNfcAdapter.isEnabled()){            AlertDialog dialog = new AlertDialog.Builder(this)                    .setTitle("提示")                    .setMessage("请确认NFC功能是否开启!")                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            dialog.dismiss();                        }                    })                    .setPositiveButton("开启", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            Intent setnfc = new Intent(Settings.ACTION_NFC_SETTINGS);                            startActivity(setnfc);                            dialog.dismiss();                        }                    }).create();            dialog.show();        }        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);    }    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);        String CardId = ByteArrayToHexString(detectedTag.getId());    }        //转为16进制字符串    private String ByteArrayToHexString(byte[] inarray) {        int i, j, in;        String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",                "B", "C", "D", "E", "F" };        String out = "";        for (j = 0; j < inarray.length; ++j) {            in = (int) inarray[j] & 0xff;            i = (in >> 4) & 0x0f;            out += hex[i];            i = in & 0x0f;            out += hex[i];        }        return out;    }    @Override    protected void onResume() {        super.onResume();        mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);    }    @Override    protected void onPause() {        super.onPause();        mNfcAdapter.disableForegroundDispatch(this);    }}
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章