NFC(7)向NFC硬件写入数据的两个示例(nfc硬件启动android应用,nfc硬件打开uri)
向NFC标签写入数据基本步骤
1,获取Tag对象
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
2,判断NFC标签的数据类型(通过Ndef.get方法)
Ndef ndef = Ndef.get(tag);
3,NFC开始连接
ndef.connect();
4,建NdefMessage数据,然后将它写入数据
ndef.writeNdefMessage(ndefMessage);
示例1: nfc标签设备启动android应用
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.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class RunApplicationActivity extends Activity { private Button mSelectAutoRunApplication;
private String mPackageName; private NfcAdapter mNfcAdapter;//用来NFC通信
private PendingIntent mPendingIntent;//用来封装当前窗口 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_run_application); mSelectAutoRunApplication = (Button) findViewById(R.id.button_select_auto_run_application); mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, , new Intent(this,
getClass()), ); }
//从另一个aty中取出数据.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == )
{
mSelectAutoRunApplication.setText(data.getExtras().getString(
"package_name"));
String temp = mSelectAutoRunApplication.getText().toString();
mPackageName = temp.substring(temp.indexOf("\n") + ); } }
//由于在manifest.xml中指定当前aty主singleTop,所以startactivity时 onCreate只被调用一次.
//但是onNewIntent在startactivity会被调用
@Override
public void onNewIntent(Intent intent) {
if (mPackageName == null)
return; //第1步,取得NFC 标签
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); writeNFCTag(detectedTag);//自写的成员函数
} //第4种 过滤方法的开始函数,要比3种过滤方法优先级高
@Override
public void onResume() {
super.onResume();
//在Resume时让当前窗口优先处理nfc数据
if (mNfcAdapter != null){
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
null);
}
}
//第4种 过滤方法结束函数,要比3种过滤方法优先级高
@Override
public void onPause() {
super.onPause();
//在onPause时取消让当前窗口优先处理nfc数据
if (mNfcAdapter != null)
mNfcAdapter.disableForegroundDispatch(this);
}
public void onClick_SelectAutoRunApplication(View view)
{
Intent intent = new Intent(this, InstalledApplicationListActivity.class);
startActivityForResult(intent, );
}
//向nfc标签写入数据的函数
public void writeNFCTag(Tag tag) {
if (tag == null) {
return;
}
try {
//第2步,判断NFC标签的数据类型(通过Ndef.get方法)
Ndef ndef = Ndef.get(tag); if(ndef != null )
{
//第3步,NFC开始连接
ndef.connect(); if(!ndef.isWritable())//标签是否可写.
{
return;
}
//第4步,准备数据
//一个NdefMessage 中可有多个NdefRecord,它们之间关系类似table与record
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] { NdefRecord
.createApplicationRecord(mPackageName) });
int size = ndefMessage.toByteArray().length;
if(ndef.getMaxSize() < size)
{
return;
}
//第5步,把数据写入到nfc标签中.
ndef.writeNdefMessage(ndefMessage);
//第6步,写入成功提示
Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
//有可能异常
e.printStackTrace();
}
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="c.e.run.application"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.NFC" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".RunApplicationActivity"
android:label="@string/title_activity_auto_run_application"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".InstalledApplicationListActivity"
android:label="@string/title_activity_installed_application_list"
android:screenOrientation="portrait" /> </application> </manifest>
示例2: nfc标签设备打开一个uri
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
import android.widget.Toast; public class AutoOpenUriActivity extends Activity {
private NfcAdapter nfcAdapter;
private PendingIntent pendingIntent; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_auto_open_uri);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, , new Intent(this,
getClass()), ); } @Override
public void onResume() {
super.onResume();
if (nfcAdapter != null)
nfcAdapter
.enableForegroundDispatch(this, pendingIntent, null, null);
} @Override
public void onNewIntent(Intent intent) {
//第1步,取得NFC 标签
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
writeNFCTag(detectedTag);
} @Override
public void onPause() {
super.onPause();
if (nfcAdapter != null)
nfcAdapter.disableForegroundDispatch(this); } public void writeNFCTag(Tag tag) {
if (tag == null) {
return;
}
Uri uri = Uri.parse("http://www.bing.com");
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] { NdefRecord.createUri(uri) });
int size = ndefMessage.toByteArray().length;
try{
Ndef ndef = Ndef.get(tag); if (ndef != null){// 已经格式化
ndef.connect();
if (!ndef.isWritable()) {
return;
}
if (ndef.getMaxSize() < size) {
return;
}
ndef.writeNdefMessage(ndefMessage);
Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
} else {// 标签未格式化
// 第1步,获取NdefFormatable
NdefFormatable format = NdefFormatable.get(tag);
if (format != null){// 可以格式化
// 第2步,连接
format.connect();
// 第3步,格式化同时把数据写入到标签中.
format.format(ndefMessage);
Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
} else {// 不可格式化
Toast.makeText(this, "formating is failed",
Toast.LENGTH_LONG).show();
}
} } catch (Exception e) {
e.printStackTrace();
}
} }
NFC(7)向NFC硬件写入数据的两个示例(nfc硬件启动android应用,nfc硬件打开uri)的更多相关文章
- Android之NFC
来源:http://blog.csdn.net/bear_huangzhen/article/details/46333421 NFC简介: Near Field Communication 近场通信 ...
- 转载-python学习笔记之输入输出功能读取和写入数据
读取.写入和 Python 在 “探索 Python” 系列以前的文章中,学习了基本的 Python 数据类型和一些容器数据类型,例如tuple.string 和 list.其他文章讨论了 Pytho ...
- NFC(6)NFC编程的几个重要类,NFC硬件启动android应用原理
用于NFC编程的几个重要类 Tag NFC 标签 NfcAdapter Nfc 的适配类 NdefMessage 描述NDEF格式的信息 NdefRecord 描述NDEF信息的一个信息段,类似tab ...
- Android训练课程(Android Training) - NFC基础
NFC 基础 本文档介绍了在Android上的基本的NFC任务.它说明了如何发送和接收的NDEF消息(NDEF messages)的形式的表单里包含的NFC数据(NFC data),并介绍Androi ...
- Android下NFC的简单使用
现在很多手机已经配备了NFC(Near Field Communication 近场通信)的功能,我就为此专门研究过,可以到本文末尾下载源代码. Android官方资料:http://develope ...
- 关于android的nfc问题
最近在研究android的nfc问题 首先再网上有很多关于android nfc 读写的问题,但是大多数都是关于Mifare Classic类型卡的读写,我百试不得骑解,于是自己写了一些程序供大家参考 ...
- 5.Android之NFC介绍
NFC简介: Near Field Communication 近场通信,是一种数据传输技术. 与wifi.蓝牙.红外线等数据传输技术的一个主要差异就是有效距离一般不能超过4cm. NFC支持3种工作 ...
- 02使用java脚本向Pxc集群写入数据
使用java脚本向Pxc集群写入数据 批量写入pxc集群程序 导入mysql驱动包 # 批量插入数据的java脚本 package pxc_demo; import java.sql.Connecti ...
- MSSQL批量写入数据方案
近来有一个项目Feature需要有批量写入数据的场景,正巧整理资料发现自己以前也类似实现的项目,在重构的同时把相关资料做了一个简单的梳理,方便大家参考. 循环写入(简单粗暴,毕业设计就这样干的)(不推 ...
随机推荐
- 滑动选择日期(基于sui-mobile的移动端)
$(page).on('touchmove','#touchMoveTime',function (event) { touchMove(event); }); scrollBarInit(); // ...
- c#的协变和逆变
关于协变和逆变要从面向对象继承说起.继承关系是指子类和父类之间的关系:子类从父类继承,所以子类的实例也就是父类的实例.比如说Animal是父类,Dog是从Animal继承的子类:如果一个对象的类型是D ...
- 谈谈python 中__name__ = '__main__' 的作用
最近刚刚学习python,看到别人的源代码中经常出现这样一个代码段: if __name__ = '__main__' dosomting() 觉得很晕,不知道这段代码的作用是什么,后来上网查了一些资 ...
- ubuntu1304下安装boa服务器
本测试在ubuntu1304下测试,具体步骤如下: 1下载源码:www.boa.org,可在ubuntu下自带的火狐浏览器下载,也可在window下下载,然后再移到ubuntu下: 2打开终端,将bo ...
- butterknife7.0.1使用
1.官网:http://jakewharton.github.io/butterknife/ Introduction Annotate fields with @Bind and a view ID ...
- html 页面 ajax 方法显示遮罩
showLoading.css 样式: ;;list-style-type:none;} a,img{;} .overlay{;;;;;width:100%;height:100%;_padding: ...
- python学习笔记8(表达式和语句)
一.print 和 import 信息 >>> print 'age:',22 # 用逗号隔开打印多个表达式 age: 22 import somemodule # 从模块导入函数 ...
- python学习笔记4(列表)
列表是最通用的Python复合数据类型,列表中包含以逗号分隔,并在方括号([])包含的项目. 在一定程度上,列表相似C语言中的数组,它们之间的一个区别是,所有属于一个列表中的项目可以是不同的数据类型的 ...
- nginx流量带宽等请求状态统计( ngx_req_status)
介绍 ngx_req_status用来展示nginx请求状态信息,类似于apache的status,nginx自带的模块只能显示连接数等等信息,我们并不能知道到底有哪些请求.以及各url域名所消耗的带 ...
- asp防注入安全问题
一.古老的绕验证漏洞虽然古老,依然存在于很多小程序之中,比如一些企业网站的后台,简单谈谈.这个漏洞出现在没有对接受的变量进行过滤,带入数据库判断查询时,造成SQL语句的逻辑问题.例如以下代码存在问题: ...