欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)讀取NFC卡的編號

 更新時間:2021年09月18日 11:09:11   作者:阿牛哞了一聲  
這篇文章主要為大家詳細介紹了Android實現(xiàn)讀取NFC卡的編號,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android讀取NFC卡的編號具體代碼,供大家參考,具體內(nèi)容如下

NFC相關androidManifest文件設置:

一、權限:<uses-permission android:name="android.permission.NFC"/>
二、sdk級別限制:<uses-sdk android:minSdkVersion="10"/>
三、特殊功能限制<uses-feature android:name="android.hardware.nfc" android:required="true" />這個生命可以讓你的應用在google play上被聲明使用者必須擁有nfc功能。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfc"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-permission android:name="android.permission.NFC" />
 
    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />
 
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.nfc.MainActivity"
            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.TECH_DISCOVERED" />
            </intent-filter>
 
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
 
        </activity>
    </application>
 
</manifest>

上面的android:resource="@xml/nfc_tech_filter"是對tech類型的過濾條件,在res文件夾新建一個xml文件夾,新建nfc_tech_filter.xml文件。

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <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.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

下面是封裝了讀取NFC卡號的一個類:

class NFCCard {
 
 private Activity context;
 private PendingIntent pendingIntent;
 private NfcAdapter adapter;
 private IntentFilter[] intentFilters;
 private String[][] techLists;
 private final char[] HEX_EXCHANGE = { '0', '1', '2', '3', '4', '5', '6',
   '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
 
 public NFCCard(Context context) {
  this.context = (Activity) context;
 }
 
 void init() {
 
  adapter = NfcAdapter.getDefaultAdapter(context);
  // 創(chuàng)建一個PendingIntent對象,當Android系統(tǒng)掃描到標簽時,則會填充到這個對象。
  pendingIntent = PendingIntent.getActivity(context, 0, new Intent(
    context, context.getClass())
    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
  try {
   ndef.addDataType("*/*");
  } catch (MalformedMimeTypeException e) {
   throw new RuntimeException("fail", e);
  }
  intentFilters = new IntentFilter[] { ndef, };
  techLists = new String[][] { new String[] { MifareClassic.class
    .getName() } };
 }
 
 public boolean open() {
  // TODO 打開NfcAdapter,已打開則返回true。應在onResume()中調(diào)用
  if (adapter != null)
   adapter.enableForegroundDispatch(context, pendingIntent,
     intentFilters, techLists);
  return adapter.isEnabled();
 }
 
 public boolean close() {
  // TODO 關閉NfcAdapter,已關閉則返回true
  if (adapter != null)
   adapter.disableForegroundDispatch(context);
  return !adapter.isEnabled();
 }
 
 public String getId(Intent intent) {
  // TODO 獲取NFC卡的編號。應在onNewIntent()中調(diào)用
  Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  return toHexString(tagFromIntent.getId(), 0,
    tagFromIntent.getId().length);
 }
 
 private String toHexString(byte[] d, int s, int n) {
  // TODO 轉換為十六進制形式的字符串
  final char[] ret = new char[n * 2];
  final int e = s + n;
  int x = 0;
  for (int i = s; i < e; ++i) {
   final byte v = d[i];
   ret[x++] = HEX_EXCHANGE[0x0F & (v >> 4)];
   ret[x++] = HEX_EXCHANGE[0x0F & v];
  }
  return new String(ret);
 }

需要注意的是,上面配置文件中有一個“ android:launchMode="singleTask"”,這是設置應用為單任務。代碼中的getId(Intent intent)必須要在Activity的onNewIntent(Intent intent)中執(zhí)行。因為系統(tǒng)檢測到NFC卡的時候,會自動生成封裝了相應Tag的Intent,當應用在接收到Intent的時候,默認情況下是啟動自己的Activity,這樣就會致使每次接收到Intent都會啟動新的Activity。把應用設置為單任務后,就可以避免這種情況的發(fā)生。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論