示例#1
0
        /// <summary>
        ///     Publishes the URI.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns>Guid.</returns>
        public Guid PublishUri(Uri uri)
        {
            var key = Guid.NewGuid();

            _published.Add(key, NdefRecord.CreateUri(uri.AbsoluteUri));

            return(key);
        }
示例#2
0
        /// <summary>
        /// Transforms a <see cref="NFCNdefRecord"/> into an Android <see cref="NdefRecord"/>
        /// </summary>
        /// <param name="record">Object <see cref="NFCNdefRecord"/></param>
        /// <returns>Android <see cref="NdefRecord"/></returns>
        NdefRecord GetAndroidNdefRecord(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NdefRecord ndefRecord = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                var languageCode = record.LanguageCode;
                if (string.IsNullOrWhiteSpace(languageCode))
                {
                    languageCode = Configuration.DefaultLanguageCode;
                }
                if (languageCode.Length > 5)
                {
                    languageCode = languageCode.Substring(0, 5);                                                //max support 5 chars like en-US or de-AT
                }
                ndefRecord = NdefRecord.CreateTextRecord(languageCode, Encoding.UTF8.GetString(record.Payload));
                //no need to force it to 2 letters only
                //ndefRecord = NdefRecord.CreateTextRecord(languageCode.Substring(0, 2), Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.Mime:
                ndefRecord = NdefRecord.CreateMime(record.MimeType, record.Payload);
                break;

            case NFCNdefTypeFormat.Uri:
                ndefRecord = NdefRecord.CreateUri(Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.External:
                ndefRecord = NdefRecord.CreateExternal(record.ExternalDomain, record.ExternalType, record.Payload);
                break;

            case NFCNdefTypeFormat.Empty:
                ndefRecord = GetEmptyNdefRecord();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(ndefRecord);
        }
示例#3
0
        /// <summary>
        /// Transforms a <see cref="NFCNdefRecord"/> into an Android <see cref="NdefRecord"/>
        /// </summary>
        /// <param name="record">Object <see cref="NFCNdefRecord"/></param>
        /// <returns>Android <see cref="NdefRecord"/></returns>
        NdefRecord GetAndroidNdefRecord(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NdefRecord ndefRecord = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                var languageCode = record.LanguageCode;
                if (string.IsNullOrWhiteSpace(languageCode))
                {
                    languageCode = Configuration.DefaultLanguageCode;
                }
                ndefRecord = NdefRecord.CreateTextRecord(languageCode.Substring(0, 2), Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.Mime:
                ndefRecord = NdefRecord.CreateMime(record.MimeType, record.Payload);
                break;

            case NFCNdefTypeFormat.Uri:
                ndefRecord = NdefRecord.CreateUri(Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.External:
                ndefRecord = NdefRecord.CreateExternal(record.ExternalDomain, record.ExternalType, record.Payload);
                break;

            case NFCNdefTypeFormat.Empty:
                ndefRecord = GetEmptyNdefRecord();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(ndefRecord);
        }
示例#4
0
        /// <summary>
        /// Transforms a <see cref="NFCNdefRecord"/> into an Android <see cref="NdefRecord"/>
        /// </summary>
        /// <param name="record">Object <see cref="NFCNdefRecord"/></param>
        /// <returns>Android <see cref="NdefRecord"/></returns>
        NdefRecord GetAndroidNdefRecord(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NdefRecord ndefRecord = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                ndefRecord = NdefRecord.CreateTextRecord(Locale.Default.ToLanguageTag(), Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.Mime:
                ndefRecord = NdefRecord.CreateMime(record.MimeType, record.Payload);
                break;

            case NFCNdefTypeFormat.Uri:
                ndefRecord = NdefRecord.CreateUri(Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.External:
                ndefRecord = NdefRecord.CreateExternal(record.ExternalDomain, record.ExternalType, record.Payload);
                break;

            case NFCNdefTypeFormat.Empty:
                ndefRecord = GetEmptyNdefRecord();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(ndefRecord);
        }
示例#5
0
        private NdefRecord GetAndroidNdefRecord(NfcNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NdefRecord ndefRecord = null;

            switch (record.TypeFormat)
            {
            case NfcNdefTypeFormat.WellKnown:
                ndefRecord = NdefRecord.CreateTextRecord("en", Encoding.UTF8.GetString(record.Payload));
                break;

            case NfcNdefTypeFormat.Mime:
                ndefRecord = NdefRecord.CreateMime(record.MimeType, record.Payload);
                break;

            case NfcNdefTypeFormat.Uri:
                ndefRecord = NdefRecord.CreateUri(Encoding.UTF8.GetString(record.Payload));
                break;

            case NfcNdefTypeFormat.Empty:
                byte[] empty = Array.Empty <byte>();
                ndefRecord = new NdefRecord(NdefRecord.TnfEmpty, empty, empty, empty);
                break;

            case NfcNdefTypeFormat.External:
            case NfcNdefTypeFormat.Unknown:
            case NfcNdefTypeFormat.Unchanged:
            case NfcNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(ndefRecord);
        }
示例#6
0
        public bool WriteWellKnown(RecordTypeDefinition type, string content, object tag)
        {
            if (!(tag is Tag))
            {
                return(false);
            }
            if (string.IsNullOrEmpty(content))
            {
                return(false);
            }

            WriteResult writeResult = WriteResult.FAILED;
            NdefMessage ndefMessage = null;

            try
            {
                switch (type)
                {
                case RecordTypeDefinition.RTD_TEXT:

                    break;

                case RecordTypeDefinition.RTD_URI:
                    NdefRecord rtdUriRecord = NdefRecord.CreateUri("http://www.raulrodriguezpacho.com");
                    ndefMessage = new NdefMessage(new NdefRecord[] { rtdUriRecord });
                    break;
                }
                if (ndefMessage == null)
                {
                    return(false);
                }
                writeResult = WriteTag(ndefMessage, (Tag)tag);
            }
            catch { }
            return(writeResult == WriteResult.OK);
        }
示例#7
0
        protected override async void OnResume()
        {
            base.OnResume();
            try
            {
                if (TAGPage.write_nfc == true)
                {
                    //App.Navigator.PushAsync(new ConfigStikerPage());
                    int user_id = 0;
                    if (NfcAdapter.ActionNdefDiscovered.Equals(Intent.Action))
                    {
                        //Get the NFC ID
                        var myTag = Intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

                        var msg    = (NdefMessage)myTag[0];
                        var record = msg.GetRecords()[0];
                        //If the NFC Card ID is not null
                        if (record != null)
                        {
                            if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
                            {
                                // Get the transfered data
                                var      data          = Encoding.ASCII.GetString(record.GetPayload());
                                string   result        = data.Substring(1);
                                string[] variables2    = result.Split("?");
                                string[] variables     = result.Split("=");
                                string[] depura_userid = variables[1].Split('&');
                                if (depura_userid[0] == "1")
                                {
                                    user_id = Convert.ToInt32(depura_userid[0]);
                                }
                                else
                                {
                                    var id_user = Encoding.UTF8.GetString(Convert.FromBase64String(variables2[2]));
                                    user_id = Convert.ToInt32(id_user);
                                }
                            }
                        }
                    }

                    string dominio = "boxweb.azurewebsites.net/";
                    string user    = MainViewModel.GetInstance().User.UserId.ToString();
                    string tag_id  = "";
                    if (user_id == Convert.ToInt32(user) || 0 == user_id)
                    {
                        string url = "";
                        if (user == "1")
                        {
                            url = dominio + "index3.aspx?user_id=" + user + "&tag_id=" + tag_id;
                        }
                        else
                        {
                            var Fullname = Convert.ToBase64String(Encoding.UTF8.GetBytes(MainViewModel.GetInstance().User.FullName));
                            var UserName = Convert.ToBase64String(Encoding.UTF8.GetBytes(Convert.ToString(MainViewModel.GetInstance().User.UserId)));
                            url = dominio + "index3.aspx?" + Fullname + "?" + UserName + "?" + Convert.ToBase64String(Encoding.UTF8.GetBytes("&tag_id")) + "?";
                            //var user2 = Convert.ToBase64String(Encoding.UTF8.GetBytes(Convert.ToString(user)));
                            //user = user2;
                            //url = dominio + "index3.aspx?user_id=?" + user + "?&tag_id=?";
                        }
                        //http://localhost:58951/index.aspx?user_id=7
                        var tag = Intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
                        if (tag != null)
                        {
                            Ndef ndef = Ndef.Get(tag);
                            if (ndef != null && ndef.IsWritable)
                            {
                                /*var payload = Encoding.ASCII.GetBytes(url);
                                 * var mimeBytes = Encoding.ASCII.GetBytes("text/html");
                                 * var record = new NdefRecord(NdefRecord.TnfWellKnown, mimeBytes, new byte[0], payload);
                                 * var ndefMessage = new NdefMessage(new[] { record });
                                 * ndef.Connect();
                                 * ndef.WriteNdefMessage(ndefMessage);
                                 * ndef.Close();*/

                                ndef.Connect();
                                NdefRecord mimeRecord = NdefRecord.CreateUri("http://" + url);
                                ndef.WriteNdefMessage(new NdefMessage(mimeRecord));
                                ndef.Close();
                            }
                        }
                        TAGPage.write_nfc = false;

                        var duration = TimeSpan.FromMilliseconds(2000);
                        Vibration.Vibrate(duration);
                    }
                    else
                    {
                        System.Threading.Tasks.Task task = App.DisplayAlertAsync(Languages.MyTAGValidation);
                    }

                    await App.Navigator.PushAsync(new Stickerconfig());

                    //OnDestroy();
                }
                else
                {
                    if (NfcAdapter.ActionNdefDiscovered.Equals(Intent.Action))
                    {
                        //Get the NFC ID
                        var myTag = Intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

                        var msg    = (NdefMessage)myTag[0];
                        var record = msg.GetRecords()[0];
                        //If the NFC Card ID is not null
                        if (record != null)
                        {
                            if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
                            {
                                // Get the transfered data
                                var    data   = Encoding.ASCII.GetString(record.GetPayload());
                                string result = data.Substring(1);

                                string[] variables     = result.Split('=');
                                string[] depura_userid = variables[1].Split('&');
                                string   tag_id        = variables[2];
                                id_user = depura_userid[0];
                                if (id_user != "1")
                                {
                                    string[] variables2 = result.Split('?');
                                    var      name_user  = variables2[1];
                                    var      id_user2   = variables2[2];
                                    tag_id   = variables2[4];
                                    id_user  = Encoding.UTF8.GetString(Convert.FromBase64String(id_user2));
                                    nameUser = Encoding.UTF8.GetString(Convert.FromBase64String(name_user));
                                }
                                else if (id_user == "1")
                                {
                                    nameUser = "******";
                                }

                                MainViewModel.GetInstance().Imprime_box = new Imprime_box();
                                var A = MainViewModel.GetInstance().Imprime_box;
                                A.Consulta_user(id_user, tag_id, nameUser);
                                //OnDestroy();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            //DisableReaderMode();
        }
示例#8
0
        protected override void OnResume()
        {
            base.OnResume();
            try
            {
                if (TAGPage.write_nfc == true)
                {
                    //PopupNavigation.Instance.PopAsync();
                    PopupNavigation.Instance.PushAsync(new ConfigStikerPage());
                    int user_id = 0;
                    if (NfcAdapter.ActionNdefDiscovered.Equals(Intent.Action))
                    {
                        //Get the NFC ID
                        var myTag = Intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

                        var msg    = (NdefMessage)myTag[0];
                        var record = msg.GetRecords()[0];
                        //If the NFC Card ID is not null
                        if (record != null)
                        {
                            if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
                            {
                                // Get the transfered data
                                var    data   = Encoding.ASCII.GetString(record.GetPayload());
                                string result = data.Substring(1);

                                string[] variables     = result.Split('=');
                                string[] depura_userid = variables[1].Split('&');
                                user_id = Convert.ToInt32(depura_userid[0]);
                            }
                        }
                    }

                    string dominio = "boxweb.azurewebsites.net/";
                    string user    = MainViewModel.GetInstance().User.UserId.ToString();
                    string tag_id  = "";
                    if (user_id == Convert.ToInt32(user) || 0 == user_id)
                    {
                        string url = dominio + "index3.aspx?user_id=" + user + "&tag_id=" + tag_id;
                        //http://localhost:58951/index.aspx?user_id=7
                        var tag = Intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
                        if (tag != null)
                        {
                            Ndef ndef = Ndef.Get(tag);
                            if (ndef != null && ndef.IsWritable)
                            {
                                /*var payload = Encoding.ASCII.GetBytes(url);
                                 * var mimeBytes = Encoding.ASCII.GetBytes("text/html");
                                 * var record = new NdefRecord(NdefRecord.TnfWellKnown, mimeBytes, new byte[0], payload);
                                 * var ndefMessage = new NdefMessage(new[] { record });
                                 * ndef.Connect();
                                 * ndef.WriteNdefMessage(ndefMessage);
                                 * ndef.Close();*/

                                ndef.Connect();
                                NdefRecord mimeRecord = NdefRecord.CreateUri("http://" + url);
                                ndef.WriteNdefMessage(new NdefMessage(mimeRecord));
                                ndef.Close();
                            }
                        }
                        TAGPage.write_nfc = false;

                        var duration = TimeSpan.FromMilliseconds(2000);
                        Vibration.Vibrate(duration);
                    }
                    else
                    {
                        System.Threading.Tasks.Task task = App.DisplayAlertAsync("¡Este Tag esta vinculado con otro usuario!");
                    }
                    PopupNavigation.Instance.PopAsync();
                    PopupNavigation.Instance.PushAsync(new Stickerconfig());
                    Thread.Sleep(4000);
                    PopupNavigation.Instance.PopAsync();
                }
                else
                {
                    if (NfcAdapter.ActionNdefDiscovered.Equals(Intent.Action))
                    {
                        //Get the NFC ID
                        var myTag = Intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

                        var msg    = (NdefMessage)myTag[0];
                        var record = msg.GetRecords()[0];
                        //If the NFC Card ID is not null
                        if (record != null)
                        {
                            if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
                            {
                                // Get the transfered data
                                var    data   = Encoding.ASCII.GetString(record.GetPayload());
                                string result = data.Substring(1);

                                string[] variables     = result.Split('=');
                                string[] depura_userid = variables[1].Split('&');
                                string   tag_id        = variables[2];
                                string   user_id       = depura_userid[0];

                                Imprime_box.Consulta_user(user_id, tag_id);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }