我开发了一个简单的 NFC 应用程序,它将在感应到 NFC 标签后启动。有两个问题想请教一下,为什么我的程序启动后,需要让设备重新感知标签,是不是启动后就读取标签?而且,我已经设置了数据类型读取为text/plain,但是为什么当我在textview字段中输出数据时,出现无意义的查询?
public class Reader extends Activity {
TextView mText;
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
IntentFilter mFilters[];
String mTechLists[][];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onStart(){
super.onStart();
setContentView(R.layout.activity_reader);
mText = (TextView) findViewById(R.id.text);
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try{
ndef.addDataType("text/plain");
}catch(MalformedMimeTypeException e){
throw new RuntimeException("fail", e);
}
IntentFilter nt = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
mFilters = new IntentFilter[]{
ndef, nt
};
mTechLists = new String[][]{
new String[]{
Ndef.class.getName()
}
};
Intent intent = getIntent();
mText.setText(getNdefMessages(intent));
}
public String getNdefMessages(Intent intent){
NdefMessage[] msgs = null;
String action = intent.getAction();
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)||
NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
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{
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};
}
}
if(msgs==null)
return "No Tag discovered!";
else
return msgs.toString();
}
@Override
public void onResume(){
super.onResume();
if (mAdapter != null)
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
@Override
public void onPause(){
super.onPause();
if (mAdapter != null)
mAdapter.disableForegroundDispatch(this);
}
@Override
public void onNewIntent(Intent intent){
Log.i("Foreground dispatch", "Discovered tag with intent:" + intent);
mText = (TextView) findViewById(R.id.text);
mText.setText(getNdefMessages(intent));
}
}
上面是我的主程序,下面是Manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.reader"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission
android:name="android.permission.NFC"/>
<uses-feature
android:name="android.hardware.nfc"
android:required="true"/>
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme"
android:debuggable="true">
<activity
android:name=".Reader"
android:label="@string/title_activity_reader">
<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"/>
<data android:mimeType="text/plain" />
<!--<data
android:scheme="http"
android:host="developer.android.com"/>-->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
消息显示在应用程序中:
最后是我输入到标签中的数据:
最佳答案
NdefMessage
是一个包含一个或多个NdefRecord
的对象。调用 yourNdefMessage.getRecords()
来访问它们。
NdefRecord
可以有各种类型和伴随的内容,包括纯文本。纯文本 NdefRecord
有两种类型:Well-Known Type Text 和 MIME 类型“text/plain”(两者使用相同的 intent 过滤器;详情请参阅 http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#ndef)。
您似乎使用了 MIME 类型的 text/plain。因此,您可以轻松地将其转换为字符串:new String(yourNdefMessage.getRecords()[0].getPayload())
。
为确保您的应用程序在启动时可以立即访问标签,您应该将 Android 应用程序记录作为 NDEF 消息中的最后一条记录。否则,生成的 Intent 将包含操作 MAIN 而不是 NDEF_DISCOVERED,并且 Intent 中将没有 NDEF 消息和 TAG EXTRAs。
关于android - 我的 NFC Reader 无法成功读取正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11517312/