我的活动正在实施
CreateNdefMessageCallback, OnNdefPushCompleteCallback
我正在打电话
mNfcAdapter.setNdefPushMessageCallback(this, this);
// Register callback to listen for message-sent success
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
我已经覆盖
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
Log.d(TAG, "Creating massage");
String text = TAG + DATA_SPLITTER + ADDRESS + DATA_SPLITTER
+ USER_NAME;
// Nachricht vorbereiten.
// String text = "Hello NFC World!";
NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
MIME_TYPE.getBytes(), new byte[0], text.getBytes());
NdefRecord[] records = { record };
NdefMessage msg = new NdefMessage(records);
return msg;
}
但是当我收到时,我会得到不同的记录,其中两个,一个用市场uri来搜索应用程序,一个只有java包名称(我猜)。 在此之前,我只是使用了enableForegroundNdefPush ...等,所以没有任何回调,但是因为我需要它来处理我在两个设备上的推送完成..我已经改为这样,现在它没有正确传输我的数据。
修改
让它更容易理解:
public class NfcActivity extends Activity implements CreateNdefMessageCallback,
OnNdefPushCompleteCallback {
// ================================ Member =================================
// Speichert den NFC Adapter.
private NfcAdapter mNfcAdapter = null;
private boolean retryChannel = false;
public static final String TAG = "NfcActivity";
private static final String DATA_SPLITTER = "__:DATA:__";
private static final String MIME_TYPE = "application/my.applications.mimetype";
private String ADDRESS = null;
private String USER_NAME = null;
// Speichert das Intent, welches von Android mit erkannten Tags gefüllt
// und an die Activity weitergeleitet wird.
private PendingIntent mNfcPendingIntent = null;
private static final String LOG_TAG = "NfcActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ADDRESS = "Something";
USER_NAME = "Someone";
// Zugriff auf den NFC Adapter erhalten.
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mNfcAdapter.setNdefPushMessageCallback(this, this);
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
Intent intent = new Intent(this, getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mNfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
}
@Override
protected void onNewIntent(Intent intent) {
if (!NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
return;
}
// Nutzdaten des Intent auslesen.
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs == null) {
return;
}
for (int i = 0; i < rawMsgs.length; ++i) {
NdefMessage msg = (NdefMessage) rawMsgs[i];
NdefRecord[] records = msg.getRecords();
for (NdefRecord record : records) {
String text = new String(record.getPayload());
// this one has the wrong content. I receive two records, one for the market, one with java packet name (or something)
}
}
}
@Override
protected void onResume() {
super.onResume();
// activate receiving of messages
mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null,
null);
}
@Override
protected void onPause() {
super.onPause();
// deactivate receiving
mNfcAdapter.disableForegroundDispatch(this);
}
private void logError(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
Log.v(LOG_TAG, msg);
}
@Override
public void onNdefPushComplete(NfcEvent arg0) {
Log.d(TAG, "Push complete");
}
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
Log.d(TAG, "Creating massage");
String text = "asdfgh";
// Nachricht vorbereiten.
// String text = "Hello NFC World!";
NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
MIME_TYPE.getBytes(), new byte[0], text.getBytes());
NdefRecord[] records = { record };
NdefMessage msg = new NdefMessage(records);
return msg;
}
}
答案 0 :(得分:1)
我发给你这个答案,你可以接受它。
解决方案是Android-Manifest中错误的Mime-Type。它必须与NdefRecord定义
相同