我试图通过android在NTAG213芯片上写NFC标签,使用的终端是nexus 7,它给我的错误是:
11-29 13:45:14.651:E / NFC(5184):NFC服务死亡11-29 13:45:14.651:E / NFC(5184):android.os.DeadObjectException 11-29 13:45: 14.651:E / NFC(5184):在android.os.BinderProxy.transactNative(Native Method)11-29 13:45:14.651:E / NFC(5184):在android.os.BinderProxy.transact(Binder.java: 496)11-29 13:45:14.651:E / NFC(5184):在android.nfc.INfcTag $ Stub $ Proxy.formatNdef(INfcTag.java:513)11-29 13:45:14.651:E / NFC( 5184):在android.nfc.tech.NdefFormatable.format(NdefFormatable.java:126)11-29 13:45:14.651:E / NFC(5184):at android.nfc.tech.NdefFormatable.format(NdefFormatable.java :94)11-29 13:45:14.651:E / NFC(5184):at com.framentos.hellonfc.MainActivity.write(MainActivity.java:106)
我的java代码是:
public class MainActivity extends Activity{
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag mytag;
Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx=this;
Button btnWrite = (Button) findViewById(R.id.button);
final TextView message = (TextView)findViewById(R.id.edit_message);
btnWrite.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
try {
if(mytag==null){
Toast.makeText(ctx, ctx.getString(R.string.error_detected), Toast.LENGTH_LONG ).show();
}else{
write(message.getText().toString(),mytag);
Toast.makeText(ctx, ctx.getString(R.string.ok_writing), Toast.LENGTH_LONG ).show();
}
} catch (IOException e) {
Toast.makeText(ctx, ctx.getString(R.string.error_writing), Toast.LENGTH_LONG ).show();
e.printStackTrace();
} catch (FormatException e) {
Toast.makeText(ctx, ctx.getString(R.string.error_writing) , Toast.LENGTH_LONG ).show();
e.printStackTrace();
}
}
});
adapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[] { tagDetected };
}
private void write(String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
int size = message.toByteArray().length;
Ndef ndef = Ndef.get(tag);
if (ndef != null)
{
ndef.connect();
if (ndef.isWritable() && ndef.getMaxSize() > size)
{
ndef.writeNdefMessage(message);
//Miscellaneous.logEvent("i", "NFC", "Done writing tag.", 2);
//return true;
}
ndef.close();
}
else
{
NdefFormatable format = NdefFormatable.get(tag);
if (format != null)
{
try
{
format.connect();
format.format(message);
format.close();
//Miscellaneous.logEvent("i", "NFC", "Done writing tag.", 2);
//return true;
}
catch(IOException e)
{
//Miscellaneous.logEvent("e", "NFC", "Error writing tag: " + Log.getStackTraceString(e), 2);
}
}
}
}
private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
String lang = "en";
byte[] textBytes = text.getBytes();
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payload = new byte[1 + langLength + textLength];
// set status byte (see NDEF spec for actual bits)
payload[0] = (byte) langLength;
// copy langbytes and textbytes into payload
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
return recordNFC;
}
@Override
protected void onNewIntent(Intent intent){
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(this, this.getString(R.string.ok_detection) + mytag.toString(), Toast.LENGTH_LONG ).show();
}
}
@Override
public void onPause(){
super.onPause();
WriteModeOff();
}
@Override
public void onResume(){
super.onResume();
WriteModeOn();
}
private void WriteModeOn(){
writeMode = true;
adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
}
private void WriteModeOff(){
writeMode = false;
adapter.disableForegroundDispatch(this);
}
}
和我androidmanifest.xml是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.framentos.hellonfc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="com.framentos.hellonfc.MainActivity" >
<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>
<uses-permission android:name="android.permission.NFC" />
</manifest>
有人能帮助我吗?谢谢。