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
}
Android近场通信---高级NFC(二)的更多相关文章
- Android近场通信---NFC基础转)
Android近场通信---NFC基础(一)(转) 本文介绍在Android系通过你所能执行的基本任务。它解释了如何用NDEF消息格式来发送和接收NFC数据,并且介绍了支持这些功能的Android框架 ...
- Android NFC近场通信1——NFC概述
最近对NFC挺感兴趣,而且新换的手机也支持NFC功能(最近换了Find5,感觉还不错O(∩_∩)O),所以打算学学NFC编程.NFC就是我们经常说的近场通信.通常距离是4厘米或更短.NFC工作频率是1 ...
- Android近场通信---NFC基础(一)(转)
转自 http://blog.csdn.net/think_soft/article/details/8169483 本文译自:http://developer.android.com/guide/t ...
- Android近场通信---NFC基础(四)(转)
转自http://blog.csdn.net/think_soft/article/details/8184539 从Intent中获取信息 如果因为NFC的Intent而启动一个Activity,那 ...
- Android近场通信---NFC基础(二)(转)
转自 http://blog.csdn.net/think_soft/article/details/8171256 应用程序如何调度NFC标签 当标签调度系统完成对NFC标签和它的标识信息封装的In ...
- Android NFC近场通信2——NFC标签调度
上面一篇文章简单介绍了NFC的背景和技术应用,今天主要是讲解一下NFC如何发起通信和标签通信(主要是翻译android官网的资料,中间加入个人心得). NFC总是在一个发起者和一个被动目标之间发生.发 ...
- Android近场通信---NFC基础(五)(转)
转自 http://blog.csdn.net/think_soft/article/details/8190463 Android应用程序记录(Android Application Record- ...
- Android近场通信---NFC基础(三)(转)
转自 http://blog.csdn.net/think_soft/article/details/8180203 过滤NFC的Intent 要在你想要处理被扫描到的NFC标签时启动你的应用程序,可 ...
- android ipc通信机制之二序列化接口和Binder
IPC的一些基本概念,Serializable接口,Parcelable接口,以及Binder.此核心为最后的IBookManager.java类!!! Serializable接口,Parcelab ...
随机推荐
- ObjectContext,DataContext和DBContext 分别获取linq 的sql方法
ObjectContext 先定义一个扩展方法: public static string ToTraceString<T>(this IQueryable<T> t) { s ...
- android 百度地图开发
package sposition.webjoy.net.sendposition; import android.os.Bundle; import android.support.design.w ...
- 关于多线程情况下Net-SNMP v3 版本导致进程假死情况的跟踪与分析
1.问题描述 在使用net-snmp对交换机进行扫描的时候经常会出现进程假死的情况(就是进程并没有死掉,但是看不到它与外界进行任何的数据交互).这时候不知道进程内部发生了什么,虽然有日志信息,但进程已 ...
- php在window下的环境配置(VC9)
配置PHP5: 1. 配置PHP5.3.3,打开php安装目录(笔者是D:\php\php5)可以看到目录下有两个这样的文件php.ini- development和php.ini-produ ...
- select()函数 timval问题
如果select调用中设置了等待时间,那么每次调用时都需要重新对这个时间赋值.例如: struct timval tv; while(1) { ........; tv.tv_sec = 2; ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...
- background-size扫盲
contain,按比例调整背景图片,使得其图片宽高比自适应整个元素的背景区域的宽高比,因此假如指定的图片尺寸过大,而背景区域的整体宽高不能恰好包含背景图片的话,那么其背景某些区域可能会有空白. cov ...
- backbone学习笔记一
backbone是一个MVC单页面框架,针对传统的WEB开发B/S架构的缺点,即通过表现层的浏览器,功能层的WEB服务器,数据层的数据库服务器构架,而操作渲染过程太过复杂.
- 理解Oracle TM和TX锁
在Oracle中有很多锁,通过v$lock_type视图可以查看Oracle中所有类型的锁,在本篇文章中我们熟悉一下TM和TX锁的类型 SQL> select * from v$lock_typ ...