USB免驅(qū)NFC讀寫(xiě)器 Android系統(tǒng)中NFC讀寫(xiě)范例
- 2019-07-04 14:10:00
- 諾塔斯智能科技 原創(chuàng)
- 4285
由于Android具有良好的開(kāi)源優(yōu)勢(shì),可進(jìn)行深度定制開(kāi)發(fā)。安卓系統(tǒng)市場(chǎng)應(yīng)用比windows晚,雖然支持的應(yīng)用不如windows系統(tǒng)多,但由于系統(tǒng)的免費(fèi)開(kāi)放性,眾多軟件廠商經(jīng)過(guò)近幾年的開(kāi)發(fā),已經(jīng)有越來(lái)越多的應(yīng)用加入進(jìn)來(lái),目前支持辦公、教學(xué)、娛樂(lè)已經(jīng)完全沒(méi)有問(wèn)題。因此,越來(lái)越多的行業(yè)接受并認(rèn)可基于Android操作系統(tǒng)的設(shè)備。
目前市面上大多數(shù)工業(yè)控制設(shè)備開(kāi)始采用Android系統(tǒng)了,然而這部分設(shè)備并沒(méi)有搭載原生的NFC功能。隨著近年來(lái),NFC功能需求的興起,很多行業(yè)需要一直可以支持Android系統(tǒng)的可接入Android設(shè)備并進(jìn)行二次開(kāi)發(fā)的NFC讀寫(xiě)器。因此,L3-U這款NFC讀寫(xiě)器 應(yīng)運(yùn)而生。作為一款采用USB免驅(qū)的NFC讀寫(xiě)器,支持在windows、Android、linux等操作系統(tǒng),免費(fèi)提供基于上述各種系統(tǒng)的SDK,方便客戶進(jìn)行功能擴(kuò)展和二次開(kāi)發(fā)。
L3-U這款USB免驅(qū)動(dòng)NFC讀寫(xiě)器已經(jīng)設(shè)計(jì)好了USB 驅(qū)動(dòng),用戶在基于Android系統(tǒng)的設(shè)備上只有能獲取USB HOST權(quán)限或者采用OTG傳輸就可以使用,無(wú)需安裝額外驅(qū)動(dòng)。如果你其它非標(biāo)準(zhǔn)的Android設(shè)備,需要提供設(shè)備我們進(jìn)行編譯驅(qū)動(dòng)。以下為標(biāo)準(zhǔn)的具有USB HOST權(quán)限的設(shè)備接入NFC讀寫(xiě)器的過(guò)程。供大家在進(jìn)行NFC讀寫(xiě)器二次開(kāi)發(fā)過(guò)程中參考!
1、在項(xiàng)目中對(duì)AndroidManifest.xml文件進(jìn)行配置,獲取相關(guān)權(quán)限。
<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、建立一個(gè)Activity頁(yè)面,用于實(shí)現(xiàn)NFC讀寫(xiě)的功能。NFC讀寫(xiě)功能實(shí)現(xiàn)的代碼如下:
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適配器,判斷設(shè)備是否支持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 };// 過(guò)濾器 mTechLists = new String[][] { new String[] { MifareClassic.class.getName() }, new String[] { NfcA.class.getName() } };// 允許掃描的標(biāo)簽類型 } @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 = "春眠不覺(jué)曉"; 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());// 獲取當(dāng)前時(shí)間 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 = "春眠不覺(jué)曉"; 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(); } } } } }
以為內(nèi)容是在Android設(shè)備上接入L3-U 免驅(qū)NFC讀寫(xiě)器的范例,讀寫(xiě)NFC標(biāo)簽需要安裝NDEF格式進(jìn)行處理,這部分后面單獨(dú)列出來(lái)分享給大家。
聯(lián)系人: | 張經(jīng)理 |
---|---|
電話: | 400-023-6659 |
傳真: | 023-68798110 |
Email: | 843718693@qq.com |
QQ: | 843718693 |
微信: | Lotus-Smart |
微博: | lotusreader |
旺旺: | 諾塔斯智能科技有限公司 |
地址: | 重慶市江北區(qū)宏帆路36號(hào)宏帆天地1棟6樓 |