我如何发送Android Beam消息到nfc-explorer

时间:2019-01-22 20:31:15

标签: c# android xamarin nfc nfc-p2p

如何向nfc-explorer发送ndef消息?我尝试使用nfc资源管理器中的源代码,但始终会得到空的ndef消息。

1 个答案:

答案 0 :(得分:0)

您有完整的演示实现here,并阅读了official documentation.

要回答您的问题,请发送消息:

public NdefMessage CreateNdefMessage (NfcEvent evt)
{
    DateTime time = DateTime.Now;
    var text = ("Beam me up!\n\n" + "Beam Time: " +
        time.ToString ("HH:mm:ss"));
    NdefMessage msg = new NdefMessage (
        new NdefRecord[]{ CreateMimeRecord (
            "application/com.example.android.beam",
            Encoding.UTF8.GetBytes (text)) });
        } };
    return msg;
}

public 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;
}