USB免驅NFC讀寫器 Android系統(tǒng)中NFC讀寫范例
- 2019-07-04 14:10:00
- 諾塔斯智能科技 原創(chuàng)
- 3931
由于Android具有良好的開源優(yōu)勢,可進行深度定制開發(fā)。安卓系統(tǒng)市場應用比windows晚,雖然支持的應用不如windows系統(tǒng)多,但由于系統(tǒng)的免費開放性,眾多軟件廠商經過近幾年的開發(fā),已經有越來越多的應用加入進來,目前支持辦公、教學、娛樂已經完全沒有問題。因此,越來越多的行業(yè)接受并認可基于Android操作系統(tǒng)的設備。
目前市面上大多數工業(yè)控制設備開始采用Android系統(tǒng)了,然而這部分設備并沒有搭載原生的NFC功能。隨著近年來,NFC功能需求的興起,很多行業(yè)需要一直可以支持Android系統(tǒng)的可接入Android設備并進行二次開發(fā)的NFC讀寫器。因此,L3-U這款NFC讀寫器 應運而生。作為一款采用USB免驅的NFC讀寫器,支持在windows、Android、linux等操作系統(tǒng),免費提供基于上述各種系統(tǒng)的SDK,方便客戶進行功能擴展和二次開發(fā)。
L3-U這款USB免驅動NFC讀寫器已經設計好了USB 驅動,用戶在基于Android系統(tǒng)的設備上只有能獲取USB HOST權限或者采用OTG傳輸就可以使用,無需安裝額外驅動。如果你其它非標準的Android設備,需要提供設備我們進行編譯驅動。以下為標準的具有USB HOST權限的設備接入NFC讀寫器的過程。供大家在進行NFC讀寫器二次開發(fā)過程中參考!
1、在項目中對AndroidManifest.xml文件進行配置,獲取相關權限。
<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="17" /> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/*" /> </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" />
2、建立一個Activity頁面,用于實現NFC讀寫的功能。NFC讀寫功能實現的代碼如下:
package cc.lotusnfc; import java.io.IOException; import java.nio.charset.Charset; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.Arrays; import cc.lotusnfc.R; import android.nfc.FormatException; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.NfcManager; import android.nfc.Tag; import android.nfc.tech.MifareClassic; import android.nfc.tech.Ndef; import android.nfc.tech.NfcA; import android.os.Bundle; import android.os.Parcelable; import android.annotation.SuppressLint; import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView resultText; private PendingIntent pendingIntent; private IntentFilter[] mFilters; private String[][] mTechLists; private Button mJumpTagBtn; private boolean isFirst = true; private NfcAdapter m_NfcAdpater; private EditText m_edtLog; private Intent m_NfcIntent = null; @Override protected void onCreate(Bundle savedInstanceState) { // 獲取nfc適配器,判斷設備是否支持NFC功能 m_NfcAdpater = NfcAdapter.getDefaultAdapter(this); if (m_NfcAdpater == null) { Toast.makeText(this, "Not Found NfcAdapter!", Toast.LENGTH_SHORT) .show(); //finish(); //return; } else if (!m_NfcAdpater.isEnabled()) { Toast.makeText(this, "Please Enabled NfcAdapter", Toast.LENGTH_SHORT).show(); //finish(); //return; } super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); m_edtLog = (EditText) findViewById(R.id.edtLog); m_edtLog.setText(""); pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); ndef.addCategory("*/*"); mFilters = new IntentFilter[] { ndef };// 過濾器 mTechLists = new String[][] { new String[] { MifareClassic.class.getName() }, new String[] { NfcA.class.getName() } };// 允許掃描的標簽類型 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @SuppressLint("NewApi") @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); m_NfcAdpater.enableForegroundDispatch(this, pendingIntent, mFilters, mTechLists); } private NdefMessage createMessage(String text) { NdefRecord[] record = new NdefRecord[1]; String lang = "en"; byte[] langBytes = lang.getBytes(Charset.forName("US-ASCII")); byte[] textBytes = text.getBytes(Charset.forName("UTF-8")); char status = (char) (langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; data[0] = (byte) status; System.arraycopy(langBytes, 0, data, 1, langBytes.length); System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); record[0] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); return new NdefMessage(record); } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); m_NfcIntent = intent; String strWriteText = "春眠不覺曉"; if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) { Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); Ndef ndef = Ndef.get(tagFromIntent); if (ndef != null) { NdefMessage ndefMessage = createMessage(strWriteText); try { ndef.connect(); // READ NdefMessage msg = ndef.getNdefMessage(); if(null == msg) return; NdefRecord[] records = msg.getRecords(); // NdefMessage nmsg = (NdefMessage) msgs[0]; NdefRecord record = records[0]; String resultStr = ""; if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN) { if (Arrays .equals(record.getType(), NdefRecord.RTD_TEXT)) { byte[] payload = record.getPayload(); if (payload == null) return; try { String textEString = ((payload[0] & 0x80) == 0) ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] & 0x3f; resultStr = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEString); AddLog("Read:" + resultStr); } catch (Exception e) { e.printStackTrace(); } } } // WRITE ndef.writeNdefMessage(ndefMessage); AddLog("WRITE:" + strWriteText); ndef.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); AddLog("IOException" ); } catch (FormatException e) { // TODO Auto-generated catch block e.printStackTrace(); AddLog("FormatException" ); } } } } private void AddLog(String strLog) { SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis());// 獲取當前時間 String strDate = formatter.format(curDate); if (null == m_edtLog) return; String strLogs = m_edtLog.getText().toString().trim(); if (strLogs.equals("")) { strLogs = strDate + " " + strLog; } else { strLogs += "\r\n" + strDate + " " + strLog; } m_edtLog.setText(strLogs); } public void OnClearLogListener(View arg0) { if (null == m_edtLog) return; m_edtLog.setText(""); } public void OnSetNfcListener(View arg0) { startActivityForResult(new Intent( android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0); } public void OnTestListener(View arg0) { String strWriteText = "春眠不覺曉"; if(null == m_NfcIntent) return; if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(m_NfcIntent.getAction())) { Tag tagFromIntent = m_NfcIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG); Ndef ndef = Ndef.get(tagFromIntent); if (ndef != null) { NdefMessage ndefMessage = createMessage(strWriteText); try { ndef.connect(); // READ NdefMessage msg = ndef.getNdefMessage(); NdefRecord[] records = msg.getRecords(); // NdefMessage nmsg = (NdefMessage) msgs[0]; NdefRecord record = records[0]; String resultStr = ""; if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN) { if (Arrays .equals(record.getType(), NdefRecord.RTD_TEXT)) { byte[] payload = record.getPayload(); if (payload == null) return; try { String textEString = ((payload[0] & 0x80) == 0) ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] & 0x3f; resultStr = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEString); AddLog("Read:" + resultStr); } catch (Exception e) { e.printStackTrace(); } } } // WRITE ndef.writeNdefMessage(ndefMessage); AddLog("WRITE:" + strWriteText); ndef.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
以為內容是在Android設備上接入L3-U 免驅NFC讀寫器的范例,讀寫NFC標簽需要安裝NDEF格式進行處理,這部分后面單獨列出來分享給大家。
聯系人: | 張經理 |
---|---|
電話: | 400-023-6659 |
傳真: | 023-68798110 |
Email: | 843718693@qq.com |
QQ: | 843718693 |
微信: | Lotus-Smart |
微博: | lotusreader |
旺旺: | 諾塔斯智能科技有限公司 |
地址: | 重慶市江北區(qū)宏帆路36號宏帆天地1棟6樓 |