耽美小说药膳人生:Android 2.3.3 NFC分析

来源:百度文库 编辑:中财网 时间:2024/04/29 05:15:39
  Android 2.3.3 NFC分析 收藏
原创:木头月亮来自:http://blog.csdn.net/mutouyueliang/archive/2011/03/01/6216108.aspxAndroid NFC在Android版本从2.3升级到2.3.3发生了较大的变化,有点动骨动筋的苗头。NFC的极大应用场景包括:电子标签读(扫描),电子标签写,点对点数据通讯,智能卡(卡模拟)。目前从Android官方文档看,2.3版本实现了电子标签读的功能,2.3.3增加了电子标签写和点对点数据通讯的功能。希望这个2.3.3的用户编程结构(API)设计的相对完善一点,否则后续添加新功能后,API变动过大,应用开发人员常感到无所是从。言归正传,谈Android 2.3.3 NFC的改动:电子标签扫描的功能提升,不仅能读NFC Forum NDEF,而且能读其他格式的电子标签了,包括NFC-A,NFC-B,NFC-F ,NFC-V,ISO-DEP,Mifare Classic,Mifare Ultralight。
增加电子标签写的功能(write access to tag)。
增加点对点数据通讯功能(peer-to-peer communication)。
电子标签消息派发机制(消息在Android Framwork和用户应用间的传递)的改变
对应功能的增加和实现的改变,用户接口(NFC API)也做了相应改动:增加Java包android.nfc.tech
NfcAdapter派发的的Intent从原来的一个变成三个:NDEF_DISCOVERED, TECH_DISCOVERED和TAG_DISCOVERED
增加Tag类,表示电子标签
增加TagTechnology,表示电子标签的类型,主要是指使用了那种行业标准
使用NFC API编写应用程序需要理解NFC的消息派发机制,包括消息的优先级处理1. Foreground activity dispatchA foreground activity that has called NfcAdapter.enableForegroundDispatch() is given priority. See the documentation on NfcAdapter.enableForegroundDispatch() for its usage. 2. NDEF data dispatchIf the tag contains NDEF data the system inspects the first NdefRecord in the first NdefMessage. If the record is a URI, SmartPoster, or MIME data startActivity(Intent) is called with ACTION_NDEF_DISCOVERED. For URI and SmartPoster records the URI is put into the intent's data field. For MIME records the MIME type is put in the intent's type field. This allows activities to register to be launched only when data they know how to handle is present on a tag. This is the preferred method of handling data on a tag since NDEF data can be stored on many types of tags and doesn't depend on a specific tag technology. See ACTION_NDEF_DISCOVERED for more detail. If the tag does not contain NDEF data, or if no activity is registered for ACTION_NDEF_DISCOVERED with a matching data URI or MIME type then dispatch moves to stage 3. 3. Tag Technology dispatchstartActivity(Intent) is called with ACTION_TECH_DISCOVERED to dispatch the tag to an activity that can handle the technologies present on the tag. Technologies are defined as sub-classes of TagTechnology, see the package android.nfc.tech. The Android OS looks for an activity that can handle one or more technologies in the tag. See ACTION_TECH_DISCOVERED for more detail. 4. Fall-back dispatchIf no activity has been matched then startActivity(Intent) is called with ACTION_TAG_DISCOVERED. This is intended as a fall-back mechanism. See ACTION_TAG_DISCOVERED.对电子标签的写操作都是在TagTechnology的子类里是实现,子类不同,其写操作的接口函数也略有不同。附录一:什么是电子标签(NFC Tag)NFC Tag BackgroundAn NFC tag is a passive NFC device, powered by the NFC field of this Android device while it is in range. Tag's can come in many forms, such as stickers, cards, key fobs, or even embedded in a more sophisticated device.Tags can have a wide range of capabilities. Simple tags just offer read/write semantics, and contain some one time programmable areas to make read-only. More complex tags offer math operations and per-sector access control and authentication. The most sophisticated tags contain operating environments allowing complex interactions with the code executing on the tag. Use TagTechnology classes to access a broad range of capabilities available in NFC tags.  附录二:官网英文介绍Near Field Communications (NFC)Android 2.3.3 provides improved and extended support for NFC, to allow applications to interact with more types of tags in new ways.A new, comprehensive set of APIs give applications read and write access to a wider range of standard tag technologies, including:NFC-A (ISO 14443-3A)
NFC-B (ISO 14443-3B)
NFC-F (JIS 6319-4)
NFC-V (ISO 15693)
ISO-DEP (ISO 14443-4)
Mifare Classic
Mifare Ultralight
NFC Forum NDEF tags
The platform also provides a limited peer-to-peer communication protocol and API. Foreground Activities can use the API to register an NDEF message that will get pushed to other NFC devices when they connect.Advanced tag dispatching now gives applications more control over how and when they are launched, when an NFC tag is discovered. Previously, the platform used a single-step intent dispatch to notify interested applications that a tag was discovered. The platform now uses a four-step process that enables the foreground application to take control of a tag event before it is passed to any other applications (android.nfc.NfcAdapter.enableForegroundDispatch()). The new dispatch process also lets apps listen for specific tag content and tag technologies, based on two new intent actions — android.nfc.action.NDEF_DISCOVERED and android.nfc.action.TECH_DISCOVERED.The NFC API is available in the android.nfc and android.nfc.tech packages. The key classes are: NfcAdapter, which represents the NFC hardware on the device.
NdefMessage, which represents an NDEF data message, the standard format in which "records" carrying data are transmitted between devices and tags. An NDEF message certain many NDEF records of different types. Applications can receive these messages from NDEF_DISCOVERED, TECH_DISCOVERED, or TAG_DISCOVERED Intents.
NdefRecord, delivered in an NdefMessage, which describes the type of data being shared and carries the data itself.
Tag, which represents a tag scanned by the device. Multiple types of tags are supported, based on the underlying tag technology.
TagTechnology, an interface that gives applications access to tag properties and I/O operations based on the technologies present in the tag. For a full list of tag technologies supported in Android 2.3.3, see android.nfc.tech.
NFC communication relies on wireless technology in the device hardware, and is not present in all Android devices. Android devices that do not support NFC will return a null object when getDefaultAdapter(Context) is called, and context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC) will return false. The NFC API is always present, however, regardless of underlying hardware support.To use the NFC API, applications must request permission from the user by declaring in their manifest files.Additionally, developers can request filtering on Android Market, such that their applications are not discoverable to users whose devices do not support NFC. To request filtering, add to the application's manifest.