【发布时间】:2015-04-21 11:13:35
【问题描述】:
我是 NFC 技术的新手,我想从读写标签开始。出于阅读目的,我使用的是 MIRFARE 1K 智能卡,而对于写作,我使用的是 NTAG203。我想要的只是在读取过程中获取 TagID 并将 TagID 和日期时间戳发送到 NTAG203。
问题:
我的应用程序在编译时遇到困难。它一直给我错误生成最终存档:无法创建'C:\Android WS\TabletApp\bin\TabletApp.apks':访问被拒绝错误。我清理了项目并重新启动 Eclipse,但徒劳无功。只有当我重新启动笔记本电脑时它才会消失。对此有何建议?
当应用程序工作时,它仍然没有给我 ID。我扫描我的卡/标签,MainActivity(此处为 ScanningActivity)一次又一次地加载。控制不会比这更进一步。关于如何处理它有什么建议吗?
public class ScanningActivity extends Activity {
private NfcAdapter sNfcAdapter;
Locale locale = new Locale("en", "US");
byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
boolean encodeInUtf8 = false;
Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char)(utfBit + langBytes.length);
static String bin2hex(byte[] data) {
return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data));
}
/* write to tag */
boolean writeNdefMessageToTag(NdefMessage message, Tag detectedTag) throws FormatException {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(detectedTag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
Toast.makeText(this, "Tag is read-only.", Toast.LENGTH_SHORT).show();
return false;
}
if (ndef.getMaxSize() < size) {
Toast.makeText(this,
"The data cannot be written since the tag capacity is" + ndef.getMaxSize() +
" bytes and the data to be transferred is " + size + " bytes",
Toast.LENGTH_LONG).show();
return false;
}
ndef.writeNdefMessage(message);
ndef.close();
Toast.makeText(this, "Data sent to the Tag", Toast.LENGTH_LONG).show();
return true;
} else {
NdefFormatable ndefFormat = NdefFormatable.get(detectedTag);
if (ndefFormat != null) {
try {
ndefFormat.connect();
ndefFormat.format(message);
ndefFormat.close();
Toast.makeText(this, "Data sent", Toast.LENGTH_LONG).show();
return true;
} catch (IOException e) {
Toast.makeText(this, "Unable to format tag", Toast.LENGTH_LONG).show();
return false;
}
} else {
Toast.makeText(this, "Not supported tag", Toast.LENGTH_LONG).show();
return false;
}
}
} catch (IOException e) {
Toast.makeText(this, "Sending failed", Toast.LENGTH_LONG).show();
}
return false;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanning);
TextView tg = (TextView)findViewById(R.id.tagid);
final TextView tvtime = (TextView)findViewById(R.id.datetime);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String currentTimeStamp = dateFormat.format(new Date()); // Find todays date
tvtime.setText(currentTimeStamp);
sNfcAdapter = NfcAdapter.getDefaultAdapter(this);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
if (sNfcAdapter == null) {
Toast.makeText(this, "This device does not support NFC or you have not scanned the card properly",
Toast.LENGTH_LONG).show();
return;
}
if (!sNfcAdapter.isEnabled()) {
Toast.makeText(this, "Looks like the NFC is not enabled on your device", Toast.LENGTH_LONG).show();
new AlertDialog.Builder(this).setTitle("NFC disabled").setMessage(
"Looks like the NFC is not enabled on your device. Please enable it.").setPositiveButton(
"Ok", null).show();
}
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
try {
String TagID = tag.getId().toString();
tg.setText(bin2hex(tag.getId()));
Toast.makeText(getApplicationContext(), bin2hex(tag.getId()), Toast.LENGTH_LONG).show();
Toast.makeText(this, TagID, Toast.LENGTH_LONG).show();
} catch (Exception ex) {
new AlertDialog.Builder(this).setTitle("Error!").setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(ScanningActivity.this, ScanningActivity.class);
startActivity(i);
}
}).setMessage(ex.getMessage() + "\n\nPlease click Ok. This will restart the application").create().show();
new AlertDialog.Builder(this).setTitle("Transferring the data").setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
TextView tvData = (TextView)findViewById(R.id.Data);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Tag tag1 = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
String DataToSend = tvtime.getText().toString() + tag1.getId().toString();
//String DataToSend = "Data";
//tvData.setText(tvtime.getText().toString()+tag.getId().toString());
byte[] textBytes = DataToSend.getBytes(utfEncoding);
byte[] dataa = new byte[1 + langBytes.length + textBytes.length];
dataa[0] = (byte)status;
System.arraycopy(langBytes, 0, dataa, 1, langBytes.length);
System.arraycopy(textBytes, 0, dataa, 1 + langBytes.length, textBytes.length);;
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[0], dataa);
NdefMessage newMessage = new NdefMessage(new NdefRecord[] { textRecord });
try {
writeNdefMessageToTag(newMessage, tag1);
} catch (FormatException e) {
e.printStackTrace();
}
}
}
}).setMessage(
"Place this tablet on the RFID Tag on the Microbiology Form. Keep it there until you get to the next screen").create().show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.scanning, menu);
return true;
}
}
清单是
<uses-permission android:name="android.permission.NFC"/>
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.tabletapp.ScanningActivity"
android:label="@string/app_name" >
<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>
</activity>
</application>
更新:
应用程序不显示任何 toast。当我在设备上放置卡片/标签时,系统会询问我喜欢使用哪个应用程序。我选择了我们正在谈论的那个,然后再次加载“ScanningActivity”(MainActivity)。既不向我显示 ID,也不通过扫描活动。
我想从卡上读取 ID,然后将其连同时间戳一起写入另一个标签。
【问题讨论】:
-
我不明白为什么有人反对这个问题。如果您觉得这不是一个合适的问题或缺少什么,也请发表评论。
-
您遇到的实际问题是什么?您的应用程序是否显示任何吐司(带有标签 ID 或任何错误消息的吐司)?您想从标签中读取 ID,然后将其写入同一个标签还是另一个标签?您的应用程序是否将 ID 读入
R.id.tagid,而您的问题是您无法写入第二个标签,或者它甚至无法写入? -
@MichaelRoland 这里是答案。很抱歉之前没有说清楚。 1.该应用程序没有显示任何吐司。当我在设备上放置卡片/标签时,系统会询问我喜欢使用哪个应用程序。我选择了我们正在谈论的那个,然后再次加载“ScanningActivity”(MainActivity)。既不向我显示 ID,也不通过扫描活动。 2. 我想从卡片中读取 ID,然后将其与时间戳一起写入另一个标签。
标签: android eclipse android-intent nfc mifare