[英]How to start emulation between two nfc devices in android
I have to share tag from one device to other, i have android 2.3.3 galaxy. 我必须将标签从一台设备共享到另一台设备,我有android 2.3.3银河。 please share me some code for sharing tag from one device to another.
请分享一些代码,以便将标签从一台设备共享到另一台设备。
I have taken this code from developer site but it shows error in that line 我已从开发者网站上获取了此代码,但该行显示错误
mNfcAdapter.setNdefPushMessageCallback(this, this);
// Check for available NFC Adapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
// Register callback
mNfcAdapter.setNdefPushMessageCallback(this, this);
now im using enable forground message but it works fine but how to see emulation .. 现在我正在使用enable forground消息,但是工作正常,但是如何查看仿真..
public class DemoNFCtagActivity extends Activity {
NdefMessage msg;
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mNfcAdapter.enableForegroundNdefPush(this,msg );
}
NfcAdapter mNfcAdapter;
TextView textView;
Button btnEmulation;
PendingIntent mNfcPendingIntent;
IntentFilter[] mNdefExchangeFilters;
NdefMessage message;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// TextView textView = (TextView) findViewById(R.id.textView);
// Check for available NFC Adapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
// Register callback
// mNfcAdapter.setNdefPushMessageCallback(this, this);
String text = ("Beam me up, Android!\n\n" +
"Beam Time: " + System.currentTimeMillis());
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")
});
}
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;
}
}
NfcAdapter.setNdefPushMessageCallback()
is only available in Android 4.0 ICS and later. NfcAdapter.setNdefPushMessageCallback()
仅在Android 4.0 ICS和更高版本中可用。 Use enableForegroundNdefPush()
instead. 请改用
enableForegroundNdefPush()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.