android nfc MifareUltralight读写
2015-11-19 16:02
459 查看
MifareUltralight :是techlist中的一种类型,共16页,每页4个字节,也就是一页只能存2个汉字或者4个字母,0到3页 也就是前四页用来存卡片信息的,所以往MifareUltralight写数据,只能从第四页开始写,总共可以写入除了自带的卡片信息,可以写入4到15页数据,也就是56个字节,折算出来就是最多可写28个汉字或者56个字符。
找MifareUltralight 卡片靠近手机摄像头附近 即可测试 demo地址
http://download.csdn.net/detail/u012303938/9282985
源码:
找MifareUltralight 卡片靠近手机摄像头附近 即可测试 demo地址
http://download.csdn.net/detail/u012303938/9282985
源码:
package com.example.writefnc; import java.io.IOException; import java.nio.charset.Charset; import java.util.Arrays; import android.support.v7.app.ActionBarActivity; import android.app.PendingIntent; import android.content.Intent; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.MifareUltralight; import android.nfc.tech.Ndef; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private NfcAdapter nfcAdapter; private PendingIntent intent; CheckBox checkBox1; TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox1=(CheckBox) findViewById(R.id.checkBox1); textView1=(TextView) findViewById(R.id.textView1); nfcAdapter=NfcAdapter.getDefaultAdapter(this); intent=PendingIntent.getActivity(this, 0, new Intent(this,getClass()), 0); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); nfcAdapter.enableForegroundDispatch(this, intent, null, null); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); nfcAdapter.disableForegroundDispatch(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); Tag tag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); Toast.makeText(this, "获取tag", Toast.LENGTH_SHORT).show(); if(checkBox1.isChecked()){ Toast.makeText(this, "选择读取", Toast.LENGTH_SHORT).show(); textView1.setText(readTag(tag)); }else{ Toast.makeText(this, "选择写入", Toast.LENGTH_SHORT).show(); writeTag(tag); } } public void writeTag(Tag tag){ String[] techlist=tag.getTechList(); if(Arrays.toString(techlist).contains("MifareUltralight")){ MifareUltralight mifareUltralight=MifareUltralight.get(tag); try { mifareUltralight.connect(); Toast.makeText(this, "开始写", Toast.LENGTH_SHORT).show(); mifareUltralight.writePage(4, "你好".getBytes(Charset.forName("GB2312"))); mifareUltralight.writePage(5, "中国".getBytes(Charset.forName("GB2312"))); mifareUltralight.writePage(6, "人们".getBytes(Charset.forName("GB2312"))); mifareUltralight.writePage(7, "百姓".getBytes(Charset.forName("GB2312"))); mifareUltralight.writePage(7, "百姓".getBytes(Charset.forName("GB2312"))); Toast.makeText(this, "写入完成", Toast.LENGTH_SHORT).show(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { mifareUltralight.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }else{ Toast.makeText(this, "不是MifareUltralightle类型", Toast.LENGTH_SHORT).show(); } } public String readTag(Tag tag){ String[] techlist=tag.getTechList(); if(Arrays.toString(techlist).contains("MifareUltralight")){ MifareUltralight mifareUltralight=MifareUltralight.get(tag); Toast.makeText(this, "开始读", Toast.LENGTH_SHORT).show(); try { mifareUltralight.connect(); byte[] data=mifareUltralight.readPages(4); return new String(data,Charset.forName("GB2312")); } catch (Exception e) { // TODO: handle exception }finally{ try { mifareUltralight.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }else{ Toast.makeText(this, "不是MifareUltralightle类型", Toast.LENGTH_SHORT).show(); } return null; } }
相关文章推荐
- Android修改状态栏颜色 布局错乱问题解决
- Android CrashHandler使用时存在多个Log文件问题
- android Graphics
- Android音频系统之AudioFlinger(二)
- Ubuntu搭建Android开发环境
- android 新手错误
- Android官方提供的支持不同屏幕大小的全部方法(转)
- android canvas.drawText()的研究
- android Intent实现页面跳转
- 编译ijkplayer-android
- 2015-11-04-Android手动缓存js,css和浏览器默认缓存的优劣势对比.md
- Android ViewPager使用方法+ViewPager循环滑动
- Android源码及SDK国内镜像下载
- Android studio 如何打.aar包,并应用
- Android Spinner的ArrayAdapter和SpinnerAdapter注意事项
- 2015-11-04-Android中Java和JavaScript交互实例
- android edittext限制输入小数位数,限制输入数字大小
- 获取android SDCard存储大小
- 往Android SDCard中写入数据
- android 回调 activity向viewpager中的fragment传值