通过前台NDEF推送API级别10到13,我可以在两个非Android设备之间进行模拟吗?
我有一个Android和一个Blackberry NFC设备。如何在API级别10中的这些设备之间进行仿真?
package com.app.app.nfctag;
import java.nio.charset.Charset;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;
public class NFCDeveloper extends Activity{
NfcAdapter mNfcAdapter;
private NdefMessage pushMessage;
// TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
String text = ("Beam me up, Android!\n\n" + "Beam Time: " + System
.currentTimeMillis());
pushMessage = new NdefMessage(new NdefRecord[] { createMimeRecord(
"application/com.example.android.beam", text.getBytes())
});
}
/*public NdefMessage createNdefMessage(N event) {
String text = ("Beam me up, Android!\n\n" +
"Beam Time: " + System.currentTimeMillis());
NdefMessage msg = new NdefMessage(
new NdefRecord[] { createMimeRecord(
"application/com.example.android.beam", text.getBytes())
*//**
* The Android Application Record (AAR) is commented out. When a device
* receives a push with an AAR in it, the application specified in the AAR
* is guaranteed to run. The AAR overrides the tag dispatch system.
* You can add it back in to guarantee that this
* activity starts when receiving a beamed message. For now, this code
* uses the tag dispatch system.
*//*
//,NdefRecord.createApplicationRecord("com.example.android.beam")
});
return msg;
}
*/
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());
}
mNfcAdapter.enableForegroundNdefPush(this, pushMessage);
}
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
void processIntent(Intent intent) {
// textView = (TextView) findViewById(R.id.textView);
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
// textView.setText(new String(msg.getRecords()[0].getPayload()));
}
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;
}
}
答案 0 :(得分:0)
NDEF推送从Android设备到Blackberry,就像其他Android设备一样。
更新:
你是对的。它只能从API 14开始工作。在此之前,Android只使用NPP协议; SNEP(BB支持)仅从API 14实现。答案 1 :(得分:0)
NDEF是最常见的NFC标准,大多数其他系统都使用它。