简体   繁体   中英

How to read multiple NdefTextRecord from Nfc tag in windows phone 8

I am developing windows phone application regarding nfc tag where I am able to write all four records that are of type NdefTextRecord .There is no problem in publishing it .The main problem comes when reading the NDEF message I cant read any of the records that I published .I want to read all four records but when reading it reads nothing. Below is the code for both publishing messages.

        public void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
    {

        var fRecord = new NdefTextRecord{Text = TxtFloor.Text , LanguageCode = "en-US" };
        var zRecord = new NdefTextRecord { Text = TxtZone.Text, LanguageCode = "en-US" };
        var latRecord = new NdefTextRecord { Text = LatitudeTextBlock.Text, LanguageCode = "en-US" };
        var longRecord = new NdefTextRecord { Text = LongitudeTextBlock.Text, LanguageCode = "en-US" };

        var msg = new NdefMessage {};
        msg.Add(fRecord);
        msg.Add(zRecord);
        msg.Add(latRecord);
        msg.Add(longRecord);

        _device.PublishBinaryMessage(
            "NDEF:WriteTag",
            msg.ToByteArray().AsBuffer(),
            MessageWrittenHandler);

        SetStatusOutput("Message written");
        }

below is the code for subscribing

private void InitializeProximityDevice()
    {
        _device = Windows.Networking.Proximity.ProximityDevice.GetDefault();
        if (_device != null)
        {
            _subscriptionIdNdef = _device.SubscribeForMessage("NDEF", MessageReceivedHandler);

        }

    }

        private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
    {

        var rawMsg = message.Data.ToArray();
        var ndefMessage = NdefMessage.FromByteArray(rawMsg);


        // Loop over all records contained in the NDEF message
        foreach (NdefRecord record in ndefMessage)
        {

            if (NdefTextRecord.IsRecordType(record))
            {
                // Convert and extract URI info
                var textRecord = new NdefTextRecord(record);
                var str = textRecord;


                //SetLogStatus();


            }

Is your read code entering the foreach statement but failing the type check? Try

foreach (NdefRecord record in ndefMessage) {
    var recordType = record.CheckSpecializedType(false);

    if (recordType == typeof(NdefTextRecord)) {
    // ...
    }
}

Windows Phone only reads and acts upon the first record of the message. All the other records stored on the tag are ignored. If the first record in the message is not understood by the OS, the tag is ignored.

see: Multiple Records on Tags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM