gpt4 book ai didi

android - 当我尝试写入 NFC 标签时出现标签丢失异常

转载 作者:可可西里 更新时间:2023-10-31 22:04:12 25 4
gpt4 key购买 nike

我希望有人能告诉我我在这里缺少什么。也许这是我的连接方式。我的标签实际上是 Mifare Ultralight,所以我没有弄错。我调试了,连接到标签是成功的——一切似乎都很好。但是日志一直在说:

android.nfc.TagLostException:标签丢失。

public class MainActivity extends Activity {

NfcAdapter mNfcAdapter;
TextView displayInfo;
Tag mNfcTag;
NdefMessage mNdefMessage;

IntentFilter [] intentFiltersArray;

String [] [] techListsArray;

PendingIntent pendingIntent;


String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
displayInfo = (TextView) findViewById(R.id.displayInfo);

NdefRecord application = NdefRecord.createApplicationRecord("com.studios.nfcdemo");


Locale locale = new Locale("en");

NdefRecord textText = createTextRecord("pleaseWork", locale, true);

mNdefMessage = new NdefMessage(textText);

pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*"); /* Handles all MIME based dispatches.
You should specify only the ones that you need. */
}
catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}

intentFiltersArray = new IntentFilter[] {ndef};

techListsArray = new String[][] { new String[] { MifareUltralight.class.getName() } };




}
public NdefRecord createTextRecord(String payload, Locale locale, boolean encodeInUtf8) {
byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
byte[] textBytes = payload.getBytes(utfEncoding);
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char) (utfBit + 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);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[0], data);
return record;
}

@Override
public void onNewIntent(Intent intent) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareUltralight ultralight = MifareUltralight.get(tagFromIntent);
write(ultralight);
}


private void write(MifareUltralight lol) {
try{
lol.connect();
lol.writePage(0, "please work".getBytes(Charset.forName("US-ASCII")));
Toast.makeText(this, "Tag written", Toast.LENGTH_LONG).show();

}
catch (Exception e){
Log.d(TAG, "no " + e.toString());
}
finally{
try{
lol.close();
}
catch (Exception e){
Log.d(TAG, e.toString());
}
}
}

public void onPause() {
super.onPause();
mNfcAdapter.disableForegroundDispatch(this);
}

public void onResume() {
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
}
}

最佳答案

我发现了我的错误。我想编写 Ndef 消息并使用标签,因为 MifareUltralight 有点傻,因为它只会使一切复杂化。相反,我将标签转换为 Ndef。这是我的代码:

private void write(Tag tag) {
Ndef ndef = Ndef.get(tag);
Locale locale = Locale.ENGLISH;
NdefRecord hi = createTextRecord("hello world", locale, true);
mNdefMessage = new NdefMessage(hi);

try{
ndef.connect();
ndef.writeNdefMessage(mNdefMessage);
Toast.makeText(this, "Message Written", Toast.LENGTH_LONG).show();
}
catch (Exception e){
Log.d(TAG, "Exception: " + e.toString());
}
finally {
try{
ndef.close();
}
catch(Exception e){
Log.d(TAG, ":( no " + e.toString());
}
}
}

如果有人需要弄清楚如何创建文本记录(以 UTF 8 编码)并从 Ndef 支持的标签中读取,我也为这些添加了我的方法:

创建文本记录:

public NdefRecord createTextRecord(String payload, Locale locale, boolean encodeInUtf8) {
byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
byte[] textBytes = payload.getBytes(utfEncoding);
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char) (utfBit + 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);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[0], data);
return record;
}

读取 NDEF 标签:

private void read(Tag tagFromIntent) {
Ndef ndef = Ndef.get(tagFromIntent);

try{
ndef.connect();
mNdefMessage = ndef.getNdefMessage();
NdefRecord [] records = mNdefMessage.getRecords();
byte [] payload = records[0].getPayload();
String displayString = getTextFromNdefRecord(records[0]);
displayInfo.setText(displayString);
Toast.makeText(this, "String read", Toast.LENGTH_LONG).show();
}
catch (Exception e){
Log.d(TAG, e.toString());
}
finally {
try{
ndef.close();
}
catch (Exception e){
Log.d(TAG, e.toString());
}
}

从 NdefRecords 读取文本:

 public String getTextFromNdefRecord(NdefRecord ndefRecord)
{
String tagContent = null;
try {
byte[] payload = ndefRecord.getPayload();
String textEncoding = "UTF-8";
int languageSize = payload[0] & 0063;
tagContent = new String(payload, languageSize + 1,
payload.length - languageSize - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("getTextFromNdefRecord", e.getMessage(), e);
}
return tagContent;
}

我希望这对其他使用 NFC 的人有所帮助。这些简单的方法应该可以为您提供 Android 中所需的所有功能。

关于android - 当我尝试写入 NFC 标签时出现标签丢失异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29970779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com