亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android實現(xiàn)通過NFC讀取卡號

 更新時間:2021年09月26日 17:37:18   作者:微雨未語  
這篇文章主要介紹了android實現(xiàn)通過NFC讀取卡號,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了android通過NFC讀取卡號的具體代碼,供大家參考,具體內(nèi)容如下

1.獲取權(quán)限

<uses-permission android:name="android.permission.NFC" />
<uses-feature
     android:name="android.hardware.nfc"
    android:required="true" />

2.設(shè)置NFC活動頁

<intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
                <data android:mimeType="text/plain" />
            </intent-filter>

3.Activity代碼

//NFC對象
private NfcAdapter mNfcAdapter;
private PendingIntent pi;

1.主方法中:

//獲取默認的NFC控制器
        //初始化NfcAdapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            Toast.makeText(AddPointActivity.this, "設(shè)備不支持NFC!", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        if (!mNfcAdapter.isEnabled()) {
            Toast.makeText(AddPointActivity.this, "請在系統(tǒng)設(shè)置中先啟用NFC功能!", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        //初始化PendingIntent
        // 初始化PendingIntent,當(dāng)有NFC設(shè)備連接上的時候,就交給當(dāng)前Activity處理
        pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

2.方法

//獲取數(shù)據(jù)
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 當(dāng)前app正在前端界面運行,這個時候有intent發(fā)送過來,那么系統(tǒng)就會調(diào)用onNewIntent回調(diào)方法,將intent傳送過來
        // 我們只需要在這里檢驗這個intent是否是NFC相關(guān)的intent,如果是,就調(diào)用處理方法
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            processIntent(intent);
        }
    }

    //啟動
    @Override
    protected void onResume() {
        super.onResume();
        mNfcAdapter.enableForegroundDispatch(this, pi, null, null);
    }

    //解析
    private void processIntent(Intent intent) {
        //取出封裝在intent中的TAG
        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String CardId =ByteArrayToHexString(tagFromIntent.getId());
        Toast.makeText(AddPointActivity.this, CardId, Toast.LENGTH_LONG).show();
    }

    //轉(zhuǎn)為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;
    }

運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論