本文介紹了空的NFC標(biāo)簽讀寫Android應(yīng)用程序。掃描空標(biāo)簽時(shí)手機(jī)返回自己的消息,但應(yīng)用程序無(wú)法工作?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我已經(jīng)為NFC標(biāo)簽創(chuàng)建了一個(gè)讀寫應(yīng)用程序。但是,當(dāng)我在打開應(yīng)用程序后掃描空的NFC標(biāo)記時(shí),它沒(méi)有像我預(yù)期的那樣響應(yīng),因?yàn)槲业膽?yīng)用程序Toast消息&標(biāo)記為空&qot;。但是,當(dāng)我關(guān)閉我的應(yīng)用程序時(shí),我會(huì)收到手機(jī)短信。
基本上,我需要以下幫助:
-
從我的應(yīng)用程序中檢測(cè)到空的NFC標(biāo)記。
在該空標(biāo)記中寫入服務(wù)器信息。
然后從該標(biāo)記讀取該信息。
我在這里包含了我的代碼。
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mylab.myapplication">
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<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" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
</application>
</manifest>
active_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write a message: ">
</TextView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="message" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Write" />
</LinearLayout>
<TextView
android:id="@+id/nfc_contents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
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.Ndef</tech>
<!-- class name -->
</tech-list>
</resources>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
public static final String ERROR_DETECTED = "No NFC tag detected!";
public static final String WRITE_SUCCESS = "Text written to the NFC tag successfully!";
public static final String WRITE_ERROR = "Error during writing, is the NFC tag close enough to your device?";
NfcAdapter nfcAdapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
Context context;
TextView tvNFCContent;
TextView message;
Button btnWrite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
tvNFCContent = (TextView) findViewById(R.id.nfc_contents);
message = (TextView) findViewById(R.id.edit_message);
btnWrite = (Button) findViewById(R.id.button);
btnWrite.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
try {
if(myTag ==null) {
Toast.makeText(context, ERROR_DETECTED, Toast.LENGTH_LONG).show();
} else {
write(message.getText().toString(), myTag);
Toast.makeText(context, WRITE_SUCCESS, Toast.LENGTH_LONG ).show();
}
} catch (IOException e) {
Toast.makeText(context, WRITE_ERROR, Toast.LENGTH_LONG ).show();
e.printStackTrace();
} catch (FormatException e) {
Toast.makeText(context, WRITE_ERROR, Toast.LENGTH_LONG ).show();
e.printStackTrace();
}
}
});
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
}
readFromIntent(getIntent());
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[] { tagDetected };
}
/******************************************************************************
**********************************Read From NFC Tag***************************
******************************************************************************/
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
buildTagViews(msgs);
}
}
private void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) return;
String text = "";
// String tagId = new String(msgs[0].getRecords()[0].getType());
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
try {
// Get the Text
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}
tvNFCContent.setText("NFC Content: " + text);
}
/******************************************************************************
**********************************Write to NFC Tag****************************
******************************************************************************/
private void write(String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
Ndef ndef = Ndef.get(tag);
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
}
private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
String lang = "en";
byte[] textBytes = text.getBytes();
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payload = new byte[1 + langLength + textLength];
// set status byte (see NDEF spec for actual bits)
payload[0] = (byte) langLength;
// copy langbytes and textbytes into payload
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
return recordNFC;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
readFromIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
@Override
public void onPause(){
super.onPause();
WriteModeOff();
}
@Override
public void onResume(){
super.onResume();
WriteModeOn();
}
/******************************************************************************
**********************************Enable Write********************************
******************************************************************************/
private void WriteModeOn(){
writeMode = true;
nfcAdapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
}
/******************************************************************************
**********************************Disable Write*******************************
******************************************************************************/
private void WriteModeOff(){
writeMode = false;
nfcAdapter.disableForegroundDispatch(this);
}
}
推薦答案
要處理未格式化的ndef標(biāo)記,請(qǐng)將";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.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
</tech-list>
</resources>
這樣,當(dāng)檢測(cè)到NdefFormatable
標(biāo)記時(shí),你的應(yīng)用程序會(huì)說(shuō)它想要得到通知,這將阻止你看到Android操作系統(tǒng)在出現(xiàn)這些標(biāo)記之一時(shí)顯示自己的消息。(如果出現(xiàn)無(wú)法存儲(chǔ)NDEF消息的標(biāo)記,操作系統(tǒng)仍會(huì)顯示一條消息)
然后重構(gòu)處理系統(tǒng)傳遞給應(yīng)用程序的意圖,以處理傳遞的標(biāo)記
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
buildTagViews(msgs);
}
}
private void write(String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
Ndef ndef = Ndef.get(tag);
// If Ndef.get is null then try formatting it and adding message
if (ndef != null) {
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
} else {
NdefFormatable ndefFormatable = NdefFormatable.get(tag);
// Really should do a null test on ndefFormatable here but as the code is looking for an exception don't test for null
ndefFormatable.connect();
// Format at write message at the same time
ndefFormatable.format(NdefMessage);
ndefFormatable.close();
}
}
希望這能起作用,我不再使用舊的enableForegroundDispatch
API,因?yàn)樗趪L試寫入標(biāo)記時(shí)太不可靠,enableReaderMode
要好得多。
注意:我已清理readFromIntent
方法嘗試從沒(méi)有NDEF消息的標(biāo)記中讀取NDEF消息是沒(méi)有意義的,因?yàn)槿绻麡?biāo)記上有NDEF消息,則始終在其他操作之前枚舉ACTION_NDEF_DISCOVERED。
還請(qǐng)注意,我沒(méi)有按照docs
修復(fù)writeNdefMessage
和format
不能從主應(yīng)用程序線程調(diào)用它
實(shí)際上,使用enableReaderModeAPI for NFC(除非您需要Android API級(jí)別19以下的NFC支持)要好得多,因?yàn)閹в写斯δ艿臉?biāo)簽會(huì)自動(dòng)在單獨(dú)的線程上處理。
這篇關(guān)于空的NFC標(biāo)簽讀寫Android應(yīng)用程序。掃描空標(biāo)簽時(shí)手機(jī)返回自己的消息,但應(yīng)用程序無(wú)法工作?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,