android NFC读取UID

1.在清单文件中添加所需要的权限

    <uses-sdk android:minSdkVersion="8" />  
    <uses-permission android:name="android.permission.NFC" />  
    <uses-feature android:name="android.hardware.nfc"  

2.在清单文件指定的Activity标签中添加过滤器

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

3.在res目录下新建目录xml,在xml目录下新建nfc_tech_filter.xml资源文件

    <?xml version="1.0" encoding="utf-8"?>  
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">  
        <tech-list>  
            <tech>android.nfc.tech.IsoDep</tech>  
        </tech-list>  
        <tech-list>  
            <tech>android.nfc.tech.NfcV</tech>  
        </tech-list>  
        <tech-list>  
            <tech>android.nfc.tech.NfcA</tech>  
        </tech-list>  
        <tech-list>  
            <tech>android.nfc.tech.NfcC</tech>  
        </tech-list>  
        <tech-list>  
            <tech>android.nfc.tech.NfcF</tech>  
        </tech-list>  
    </resources>  

4.具体代码MainActivity

    package com.etouse.nfcscanuid;  

    import android.app.PendingIntent;  
    import android.content.Intent;  
    import android.nfc.NfcAdapter;  
    import android.nfc.Tag;  
    import android.support.v7.app.AppCompatActivity;  
    import android.os.Bundle;  
    import android.widget.TextView;  
    import android.widget.Toast;  

    public class MainActivity extends AppCompatActivity {  
        private NfcAdapter nfcAdapter;  
        private PendingIntent pendingIntent;  
        private TextView tvUid;  

        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            tvUid = (TextView) findViewById(R.id.tv_uid);  
            nfcAdapter = NfcAdapter.getDefaultAdapter(this);  
            pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,  
                    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);  

            if (nfcAdapter == null) {  
                Toast.makeText(MainActivity.this,"设备不支持NFC",Toast.LENGTH_LONG).show();  
                return;  
            }  
            if (nfcAdapter!=null&&!nfcAdapter.isEnabled()) {  
                Toast.makeText(MainActivity.this,"请在系统设置中先启用NFC功能",Toast.LENGTH_LONG).show();  
                return;  
            }  
            onNewIntent(getIntent());  
        }  

        @Override  
        protected void onNewIntent(Intent intent) {  
            super.onNewIntent(intent);  
            resolveIntent(intent);  
        }  

        void resolveIntent(Intent intent) {  
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
            if (tag != null) {  
                processTag(intent);  
            }  
        }  

        public void processTag(Intent intent) {//处理tag  
            String uid = "";  
            Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
    //      str+="Tech List:"+tagFromIntent.getTechList()[0]+"\n";//打印卡的技术列表  
            byte[] aa = tagFromIntent.getId();  
            uid += bytesToHexString(aa);//获取卡的UID  
            tvUid.setText(uid);  
        }  

        //字符序列转换为16进制字符串  
        private String bytesToHexString(byte[] src) {  
            StringBuilder stringBuilder = new StringBuilder("0x");  
            if (src == null || src.length <= 0) {  
                return null;  
            }  
            char[] buffer = new char[2];  
            for (int i = 0; i < src.length; i++) {  
                buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);  
                buffer[1] = Character.forDigit(src[i] & 0x0F, 16);  
                stringBuilder.append(buffer);  
            }  
            return stringBuilder.toString();  
        }  

        @Override  
        protected void onPause() {  
            super.onPause();  
            if (nfcAdapter != null)  
                nfcAdapter.disableForegroundDispatch(this);  
        }  

        @Override  
        protected void onResume() {  
            super.onResume();  
            if (nfcAdapter != null)  
                nfcAdapter.enableForegroundDispatch(this, pendingIntent,  
                        null, null);  
        }  


    }  

5.布局中代码activity_main

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:id="@+id/activity_main"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:background="#ffffff"  
        android:orientation="vertical"  
        tools:context="com.etouse.nfcscanuid.MainActivity">  


        <ImageView  
            android:layout_width="match_parent"  
            android:background="@mipmap/nfcbg"  
            android:layout_height="100dp" />  
        <TextView  
            android:layout_width="match_parent"  
            android:text="读取到的标签"  
            android:layout_height="wrap_content" />  

        <TextView  
            android:id="@+id/tv_uid"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"/>  
    </LinearLayout>  

猜你喜欢

转载自blog.csdn.net/mingchong2005/article/details/79867831