当前位置:操作系统 > 安卓/Android >>

Android NFC 开发实例

使用硬件:Google Nexus S,北京大学学生卡。(ps:笔者本想使用公交一卡通进行测试,发现手机不能正确识别)

手机操作系统:Android ICS 4.04。

开发时,笔者从Google Play Store上下载了NFC TagInfo软件进行对比学习。所以我们可以使用任意一张能被TagInfo软件正确识别的卡做测试。

在Android NFC 应用中,Android手机通常是作为通信中的发起者,也就是作为各种NFC卡的读写器。Android对NFC的支持主要在 android.nfc 和android.nfc.tech 两个包中。

android.nfc 包中主要类如下:

NfcManager 可以用来管理Android设备中指出的所有NFCAdapter,但由于大部分Android设备只支持一个NFC Adapter,所以一般直接调用getDefaultAapater来获取手机中的Adapter。

NfcAdapter 相当于一个NFC适配器,类似于电脑装了网络适配器才能上网,手机装了NfcAdapter才能发起NFC通信。

 NDEF: NFC Data Exchange Format,即NFC数据交换格式。

NdefMessage 和NdefRecord NDEF 为NFC forum 定义的数据格式。

Tag 代表一个被动式Tag对象,可以代表一个标签,卡片等。当Android设备检测到一个Tag时,会创建一个Tag对象,将其放在Intent对象,然后发送到相应的Activity。

android.nfc.tech 中则定义了可以对Tag进行的读写操作的类,这些类按照其使用的技术类型可以分成不同的类如:NfcA, NfcB, NfcF,以及MifareClassic 等。其中MifareClassic比较常见。

在本次实例中,笔者使用北京大学学生卡进行数据读取测试,学生卡的TAG类型为MifareClassic。

 


AndroidManifest.xml:

 


[html] <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="org.reno" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-permission android:name="android.permission.NFC" /> 
    <uses-sdk android:minSdkVersion="14" /> 
    <uses-feature android:name="android.hardware.nfc" android:required="true" /> 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name="org.reno.Beam" 
            android:label="@string/app_name" 
            android:launchMode="singleTop" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
            <intent-filter> 
                <action android:name="android.nfc.action.TECH_DISCOVERED" /> 
            </intent-filter> 
            <meta-data 
                android:name="android.nfc.action.TECH_DISCOVERED" 
                android:resource="@xml/nfc_tech_filter" /> 
        </activity> 
    </application> 
</manifest> 
</span> 
<span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.reno"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="14" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="org.reno.Beam"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    </application>
</manifest>
</span>

 

res/xml/nfc_tech_filter.xml:

 


<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <tech-list>

       <tech>android.nfc.tech.MifareClassic</tech>

    </tech-list>

</resources>

 

 

<uses-permission android:name="android.permission.NFC" />

<uses-feature android:name="android.hardware.nfc" android:required="true" />

 


表示会使用到硬件的NFC功能。并且当用户在Google Play Store中搜索时,只有带有NFC功能的手机才能够搜索到本应用。

 

当手机开启了NFC,并且检测到一个TAG后,TAG分发系统会自动创建一个封装了NFC TAG信息的intent。如果多于一

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,