Androidは携帯電話のNFCを使用してNFCタグデータを読み取ります

Nfcフィルタータグの設定

1マニフェストに権限を追加します。

xmlでnfcを使用する権限を追加します

1

<uses-permission android:name="android.permission.NFC" />

<uses-permission android:name = "android.permission.NFC" />

これは、インストール権限を制限するためのもので、nfc機能を備えた携帯電話のみです(オプション)

1

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

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

2nfcのフィルタリング方法は次のとおりです。

  • ACTION_NDEF_DISCOVERED、
  • ACTION_TECH_DISCOVERED、
  • 3種類のACTION_TAG_DISCOVERED。フィルターの役割は不純物を取り除くことであり、残りは私たちが必要とするものです。これらの3つのフィルタリング方法は、フィルターの特定のレイヤーの要件を満たしている限り、上から下の3つのレイヤーなど、同時に構成できます。フィルター処理後、次のレイヤーに停止します。

アクティビティのフィルターに必要な権限を追加します。

ACTION_NDEF_DISCOVERED

1

2

3

4

5

6

7

<activity>

...

      <intent-filter>

        <action android:name="android.nfc.action.NDEF_DISCOVERED" />

      </intent-filter>

...

</activity>

<project-root> / res / xmlの下に新しいnfc_tech_filter.xmlファイルを作成し(自分でxmlフォルダーを作成)、サポートする必要のあるタグタイプを追加します。(以下の構成項目は複数選択できます)。次の例は、NfcAおよびNdefテクノロジーとのNFCタグマッチングをサポートするためのものです。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

  <tech-list>

    <resources >

  <tech-list>

    <!--<tech>android.nfc.tech.IsoDep</tech>-->

    <tech>android.nfc.tech.NfcA</tech>

    <!--<tech>android.nfc.tech.NfcB</tech>-->

    <!--<tech>android.nfc.tech.NfcF</tech>-->

    <!--<tech>android.nfc.tech.NfcV</tech>-->

    <tech>android.nfc.tech.Ndef</tech>

    <!--<tech>android.nfc.tech.NdefFormatable</tech>-->

    <!--<tech>android.nfc.tech.MifareClassic</tech>-->

    <!--<tech>android.nfc.tech.MifareUltralight</tech>-->

  </tech-list>

</resources>

  </tech-list>

</resources>

<activity>

...

      <intent-filter>

        <action android:name="android.nfc.action.TECH_DISCOVERED" />

      </intent-filter>

 

      <meta-data

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

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

 

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"

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

...

</activity>

ACTION_TAG_DISCOVERED,可以添加如下权限

 

<activity>

···

      <intent-filter>

        <action android:name="android.nfc.action.TAG_DISCOVERED" />

      </intent-filter>

···

</activity>

3タグの順序を特定します

2つのnfc読み取り操作(NEDFデータを読み取ります。他のタイプのバスカードデータは自分で調べることができます)

1 nfcツールを初期化して、nfcがあるかどうか、およびnfcが開いているかどうかを確認します

2 nfcタグを検知した後、nfcタイプに対応するタグデータを読み取って分析します

3リターンディスプレイ

package com.example.chenqiuyang.nfcread;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chenqiuyang.nfcread.read.ParsedNdefRecord;
import com.example.chenqiuyang.nfcread.read.TextRecord;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;

/**
 * TODO:功能说明
 *
 * @author: chenqiuyang
 * @date: 2018-07-12 11:18
 */
public class NfcActivity extends Activity {

    private static final String TAG = "NfcActivity";
    private TextView tvNFCMessage;
    private PendingIntent mPendingIntent;
    private NfcAdapter mNfcAdapter;
    private Button btnClean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);
        Log.i(TAG, "onCreate: ");
        btnClean = findViewById(R.id.btn_clean);
        tvNFCMessage = findViewById(R.id.tv_show_nfc);




        //初始化nfc
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        mPendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        if (mNfcAdapter == null) {
            Toast.makeText(NfcActivity.this, "nfc is not available", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }


        btnClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvNFCMessage.setText("");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
        if (mNfcAdapter != null) { //有nfc功能
            if (mNfcAdapter.isEnabled()) {
                //nfc功能打开了
                //隐式启动
                mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
            } else {
                Toast.makeText(NfcActivity.this, "请打开nfc功能", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, "onNewIntent: ");
        setIntent(intent);
        if (mNfcAdapter != null) { //有nfc功能
            if (mNfcAdapter.isEnabled()) {//nfc功能打开了
                resolveIntent(getIntent());
            } else {
                Toast.makeText(NfcActivity.this, "请打开nfc功能", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (mNfcAdapter != null) {
            mNfcAdapter.disableForegroundDispatch(this);
        }
    }

    //初次判断是什么类型的NFC卡
    private void resolveIntent(Intent intent) {
        NdefMessage[] msgs = NfcUtil.getNdefMsg(intent); //重点功能,解析nfc标签中的数据

        if (msgs == null) {
            Toast.makeText(NfcActivity.this, "非NFC启动", Toast.LENGTH_SHORT).show();
        } else {
            setNFCMsgView(msgs);
        }

    }

    /**
     * 显示扫描后的信息
     *
     * @param ndefMessages ndef数据
     */
    @SuppressLint("SetTextI18n")
    private void setNFCMsgView(NdefMessage[] ndefMessages) {
        if (ndefMessages == null || ndefMessages.length == 0) {
            return;
        }

//        tvNFCMessage.setText("Payload:" + new String(ndefMessages[0].getRecords()[0].getPayload()) + "\n");

        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        tvNFCMessage.append(hour + ":" + minute + "\n");
        List<ParsedNdefRecord> records = NdefMessageParser.parse(ndefMessages[0]);
        final int size = records.size();
        for (int i = 0; i < size; i++) {
            ParsedNdefRecord record = records.get(i);
            tvNFCMessage.append(record.getViewText() + "\n");
        }
    }

}

 

おすすめ

転載: blog.csdn.net/guodashen007/article/details/105820641