找到了个 SnepPusher的代码,但只能把数据push到PC,PC回传给手机的没数据么?是什么问题呢?
package it.ismb.nfc.sneppusher;
import java.nio.charset.Charset;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateBeamUrisCallback;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import android.text.format.Time;
public class SnepPusher extends Activity
implements CreateNdefMessageCallback,
OnNdefPushCompleteCallback,
CreateBeamUrisCallback
{
NfcAdapter mNfcAdapter;
TextView mInfoText;
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_snep_pusher);
// Check for available NFC Adapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
mInfoText = (TextView) findViewById(R.id.textView3);
mInfoText.setText("NFC is not available on this device.");
}
// Register callback to set NDEF message
mNfcAdapter.setNdefPushMessageCallback(this, this);
// Register callback to listen for message-sent success
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
mNfcAdapter.setBeamPushUrisCallback(this, this);
}
@Override
public void onResume() {
super.onResume();
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_snep_pusher, menu);
return true;
}
@Override
public void onNdefPushComplete(NfcEvent arg0) {
// A handler is needed to send messages to the activity when this
// callback occurs, because it happens from a binder thread
mHandler.obtainMessage(1).sendToTarget();
}
// 55 AA 05 12 FF 02 01 00 FF
// 55 AA 05 12 FF 02 00 00 FF
// 55 AA 22 12 FF 05 61 6D 20 6D 65 20 75 70 21 0A 0A 42 65 61 6D 20 54 69 6D 65 3A 20 31 30 3A 33 32 3A 31 32 00 FF
// 55 AA 05 12 FF 02 01 00 FF
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
Time time = new Time();
time.setToNow();
String text = ("Beam me up!\n\n" + "Beam Time: " + time.format("%H:%M:%S"));
NdefMessage msg = new NdefMessage(
new NdefRecord[] { createMimeRecord(
"application/it.ismb.nfc.sneppusher", text.getBytes())});
return msg;
}
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
return mimeRecord;
}
void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
mInfoText.setText(new String(msg.getRecords()[0].getPayload()));
}
@Override
public Uri[] createBeamUris(NfcEvent arg0) {
// TODO Auto-generated method stub
return null;
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.ismb.nfc.sneppusher"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<!--
<meta-data android:name="android.nfc.disable_beam_default" android:value="true"/>
--> <uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SnepPusher"
android:label="@string/title_activity_snep_pusher" >
<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="application/it.ismb.nfc.sneppusher"/>
</intent-filter>
</activity>
</application>
</manifest>
------解决思路----------------------
<data android:mimeType="application/it.ismb.nfc.sneppusher"/> 的问题