Android设备读写NFC标签

2016就这样静悄悄的过去了,最近也是颓废的不行,都没怎么充实自己了,每天练练车,打打游戏和麻将,书都基本没翻个几页,家里完全没有状态。2017,加油吧,为了事业和家庭,Fighting!

关于接触到NFC这东西,是我2年前导师项目中认识到的,当时自己也是刚学Android,由于项目需要,所以自己当时承担了这方面的工作,由此也开启了我Android生涯。想当初我也是翻遍了网上的所有资料,基本上没有一个是完完全全写好的,能直接拿来用的,当时能读出一个标签的id也是乐的不行,所以我当时在想,要是我弄好了,就分享下经验,顺便共享下代码。但是为什么现在才写呢,因为当时做完后也没有继续做这个了,导致这个事情也是一拖再拖,直到最近导师又需要这个,又拿出来把代码改了改,才想起以前说要记录的这事。

NFC(Near Field Communication)近场通信,属于RFID中的一种,它是一种无源、低频、近距离的通信方式。通过扫描方提供的电能,利用电磁感应原理来驱动标签与扫描方进行通信。是一种快速,且安全的通信方式。现在广泛运用于公交卡、地铁票、门禁卡、校园卡等场景。现在应用比较广泛的是MifareClassic 1K卡,属于NfcA型标签,NFC标签共有4种类型的标签,具体类型和用途不一,这里就不说了。而当时我项目中用到的就是NfcA型卡,对NfcA卡进行数据读取和写入。至于NFC标签内部数据存放格式,读写的过程以及标签扇区(sector)和块(block)的概念,也就不说了,相信你可以通过其他途径可以获取到,这里我就只介绍NFC读写的代码了。

完成后整个功能界面如下,由于主要是测试的玩,所以界面什么的就不用太在意了,功能实现就好了。

                  

整个应用分为3大功能模块:读取标签内存,将内存数据转为Ascii码,写入数据。

        后面的是sector和block是写入数据时,自己要将数据写入的扇区和块,data是要写入的数据。

首先你得拥有NFC标签,相信这个不难,校园卡,地铁票,公交卡什么的,都可以的。其次你得有读写的设备,作为Android开发者,相信你也不会专门买个读写的设备,不然就和Android联系不上了,我们要做的是开发一个简易版的NFC标签读写软件,所以你得有一个NFC读写的手机或Pad。好了,准备好了之后就可以测试玩一玩了。你也可以先从应用市场上下载NFC的应用,来测试你的设备是否支持NFC功能或者你的标签有没有问题。

1).新建Android项目,在res文件夹下新建一个xml的文件夹,里面放的是Android支持的NFC类型的配置数据。nfc_tech_filter.xml如下:

 

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
    <!-- 可以处理所有Android支持的NFC类型 -->
 <tech-list>
     <tech>android.nfc.tech.IsoDep</tech>  
        <tech>android.nfc.tech.NfcA</tech>  
        <tech>android.nfc.tech.NfcB</tech>  
        <tech>android.nfc.tech.NfcF</tech>  
        <tech>android.nfc.tech.NfcV</tech>  
        <tech>android.nfc.tech.Ndef</tech>  
        <tech>android.nfc.tech.NdefFormatable</tech>  
        <tech>android.nfc.tech.MifareUltralight</tech> 
     <tech>android.nfc.tech.MifareClassic</tech>
 </tech-list>
</resources> 

2)在AndroidManifest中进行权限声明:

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfc_test3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/nfc"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.nfc_read_write.NfcRW"
            android:label="@string/app_name" >
            <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.NDEF_DISCOVERED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" >
                </action>

                <category android:name="android.intent.category.DEFAULT" >
                </category>
            </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>

3)布局界面代码如下activity_nfc_main:

 

 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#748694"
   >
    <LinearLayout 
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:background="#A9A9A9">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请将标签靠近设备背面......" 
        android:textSize="15sp"
        android:gravity="center"
       
  • 12
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 62
    评论
要在Android Studio中实现NFC读写功能,需要以下几个步骤: 1. 确保你的设备支持NFC功能,并在AndroidManifest.xml文件中添加必要的权限和配置。例如,添加以下权限到<manifest>标签中: ```xml <uses-permission android:name="android.permission.NFC" /> ``` 并在<application>标签内添加以下配置: ```xml <uses-feature android:name="android.hardware.nfc" android:required="true" /> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED" /> </intent-filter> ``` 2. 创建一个NFC Adapter实例,并启用前台调度模式以处理NFC意图。这可以在Activity或Service中完成。例如,在你的Activity的onCreate()方法中添加以下代码: ```java NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null && nfcAdapter.isEnabled()) { PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter[] intentFiltersArray = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)}; String[][] techListsArray = new String[][]{{android.nfc.tech.NfcA.class.getName()}}; nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray); } ``` 3. 处理NFC意图,读取或写入NFC标签数据。在你的Activity中,重写onNewIntent()方法并添加以下代码: ```java @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 在这里执行读取或写入NFC标签的操作 } } ``` 你可以使用Tag对象来读取或写入NFC标签数据。你可以使用技术类(例如Ndef或MifareClassic)来解析标签上的数据,并执行相应的操作。 这些是实现Android Studio中NFC读写功能的基本步骤。请根据你的需求和具体情况进行适当的调整和扩展。参考中的示例代码可以帮助你更详细地了解NFC读写功能的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 62
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值