我正在开发和应用程序来演示NFC的工作原理。我的目标是制作与Android Beam非常相似的应用程序。我正在使用Xamarin.Android。目的是将消息键入一台设备,按按钮,然后应将消息发送到具有相同应用程序的另一台设备上。我已经尝试了几乎所有内容,甚至是文档,但似乎不起作用。有人对此技术有经验吗?这项技术现在是否可用?
我有一些代码可以让您了解我要做什么:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
mNfcAdapter = NfcAdapter.GetDefaultAdapter(this);
myButton.Click += (e, o) => {
mNfcAdapter.SetNdefPushMessageCallback(this, this);
mNfcAdapter.SetOnNdefPushCompleteCallback(this, this);
};
}
public NdefMessage CreateNdefMessage(NfcEvent e)
{
DateTime time = DateTime.Now;
var text = (time.ToString("HH:mm:ss") + message2);
NdefMessage msg = new NdefMessage(
new NdefRecord[] { CreateMimeRecord (
text, Encoding.UTF8.GetBytes (text))});
return msg;
}
private NdefRecord CreateMimeRecord(string mimeType, byte[] payload)
{
byte[] mimeBytes = Encoding.UTF8.GetBytes(mimeType);
NdefRecord mimeRecord = new NdefRecord(
NdefRecord.TnfMimeMedia, mimeBytes, new byte[0], payload);
return mimeRecord;
}
public void OnNdefPushComplete(NfcEvent e)
{
Toast.MakeText(this.ApplicationContext, "Message sent", ToastLength.Long).Show();
}
protected override void OnResume()
{
base.OnResume();
if (NfcAdapter.ActionNdefDiscovered == Intent.Action)
{
ProcessIntent(Intent);
}
}
protected override void OnNewIntent(Intent intent)
{
Intent = intent;
}
void ProcessIntent(Intent intent)
{
IParcelable[] rawMsgs = intent.GetParcelableArrayExtra(
NfcAdapter.ExtraNdefMessages);
NdefMessage msg = (NdefMessage)rawMsgs[0];
var textViewMsg = FindViewById<TextView>(Resource.Id.textViewMsg);
textViewMsg.Text = Encoding.UTF8.GetString(msg.GetRecords()[0].GetPayload());
}
谢谢你们 :)
OnNdefPushComplete
并且不推荐使用整个Android Beam,并将其从Android 10中删除
https://developer.android.com/reference/android/nfc/NfcAdapter.OnNdefPushCompleteCallback
如果要继续进行“设备到设备” NFC,则一部手机可以进行主机卡仿真(HCE),而另一部手机可以使用 enableReaderMode
但是Google建议使用Bluetooth或Wifi Direct作为Android Beam的更可靠替代。Google提供的一种替代方法是Android附近的https://developers.google.com/nearby
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句