我有一个应用程序扫描NFC标签以从有效负载获取信息,然后通过Web服务将该信息发送到服务器。它工作正常。标签是Mifare。我加入公司之前系统的移动部分是用J2me为诺基亚c700编写的。我在Android中重写了这个系统。 Android应用可以获取标记ID并读取有效负载。如果没有有效负载,那么它只读取标签ID,这很好。
问题是当我扫描一个尚未格式化的标签时,我无法获得tagID。 nokias上的旧系统可以从无格式标记中读取tagID。我怎么能在Android中这样做?为什么标签必须格式化?
提前致谢。
[编辑]
if( intentAction.equalsIgnoreCase(NFC_ACTION)){
nfcScanTimeFromDB = null;
cursor = nfcscannerapplication.loginValidate.getLastTagFromTransations();
if (cursor.getCount() > 0) {
if (cursor.moveToLast()) {
_tagScanTime = cursor.getLong(cursor
.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
nfcScanTimeFromDB = new DateTime(_tagScanTime);
}
}
if(cursor.getCount() == 0){
//DB must be empty
Log.e(TAG, "just scanned an nfc tag and DB must be empty");
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
tagId = bytesToHexString(tag.getId());
Log.e(TAG, "tagId immediately after scanning nfc tag = " + tagId);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Log.e(TAG, "NFC Tag scanned");
// get the messages from the intent
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 {
Log.e(TAG, "ndef not discovered!!!!!!");
}
Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
if(c.getCount() == 0){
Toast.makeText(this, "Please login before signing in",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(NfcscannerActivity.this, EntryActivity.class);
startActivity(intent);
}else{
c.moveToLast();
tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
}
// process the msgs array
for (int i = 0; i < msgs.length; i++) {
NdefRecord[] records = msgs[i].getRecords();
Log.e(TAG, "ndefrecord has a length of " + records.length);
tr = parse(records[i]);
payload = tr.getText();
Log.e(TAG, "TextRecord.text = " + tr.getText());
}
if(payload.length() == 0){
tagType = "1";
tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
tagPerson = tagId;//c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
tagUserName = tagId;
tagLatitude = "0.0000000";
tagLongitude = "0.0000000";
if(rotasOnly.trim().equalsIgnoreCase("false")){
processinfo();
}
}else{
// /////////////////////////////////////////////////// split the payload
// using delimiter. assign value at position[0] to tagType
String[] splitPayload = payload.split(",");
tagType = splitPayload[0];
//tagCompany = splitPayload[1];
c.moveToLast();
tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
if(splitPayload[2].length() == 0 || splitPayload[2] == null || splitPayload[2].trim().equalsIgnoreCase("0")){
tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
}else{
tagPerson = splitPayload[2];
}
tagUserName = splitPayload[3];
tagLatitude = "0.0000000";
tagLongitude = "0.0000000";
if(rotasOnly.trim().equalsIgnoreCase("false")){
processinfo();
}else{
Log.e(TAG, "cant process tag because of company option 160 rotas only");
}
}
}else{
//DB not empty///////////////////
DateTime nfcScanTimeMinustagTouchInterval = new DateTime();
nfcScanTimeMinustagTouchInterval.minusMinutes(Integer.parseInt(tagTouchInterval));
// AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
// // For example to set the volume of played media to maximum.
// audioManager.setStreamVolume (AudioManager.STREAM_MUSIC,
// audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
// MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.meep2);
// mediaPlayer.start();
//mediaPlayer.release();
//mediaPlayer = null;
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
tagId = bytesToHexString(tag.getId());
Log.e(TAG, "tagId immediately after scanning nfc tag = " + tagId);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Log.e(TAG, "NFC Tag scanned");
// get the messages from the intent
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 {
Log.e(TAG, "ndef not discovered!!!!!!");
}
Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
if(c.getCount() == 0){
Toast.makeText(this, "Please login before signing in",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(NfcscannerActivity.this, EntryActivity.class);
startActivity(intent);
}else{
c.moveToLast();
tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
}
// process the msgs array
for (int i = 0; i < msgs.length; i++) {
NdefRecord[] records = msgs[i].getRecords();
Log.e(TAG, "ndefrecord has a length of " + records.length);
tr = parse(records[i]);
payload = tr.getText();
Log.e(TAG, "TextRecord.text = " + tr.getText());
Log.e(TAG, "payload len = " + payload.length() + " so must be a blank formatted tag!");
}
if(payload.length() == 0){
tagType = "1";
tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
tagPerson = tagId;//c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
tagUserName = tagId;
tagLatitude = "0.0000000";
tagLongitude = "0.0000000";
if(rotasOnly.trim().equalsIgnoreCase("false")){
processinfo();
}
}else{
// /////////////////////////////////////////////////// split the payload
// using delimiter. assign value at position[0] to tagType
String[] splitPayload = payload.split(",");
tagType = splitPayload[0];
//tagCompany = splitPayload[1];
tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
if(splitPayload[2].length() == 0 || splitPayload[2] == null || splitPayload[2].trim().equalsIgnoreCase("0")){
tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
}else{
tagPerson = splitPayload[2];
}
tagUserName = splitPayload[3];
tagLatitude = "0.0000000";
tagLongitude = "0.0000000";
if(rotasOnly.trim().equalsIgnoreCase("false")){
processinfo();
}else{
Log.e(TAG, "cant process tag because of company option 160 rotas only");
}
}
}//finished processing tag with a NONE empty DB
}
[EDIT2]
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] tagIdbyteArray = tag.getId();
Log.e(TAG, "unformatted tag found with a tagID of " + tagIdbyteArray);
}
[EDIT3]
<activity
android:name=".NfcscannerActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="com.carefreegroup.QRCODE_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] tagIdbyteArray = tag.getId();
Log.e(TAG, "unformatted tag found with a tagID of " + tagIdbyteArray);
}
[编辑4]
01-21 09:27:35.180: D/NativeNfcTag(512): Connect Failed - status = 146
01-21 09:27:35.180: E/NFC JNI(512): doDisconnect() - Target already disconnected
[edit5]
01-21 10:07:25.420: E/NfcService(512): No tag fallback activity found for Intent { act=android.nfc.action.TAG_DISCOVERED (has extras) }
[edit6]
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
答案 0 :(得分:1)
如果MIFARE Classic标签未格式化为包含NDEF,您可以按照以下方式(从documentation)接收它们的意图(并获取ID):
添加您需要接收意图的活动的清单:
<activity>
...
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
...
</activity>
创建一个新文件<project-root>/res/xml/nfc_tech_filter.xml
,其中包含:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
在您的活动中,在某个可取的地方,添加:
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] tagId = tag.getId();
}