android同时识别多个nfc标签,同时阅读,写作,然后再读回NFC标签可能在Android?...

是否可以读取然后再写入,然后再次读回之前我们在NFC标签上写入的相同数据,而无需将其从Android的移动领域(移动)中移除?如果有人已经这样做了,那么请分享过来....同时阅读,写作,然后再读回NFC标签可能在Android?

public class MainActivity extends Activity

{

NfcAdapter adapter;

PendingIntent pendingIntent;

IntentFilter writeTagFilters[];

Tag mytag;

private TextView mTextViewData;

Context ctx;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ctx=this;

mTextViewData = (TextView)findViewById(R.id.mtextView);

Button btnWrite = (Button) findViewById(R.id.write_button);

final EditText mEditText = (EditText)findViewById(R.id.edit_message);

adapter = NfcAdapter.getDefaultAdapter(this);

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

IntentFilter tagDetected1 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

try

{

tagDetected1.addDataType("text/nfc-service-tag");

}

catch (MalformedMimeTypeException e)

{

throw new RuntimeException("Could not add MIME type.", e);

}

writeTagFilters = new IntentFilter[] { tagDetected1 };

btnWrite.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v)

{

if(mytag==null)

{

Toast.makeText(ctx, ctx.getString(R.string.error_detected), Toast.LENGTH_LONG).show();

}

else

{

writeTag(mEditText.getText().toString(),mytag);

}

}

});

}

private void writeTag(String text,Tag tag)

{

// record to launch Play Store if app is not installed

// record that contains our custom "retro console" game data, using custom MIME_TYPE

String mimeType = "text/nfc-service-tag";

byte[] payload = text.getBytes(Charset.forName("US-ASCII"));

byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));

NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);

NdefMessage message = new NdefMessage(new NdefRecord[] { record });

try

{

// see if tag is already NDEF formatted

Ndef ndef = Ndef.get(tag);

if (ndef != null)

{

ndef.connect();

if (!ndef.isWritable())

{

Toast.makeText(ctx, "Read-only tag.", Toast.LENGTH_LONG).show();

}

// work out how much space we need for the data

int size = message.toByteArray().length;

if (ndef.getMaxSize() < size)

{

Toast.makeText(ctx, "Tag doesn't have enough free space.", Toast.LENGTH_LONG).show();

}

ndef.writeNdefMessage(message);

Toast.makeText(ctx, ctx.getString(R.string.ok_writing), Toast.LENGTH_LONG).show();

ndef.close();

}

else

{

// attempt to format tag

NdefFormatable format = NdefFormatable.get(tag);

if (format != null)

{

try

{

format.connect();

format.format(message);

Toast.makeText(ctx, ctx.getString(R.string.ok_writing), Toast.LENGTH_LONG).show();

}

catch (IOException e)

{

Toast.makeText(ctx,"Unable to format tag to NDEF.", Toast.LENGTH_LONG).show();

}

}

else

{

Toast.makeText(ctx,"Tag doesn't appear to support NDEF format.", Toast.LENGTH_LONG).show();

}

}

}

catch(Exception e)

{

}

}

@Override

protected void onNewIntent(Intent intent)

{

String action = intent.getAction();

if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED))

{

NdefMessage[] msgs = getNdefMessagesFromIntent(intent);

final NdefMessage msg = (NdefMessage)msgs[0];

String payload = new String(msg.getRecords()[0].getPayload());

mTextViewData.setText(payload);

mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

Toast.makeText(this, "In onNewIntent()", Toast.LENGTH_LONG).show();

}

else if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED))

{

Toast.makeText(this, "This NFC tag has no NDEF data.", Toast.LENGTH_LONG).show();

}

}

@Override

public void onResume()

{

super.onResume();

if (getIntent().getAction() != null)

{

// tag received when app is not running and not in the foreground:

if (getIntent().getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED))

{

NdefMessage[] msgs = getNdefMessagesFromIntent(getIntent());

NdefRecord record = msgs[0].getRecords()[0];

byte[] payload = record.getPayload();

String payloadString = new String(payload);

mTextViewData.setText(payloadString);

Toast.makeText(this, "In onResume()", Toast.LENGTH_LONG).show();

}

}

adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);

}

@Override

public void onPause()

{

super.onPause();

adapter.disableForegroundDispatch(this);

}

NdefMessage[] getNdefMessagesFromIntent(Intent intent)

{

// Parse the intent

NdefMessage[] msgs = null;

String action = intent.getAction();

if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED))

{

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

if (rawMsgs != null){

msgs = new NdefMessage[rawMsgs.length];

for (int i = 0; i < rawMsgs.length; i++)

{

msgs[i] = (NdefMessage) rawMsgs[i];

}

}

else

{

// Unknown tag type

byte[] empty = new byte[] {};

NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);

NdefMessage msg = new NdefMessage(new NdefRecord[] { record });

msgs = new NdefMessage[] { msg };

}

}

else

{

//Log.e(TAG, "Unknown intent.");

finish();

}

return msgs;

}

}

+0

是的,这是可能的。你能更详细地解释你的问题吗? –

+0

转到这篇文章... http://stackoverflow.com/questions/12150062/android-read-back-the-tag-infomation-after-write-without-second-touch-of-tag在这里你告诉过它是可能的....但如何?可以更详细地解释它.... –

+0

我在上面添加了我的完整代码...它的读取工作正常,然后写入....但我想在写入后再次读取标记... –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值