我想在 while() 循环内写入不同的 NDEF 消息。
最后编辑:看来微 Controller 无法如此快地处理数据,所以我的问题无法解决。
//ndef.connect();
ndef.writeNdefMessage(message);
//ndef.close();
我的 write() 方法,经过简化,没有所有的 try/catch
因此,在第一个循环中它可以正常工作,但接下来的循环则不能。但经过多次循环后,它又再次起作用。如此重复。
stop = 0;
while(stop < 1000)
{
write();
stop++
}
write() 在一个循环中工作正常。
编辑:我用计时器替换了 while() :
new Timer().schedule(new TimerTask() {
@Override
public void run() {
write();
}
}, 2000);
但这太慢了...我每秒至少需要写入 5 次。 如果我设置的计时器周期小于 2000,它将不起作用,与 while() 的工作方式相同
EDIT2:我测量了消息传输和接收的速度。看起来发送消息大约需要 55ms,接收消息大约需要 7ms。这就是我想要的,但是如果我将计时器设置为在 100 毫秒后重复,例如,我会从 writeNDEFmessage()
收到此错误:
java.io.IOException:标签不是 ndef
。因此,如果我循环 10 次 writeNDEFmessage()
,它在第一个循环中工作正常,但在接下来的 9 次循环中收到异常。
编辑3:
onNewIntent()
:
@Override
protected void onNewIntent(Intent intent)
{
try {
if(intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) ||
intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)||
intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED))
{
detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(detectedTag != lastDetectedTag)
{
lastDetectedTag = detectedTag;
setIntent(intent);
}
}
} catch (Exception e)
{
Log.e("", "onIntent >>> "+e.getMessage());
}
}
这被分配给一个按钮:
public void testWrite()
{
final Timer timer = new Timer();
try {
ndef = Ndef.get(detectedTag);
ndef.connect();
} catch (IOException e) {
Log.e("", "Cannot connect");
e.printStackTrace();
}
timer.schedule(new TimerTask() {
@Override
public void run() {
transmit.writeTag(message), ndef)
}, 0, 200);
}
和writeTag()
:
public boolean writeTag(String str, Ndef ndef) {
try {
message = getNdefMessage(str);
}
catch (Exception e)
{
toast("Message error");
}
int size = message.toByteArray().length;
try {
if (ndef != null) {
if(!ndef.isConnected())
{
ndef.connect();
Log.e("", ""+ndef.toString());
}
if (!ndef.isWritable()) {
return false;
}
if (ndef.getMaxSize() < size) {
toast("Tag capacity is " + ndef.getMaxSize() + " bytes, message is " + size + " bytes.");
return false;
}
try{
ndef.writeNdefMessage(message);
}
catch(IOException e){
toast("error send");
Log.e("IOException", e + "-+-");
return false;
}
return true;
}
}
catch (Exception e) {
toast("Failed to write tag");
}
return false;
}
transmit
是 Transmit
类中的一个对象,其中定义了 writeTag()
onCreate()
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
transmit = new Transmit(this);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
textView.setText("");
detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
lastDetectedTag = detectedTag;
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(this, getClass()).
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter filter2 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
readTagFilters = new IntentFilter[]{tagDetected,filter2};
techListsArray = new String[][] { new String[] { NfcF.class.getName() } };
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
finish();
return;
}
if (!mNfcAdapter.isEnabled()) {
}
buttonListener(testButton);
}
最佳答案
摘自 Ndef
文档中的 close()
方法:“禁用从此 TagTechnology 对象对标签进行 I/O 操作,并释放资源。”
所以我认为当您调用close()
时,内部TagTechnology
被释放。如果您在同一个 ndef 上调用 connect ,那么它就是一个“陈旧”对象。每次尝试通过传递 Tag 对象来创建一个新的 Ndef
对象。
编辑:或者在实际完成之前不要调用 close()
。您仍然需要第一次调用connect()
。此外,我总是首先调用 isConnected()
以确保标签存在并已连接。
关于android - 在循环内写入 NDEF 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31983383/