• Android近场通信---高级NFC(二)


    读写NFC标签
    读写NFC标签,要涉及到从Intent对象中获取标签,并要打开与标签的通信。要读写NFC标签数据,你必须要定义自己的协议栈。但是,要记住在直接使用NFC标签工作时,你依然能够读写NDEF数据。这是你想要如何构建的事情。下例演示了如何使用MIFARE超薄标签来工作:
    package com.example.android.nfc;
    import android.nfc.Tag;
    import android.nfc.tech.MifareUltralight;
    import android.util.Log;
    import java.io.IOException;
    import java.nio.charset.Charset;
     
    publicclassMifareUltralightTagTester{
     
        privatestaticfinalString TAG =MifareUltralightTagTester.class.getSimpleName();
     
        publicvoid writeTag(Tag tag,String tagText){
            MifareUltralight ultralight =MifareUltralight.get(tag);
            try{
                ultralight.connect();
                ultralight.writePage(4,"abcd".getBytes(Charset.forName("US-ASCII")));
                ultralight.writePage(5,"efgh".getBytes(Charset.forName("US-ASCII")));
                ultralight.writePage(6,"ijkl".getBytes(Charset.forName("US-ASCII")));
                ultralight.writePage(7,"mnop".getBytes(Charset.forName("US-ASCII")));
            }catch(IOException e){
                Log.e(TAG,"IOException while closing MifareUltralight...", e);
            }finally{
                try{
                    ultralight.close();
                }catch(IOException e){
                    Log.e(TAG,"IOException while closing MifareUltralight...", e);
                }
            }
        }
     
        publicString readTag(Tag tag){
            MifareUltralight mifare =MifareUltralight.get(tag);
            try{
                mifare.connect();
                byte[] payload = mifare.readPages(4);
                returnnewString(payload,Charset.forName("US-ASCII"));
            }catch(IOException e){
                Log.e(TAG,"IOException while writing MifareUltralight
                message...", e);
            }finally{
                if(mifare !=null){
                   try{
                       mifare.close();
                   }
                   catch(IOException e){
                       Log.e(TAG,"Error closing tag...", e);
                   }
                }
            }
            returnnull;
        }
    }
     
    使用前台调度系统
    前台调度系统允许一个Activity拦截Intent对象,并且声明该Activity的优先级要比其他的处理相同Intent对象的Activity高。使用这个系统涉及到为Android系统构建一些数据结构,以便能够把相应的Intent对象发送给你的应用程序,以下是启用前台调度系统的步骤:
    1.  在你的Activity的onCreate()方法中添加下列代码:
         A.  创建一个PendingIntent对象,以便Android系统能够在扫描到NFC标签时,用它来封装NFC标签的详细信息。
              PendingIntent pendingIntent =PendingIntent.getActivity(
                     this,0,newIntent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
         B.  声明你想要截获处理的Intent对象的Intent过滤器。前台调度系统会在设备扫描到NFC标签时,用声明的Intent过滤器来检查接收到的Intent对象。如果匹配就会让你的应用程序来处理这个Intent对象,如果不匹配,前台调度系统会回退到Intent调度系统。如果Intent过滤器和技术过滤器的数组指定了null,那么就说明你要过滤所有的退回到TAG_DISCOVERED类型的Intent对象的标签。以下代码会用于处理所有的NDEF_DISCOVERED的MIME类型。只有在需要的时候才做这种处理:
     IntentFilter ndef =newIntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try{
            ndef.addDataType("*/*");    /* Handles all MIME based dispatches.
                                           You should specify only the ones that you need. */
        }
        catch(MalformedMimeTypeException e){
            thrownewRuntimeException("fail", e);
        }
       intentFiltersArray =newIntentFilter[]{ndef,};
         C.  建立一个应用程序希望处理的NFC标签技术的数组。调用Object.class.getName()方法来获取你想要支持的技术的类:

    techListsArray = new String[][] { new String[] { NfcF.class.getName() } };

    2.  重写下列Activity生命周期的回调方法,并且添加逻辑在Activity挂起(onPause())和获得焦点(onResume())时,来启用和禁用前台调度。enableForegroundDispatch()方法必须在主线程中被调用,并且只有在该Activity在前台的时候(要保证在onResume()方法中调用这个方法)。你还需要实现onNewIntent回调方法来处理扫描到的NFC标签的数据:
    publicvoid onPause(){
        super.onPause();
        mAdapter.disableForegroundDispatch(this);
    }
     
    publicvoid onResume(){
        super.onResume();
        mAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
    }
     
    publicvoid onNewIntent(Intent intent){
        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        //do something with tagFromIntent
    }
  • 相关阅读:
    获取时间差
    列表添加空字符串
    python提示InsecureRequestWarning
    网络抓包工具
    活动目录相关的面试题(一)
    How to Install Zabbix 4 on CentOS 8 / RHEL 8 in 10 minutes!
    服务器在使用 yum 命令时出现 No match for argument: screen Error: Unable to find a match 错误
    RHEL8/CentOS8的基础防火墙配置-用例
    RHEL8和CentOS8怎么重启网络
    信息安全等级保护等级划分
  • 原文地址:https://www.cnblogs.com/jyycnblogs/p/5057581.html
Copyright © 2020-2023  润新知