我有一个应用程序可以扫描 NFC 标签以从有效负载中获取信息,然后通过 Web 服务将该信息发送到服务器。它工作正常。标签是 Mifare。在我加入公司之前,系统的移动部分是用 J2me 为诺基亚 c700 编写的。我已经在 Android 中重新编写了系统。Android 应用可以获取标签 ID 并读取有效负载。如果没有有效负载,那么它只会读取标签 ID,这很好。
问题是当我扫描尚未格式化的标签时,我无法获取 tagID。nokias 上的旧系统可以从未格式化的标签中读取标签 ID 。我如何在 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
}
[编辑2]
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);
}
[编辑3]
<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
[编辑5]
01-21 10:07:25.420: E/NfcService(512): No tag fallback activity found for Intent { act=android.nfc.action.TAG_DISCOVERED (has extras) }
[编辑6]
<?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>