コード例 #1
0
        private Boolean FormatNFC(Ndef ndef)
        {
            bool retorno = false;

            NdefFormatable ndefFormatable = NdefFormatable.Get(ndef.Tag);

            Java.Lang.String msg = new Java.Lang.String(MENSAGEM_PADRAO);
            try
            {
                if (ndefFormatable == null)
                {
                    return(retorno);
                }

                if (!ndefFormatable.IsConnected)
                {
                    ndefFormatable.Connect();
                }
                ndefFormatable.Format(new NdefMessage(NdefRecord.CreateMime
                                                          ("UTF-8", msg.GetBytes(Charset.ForName("UTF-8")))));
                ndefFormatable.Close();
                retorno = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new System.Exception("Não foi possível ler o cartão.");
            }

            return(retorno);
        }
コード例 #2
0
        private bool?FormatNdef(Tag tag, NdefMessage ndefMsg)
        {
            NdefFormatable ndef = NdefFormatable.Get(tag);

            if (ndef == null)
            {
                ShowToast("Tag is not NDEF formatable.");
                return(false);
            }

            try
            {
                ndef.Connect();
                ndef.Format(ndefMsg);
            }
            catch (Exception e)
            {
                ShowToast("Failed formating tag: " + e.Message);
                return(null);
            }
            finally
            {
                try
                {
                    ndef.Close();
                }
                catch (Exception)
                {
                    ShowToast("Tag connection failed to close.");
                }
            }

            return(true);
        }
コード例 #3
0
        private void OnWriteTag(Intent intent, string content)
        {
            if (null != content)
            {
                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(content);
                        var mimeBytes   = Encoding.ASCII.GetBytes("text/plain");
                        var record      = new NdefRecord(NdefRecord.TnfWellKnown, mimeBytes, new byte[0], payload);
                        var ndefMessage = new NdefMessage(new[] { record });

                        ndef.Connect();
                        ndef.WriteNdefMessage(ndefMessage);
                        ndef.Close();
                    }
                    else
                    {
                        NdefFormatable ndefFormatable = NdefFormatable.Get(tag);

                        if (ndefFormatable != null)
                        {
                            try
                            {
                                var payload     = Encoding.ASCII.GetBytes(content);
                                var mimeBytes   = Encoding.ASCII.GetBytes("text/plain");
                                var record      = new NdefRecord(NdefRecord.TnfWellKnown, mimeBytes, new byte[0], payload);
                                var ndefMessage = new NdefMessage(new[] { record });

                                ndefFormatable.Connect();
                                ndefFormatable.Format(ndefMessage);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                            finally
                            {
                                try
                                {
                                    ndefFormatable.Close();
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #4
0
        public WriteResponse WriteTag(NdefMessage message, Tag tag)
        {
            int    size = message.ToByteArray().Length;
            string mess = "";

            try {
                Ndef ndef = Ndef.Get(tag);
                if (ndef != null)
                {
                    ndef.Connect();

                    if (!ndef.IsWritable)
                    {
                        return(new WriteResponse(0, "Tag is read-only"));
                    }
                    if (ndef.MaxSize < size)
                    {
                        mess = "Tag capacity is " + ndef.MaxSize + " bytes, message is " + size
                               + " bytes.";
                        return(new WriteResponse(0, mess));
                    }

                    ndef.WriteNdefMessage(message);
                    if (writeProtect)
                    {
                        ndef.MakeReadOnly();
                    }
                    mess = "Wrote message to pre-formatted tag.";
                    return(new WriteResponse(1, mess));
                }
                else
                {
                    NdefFormatable format = NdefFormatable.Get(tag);
                    if (format != null)
                    {
                        try {
                            format.Connect();
                            format.Format(message);
                            mess = "Formatted tag and wrote message";
                            return(new WriteResponse(1, mess));
                        } catch (IOException) {
                            mess = "Failed to format tag.";
                            return(new WriteResponse(0, mess));
                        }
                    }
                    else
                    {
                        mess = "Tag doesn't support NDEF.";
                        return(new WriteResponse(0, mess));
                    }
                }
            } catch (Exception) {
                mess = "Failed to write tag";
                return(new WriteResponse(0, mess));
            }
        }
コード例 #5
0
        public override WriteResult WriteTag(NdefMessage message, Tag tag)
        {
            if (tag == null)
            {
                return(WriteResult.NULL);
            }

            try
            {
                var ndefTag = Ndef.Get(tag);
                if (ndefTag == null)
                {
                    NdefFormatable nForm = NdefFormatable.Get(tag);
                    if (nForm != null)
                    {
                        try
                        {
                            nForm.Connect();
                            nForm.Format(message);
                            nForm.Close();
                            return(WriteResult.OK);
                        } catch { return(WriteResult.FORMATFAILED); }
                    }
                    return(WriteResult.NOTSUPPORTED);
                }
                else
                {
                    ndefTag.Connect();
                    if (ndefTag.MaxSize < message.ToByteArray().Length)
                    {
                        return(WriteResult.TOOLARGE);
                    }
                    if (ndefTag.IsWritable)
                    {
                        ndefTag.WriteNdefMessage(message);
                        ndefTag.Close();
                        return(WriteResult.OK);
                    }
                    return(WriteResult.READONLY);
                }
            }
            catch { return(WriteResult.FAILED); }
        }
コード例 #6
0
        public NFCMessage NdefFormatable_FormatTag(Tag tag)
        {
            if (!tag.GetTechList().Contains(Tech_NdefFormatable))
            {
                return(NFCMessage.NFC_UN_FORMATABLE_TAG);
            }
            NdefFormatable ndefFormatable = NdefFormatable.Get(tag);

            if (ndefFormatable == null)
            {
                return(NFCMessage.NFC_CANT_FORMAT);
            }
            ndefFormatable.Connect();
            NdefRecord  record  = NdefRecord.CreateMime("text/plain", Encoding.ASCII.GetBytes("New"));
            NdefMessage message = new NdefMessage(new NdefRecord[] { record });

            ndefFormatable.Format(message);
            ndefFormatable.Close();
            OnFormatting_NdefTag?.Invoke();
            return(NFCMessage.NFC_TAG_FORMATED);
        }
コード例 #7
0
        /**
         *
         * Método faz a formatação do cartão.
         *
         * A formatação do cartão só é necessario na sua primeira gravação.
         *
         * Após já existir algum valor gravado no cartão, não será possível formata-lo
         * novamente.
         *
         * @param ndef = Contém as informações do cartão que esta sendo lido.
         *
         * @throws IOException
         * @throws FormatException
         *
         * @return boolean =>  True = Cartão Formatado / False = Cartão não formatado
         *
         * */
        public bool FormataCartao(Ndef ndef)
        {
            bool retorno = false;

            NdefFormatable ndefFormatable = NdefFormatable.Get(ndef.Tag);

            Java.Lang.String msg = new Java.Lang.String(MENSAGEM_PADRAO);
            try
            {
                if (ndefFormatable == null)
                {
                    return(retorno);
                }

                if (!ndefFormatable.IsConnected)
                {
                    ndefFormatable.Connect();
                }
                ndefFormatable.Format(new NdefMessage(NdefRecord.CreateMime
                                                          ("UTF-8", msg.GetBytes(Charset.ForName("UTF-8")))));
                ndefFormatable.Close();
                retorno = true;
            }
            catch (IOException e)
            {
                throw new IOException(e.Message);
            }
            catch (System.FormatException e)
            {
                throw new System.FormatException(e.Message);
            }
            finally
            {
                this.GravaTempoFinal();
            }


            return(retorno);
        }