Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to parse NDefRecord from record byte array. #15

Open
dberroa opened this issue Dec 30, 2017 · 2 comments
Open

Unable to parse NDefRecord from record byte array. #15

dberroa opened this issue Dec 30, 2017 · 2 comments

Comments

@dberroa
Copy link

dberroa commented Dec 30, 2017

I'm using this library for a Xamarin Forms project that supports iOS, Android, and UWP. I am using CoreNFC on iOS to read an NDEF tag. CoreNFC only gives me the bytes of an individual record.

For this library, I only see the ability to create an NDefMessage from a byte array and then loop through that to get the payloads. I have a record payload in byte form but don't see a way to init just a single record to get it's contents.

How can I get the contents from a NDEF Record byte array?

@acaliaro
Copy link

acaliaro commented Apr 5, 2019

Hi @dberroa
Have you a XF sample to use this library with ios and android?
Thanks

@dberroa
Copy link
Author

dberroa commented Apr 5, 2019

@acaliaro I don't have a sample that I can readily share as I went pretty deep in my integration that is tied to my code. I can copy and paste what I did for iOS and Android though that directly relates to this library.

For iOS when you use CoreNFC to perform a scan it calls back to DidDetect with an iOS class NFCNdefMessage passed in. If you get one of these records you can make your NdefRecord from this library from that. In the code below messages is the NFCNdefMessage[] messages from the delegate public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)

//Get the first record from the scanned messages.
            var record = messages.FirstOrDefault()?.Records?.FirstOrDefault();

            //Check that we have a scanned record.
            if (record != null)
            {
                //Create a common ndefrecord.            
                NdefRecord ndefRecord = new NdefRecord();

                //Fill in the ndef information
                ndefRecord.Payload = record?.Payload.ToArray();
                ndefRecord.Type = record?.Type?.ToArray();
                ndefRecord.TypeNameFormat = (NdefRecord.TypeNameFormatType)(record.TypeNameFormat);
            }

For android I'm a little rusty with. I don't remember how exactly I get to the code below but I believe a function gets called when the tag is scanned and in this function I have an Android.Nfc.Tag that is passed in. The custom function I made is called public void ReadNFCScan(object tagInformation = null) with the tagInformation as an object but you could have casted it before hand to a Tag.

`//Get the scanned tag as an NDEF Message.
Android.Nfc.NdefMessage tag = new Android.Nfc.NdefMessage(Android.Nfc.Tech.Ndef.Get(tagInformation as Android.Nfc.Tag)?.CachedNdefMessage.ToByteArray());

            //Make sure we have a tag.
            if (tag != null)
            {
                //Process the tag content.
                NdefMessage message = NdefMessage.FromByteArray(tag.ToByteArray());
            }`

So at this point with both examples I had a NdefMessage that was from this library and I was able to read it to get the information I want. I personally only needed the string content of the tag so I wrote the below function but this part you would need to handle however you are doing whatever it is you are coding:

`///


/// Takes in an ndef message and gets the content we want from it.
///

/// The ndef record containing the content of an NFC tag.
/// The string of the message.
public static String ProcessNFCRecord(NdefRecord record)
{
//Define the tag content we want to return.
String tagContent = null;

        //Make sure we have a record.
        if (record != null)
        {
            //Check if the record is a URL.
            if (record.CheckSpecializedType(true) == typeof(NdefUriRecord))
            {
                //The content is a URL.
                tagContent = new NdefUriRecord(record).Uri;
            }
            else if (record.CheckSpecializedType(true) == typeof(NdefMailtoRecord))
            {
                //The content is a mailto record.
                tagContent = new NdefMailtoRecord(record).Uri;
            }
            else if (record.CheckSpecializedType(true) == typeof(NdefTelRecord))
            {
                //The content is a tel record.
                tagContent = new NdefTelRecord(record).Uri;
            }
            else if (record.CheckSpecializedType(true) == typeof(NdefSmsRecord))
            {
                //The content is a sms record.
                tagContent = new NdefSmsRecord(record).Uri;
            }
            else if (record.CheckSpecializedType(true) == typeof(NdefTextRecord))
            {
                //The content is a text record.
                tagContent = new NdefTextRecord(record).Text;
            }
            else
            {
                //Try and force a pure text conversion.
                tagContent = Encoding.UTF8.GetString(record.Payload);
            }
        }

        //Return the tag content.
        return tagContent;
    }`

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants