Android development NFC technology development (1)

##Overview
NFC is a set of short-distance wireless communication, usually the distance is 4 cm or less. The operating frequency of NFC is 13.56M Hz, and the transmission rate is 106kbit/s to 848kbit/s. NFC always occurs between an initiator and a passive target. The initiator emits near-field radio waves, which can power passive targets. These passive targets include tags and cards that do not require power, and they can also be devices with power. Compared with other wireless communication technologies, such as Bluetooth and WiFi, NFC provides lower bandwidth and distance, and is low-cost, does not require power supply, and does not need to achieve matching. The entire communication process can be completed in just a short distance of one second. An android device with NFC support is usually an initiator. It can also be used as an NFC read-write device. He will detect NFC tags and open an Activity to process it. Starting from Android 2.3.3, it supports limited P2P transmission. NFC will directly use Bluetooth transmission. This technology is called Android Beam. The two devices that Android Beam transfer data are no longer Limited to within 4 cm.
NfcManager is an NFC adapter manager that can list all the NFC adapters supported by this android device. However, most android devices only have one NFC adapter, so in most cases you can directly use the static method getDefaultAdapter(context) to get the adapter.
NfcAdapter represents the NFC adapter of this device. You can define an Intent to request that the system detects tags to be sent to your Activity. It also provides methods to register the front-end tag reminder release and front-end NDEF push. Front-end NDEF push is the only p2p NFC communication method supported by the current android version.
PendingIntent notifies the Activity, once the NFC message is intercepted, the window NdefMessage will be called through the PendingIntent
and NdefRecord NDEF is a data structure defined by the NFC Forum, used to effectively store data to NFC tags. Such as text, URL, and other MIME types. An NdefMessage acts as a container, which stores the sent and read data. An NdefMessage object contains 0 or more NdefRecords, and each NDEF record has a type, such as text, URL, smart poster/advertisement, or other MIME data. The first NfcRecord type in NDEFMessage is used to send a tag to an activity on an android device.
Tag identifies a passive NFC target, such as a tag, card, key holder, or even a phone simulation NFC card. When A tag is detected, a tag object will be created and encapsulated in an Intent, and then the NFC publishing system will use startActivity to send this Intent to the activity registered to accept this Intent. You can use the getTechList() method to get the technical details supported by this tag and create a corresponding TagTechnology object provided by android.nfc.tech.
NFC protocol standard, different TagTechnology objects supported by the tag:

  2. NfcB,Provides access to NFC-B (ISO 14443-3B) properties and I/O operations.
  3. NfcF,Provides access to NFC-F (JIS 6319-4) properties and I/O operations.
  4. NfcV,Provides access to NFC-V (ISO 15693) properties and I/O operations.
  5. IsoDep,Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations.
  6. Ndef,提供对那些被格式化为NDEF的tag的数据的访问和其他操作。 Provides access to NDEF data and operations on NFC tags that have been formatted as NDEF.   ```
		其中北京市政交通卡2008以前发行的卡显示类型为 MifareClassic + NfcA,08年以后的卡显示为 IsoDep,二代身份证的类型是 NfcB,Android 文件分享用的是Ndef格式传输数据也就是Android Beam传输的数据。
		如果你的数据类是NDEF格式数据,有两个类:
		1)NdefMessage:描述NDEF格式的信息,实际上我们写入NFC标签的就是NdefMessage对象。 
2)NdefRecord:描述NDEF信息的一个信息段,一个NdefMessage可能包含一个或者多个NdefRecord。
###下面是以IsoDep数据类型北京市政一卡通为例
	private NfcAdapter mNfcAdapter;
	private PendingIntent mPendingIntent;

    /**
     * 启动Activity,界面可见时
     */
    @Override
    protected void onStart() {
        super.onStart();
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        //一旦截获NFC消息,就会通过PendingIntent调用窗口
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
    }

    /**
     * 获得焦点,按钮可以点击
     */
    @Override
    public void onResume() {
        super.onResume();
        //设置处理优于所有其他NFC的处理
        if (mNfcAdapter != null)
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
    }

    /**
     * 暂停Activity,界面获取焦点,按钮可以点击
     */
    @Override
    public void onPause() {
        super.onPause();
        //恢复默认状态
        if (mNfcAdapter != null)
            mNfcAdapter.disableForegroundDispatch(this);
    }
	@Override
    public void onNewIntent(Intent intent) {
        String idm = null;
        StringBuffer idmByte = new StringBuffer();
      
        byte[] rawIdm = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

        Log.i("tag","-------"+Arrays.toString(rawIdm));
        if (rawIdm != null) {
            for (int i = 0; i < rawIdm.length; i++) {
                idmByte.append(Integer.toHexString(rawIdm[i] & 0xff));
            }
            idm = idmByte.toString();
        }
        mNfcText.setText("数据:" + idm);

    }
[源码](https://github.com/XHiStone/NFCDemo)

Guess you like

Origin blog.csdn.net/xhf_123/article/details/53640418