Java 从android上的createMime值数据读取NFC
我用CreateMime将标记写入NFC成功创建。我们如何从NFC CreateMime值读取和获取数据字符串以显示textview android布局?请帮助任何人Java 从android上的createMime值数据读取NFC,java,android,nfc,Java,Android,Nfc,我用CreateMime将标记写入NFC成功创建。我们如何从NFC CreateMime值读取和获取数据字符串以显示textview android布局?请帮助任何人 @Override protected void onNewIntent(Intent intent) { // Tag writing mode if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getA
@Override
protected void onNewIntent(Intent intent) {
// Tag writing mode
if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefRecord name = NdefRecord.createMime( ((TextView)findViewById(R.id.tvname)).getText().toString(), ((TextView)findViewById(R.id.name)).getText().toString().getBytes());
NdefRecord phone = NdefRecord.createMime( ((TextView)findViewById(R.id.tvphone)).getText().toString(), ((TextView)findViewById(R.id.phone)).getText().toString().getBytes());
NdefMessage message = new NdefMessage(new NdefRecord[] { name,phone });
if (writeTag(message, detectedTag)) {
Toast.makeText(this, "Success: Wrote placeid to nfc tag", Toast.LENGTH_LONG)
.show();
}
}
}
将NdefMessage写入NFC标记
public boolean writeTag(NdefMessage message, Tag tag) {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
Toast.makeText(getApplicationContext(),
"Error: tag not writable",
Toast.LENGTH_SHORT).show();
return false;
}
if (ndef.getMaxSize() < size) {
Toast.makeText(getApplicationContext(),
"Error: tag too small",
Toast.LENGTH_SHORT).show();
return false;
}
ndef.writeNdefMessage(message);
return true;
} else {
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
return true;
} catch (IOException e) {
return false;
}
} else {
return false;
}
}
} catch (Exception e) {
return false;
}
}
public boolean writeTag(NdefMessage消息,Tag标记){
int size=message.toByteArray().length;
试一试{
Ndef Ndef=Ndef.get(标签);
如果(ndef!=null){
connect();
如果(!ndef.isWritable()){
Toast.makeText(getApplicationContext(),
“错误:标记不可写”,
吐司。长度(短)。show();
返回false;
}
if(ndef.getMaxSize()
在onCreate中,编写以下内容:
Intent intent=getIntent();
然后调用read方法
private void read(Intent intent)
{
try{
Parcelable msgs[] = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) msgs[0];
NdefRecord nameRecord = msg.getRecords()[0];
NdefRecord phnoRecord = msg.getRecords()[1];
String name,phno;
byte[] namepayload = nameRecord.getPayload();
byte[] phnopayload = phnoRecord.getPayload();
String textEncoding = ((namePayload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = namePayload[0] & 0077;
name = new String(namepayload, languageCodeLength + 1, namepayload.length - languageCodeLength - 1, textEncoding);
phno = new String(phnoPayload, languageCodeLength + 1, phnopayload.length - languageCodeLength - 1, textEncoding);
t1.setText(name);
t2.setText(phno);
}
catch(UnsupportedEncodingException e){}
}
在onCreate中,编写以下内容:
Intent intent=getIntent();
然后调用read方法
private void read(Intent intent)
{
try{
Parcelable msgs[] = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) msgs[0];
NdefRecord nameRecord = msg.getRecords()[0];
NdefRecord phnoRecord = msg.getRecords()[1];
String name,phno;
byte[] namepayload = nameRecord.getPayload();
byte[] phnopayload = phnoRecord.getPayload();
String textEncoding = ((namePayload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = namePayload[0] & 0077;
name = new String(namepayload, languageCodeLength + 1, namepayload.length - languageCodeLength - 1, textEncoding);
phno = new String(phnoPayload, languageCodeLength + 1, phnopayload.length - languageCodeLength - 1, textEncoding);
t1.setText(name);
t2.setText(phno);
}
catch(UnsupportedEncodingException e){}
}