android-Advanced NFC
2015-12-20 16:56
465 查看
There are cases, however, when you scan a tag that does not contain NDEF data or when the NDEF data could not be mapped to a MIME type or URI. In these cases, you need to open
communication directly with the tag and read and write to it with your own protocol (in raw bytes).
提到移动互联网,不能不提Android;说到云计算,不能不说KVM、Xen、OpenStack;谈起大数据,不能不谈Hadoop(大数据的最优解决方案)……这些开源技术
》The following example shows how to work with a MIFARE Ultralight tag.
communication directly with the tag and read and write to it with your own protocol (in raw bytes).
android.nfc.techpackage
提到移动互联网,不能不提Android;说到云计算,不能不说KVM、Xen、OpenStack;谈起大数据,不能不谈Hadoop(大数据的最优解决方案)……这些开源技术
》The following example shows how to work with a MIFARE Ultralight tag.
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; public class MifareUltralightTagTester { private static final String TAG = MifareUltralightTagTester.class.getSimpleName(); public void 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); } } } public String readTag(Tag tag) { MifareUltralight mifare = MifareUltralight.get(tag); try { mifare.connect(); byte[] payload = mifare.readPages(4); return new String(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); } } } return null; } }
》 Override the following activity lifecycle callbacks and add logic to enable and disable the foreground dispatch when the activity loses ([code]onPause()) and regains (
onResume()) focus.
, java.lang.String[][])]enableForegroundDispatch()must be called from the main thread and only when the activity is in the foreground (calling in
onResume()guarantees this). You also need to implement the
onNewIntentcallback to process the data from the scanned NFC tag.[/code]
public void onPause() { super.onPause(); mAdapter.disableForegroundDispatch(this); } public void onResume() { super.onResume(); mAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray); } public void onNewIntent(Intent intent) { Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //do something with tagFromIntent }
相关文章推荐
- Android招財进宝手势password的实现
- Android读书笔记之对话框的使用
- Android中fragment实现按钮点击事件
- Android 放大镜效果实现原理
- Android中防止Toast重复显示的问题
- 本文介绍Android中Intent的各种常见作用
- AndroidStudio快捷键
- android-Notification点击跳转Activity
- 【android】Cursor记录集游标、ListView和SimpleCursorAdapter、ListView数据动态更新
- Android笔记(8)---Fragment的使用(静态加载,动态加载)
- android 计数器---定时执行任务
- Android中TouchEvent触摸事件机制
- Android自动电话录音开发过程及问题
- Android SurfaceView使用dirty rect刷新
- Android Studio 错误 Duplicate files copied in APK META-INF/LICENSE.txt
- 【android】获取屏幕宽和高
- android-NFC Basics
- Android读书笔记之ViewPager的使用
- android-Near Field Communication(NFC)
- android intent 跳转到第三方应用 如果第三方应用已经启动,但是隐藏在home下,如何让它显示出来?