Ejemplo n.º 1
0
        public void WriteTag(NdefLibrary.Ndef.NdefMessage message)
        {
            int messageSize = 0;

            foreach (NdefRecord record in message)
            {
                messageSize += record.Payload.Length;
                if (!record.CheckIfValid())
                    throw new Exception("A record on NDEFMessage is not valid");
            }

            if (!isTagPresent)
                throw new Exception("No Tag present or Tag is incompatible ");

            if (!nfcTag.IsWriteable)
                throw new Exception("Tag is write locked ");

            if (nfcTag.MaxSize < messageSize)
                throw new Exception("Tag is too small for this message");

            RaiseTagConnected(nfcTag);

            nfcDevice.PublishBinaryMessage("NDEF:WriteTag", message.ToByteArray().AsBuffer(), writerHandler);
        }
Ejemplo n.º 2
0
		public void WriteTag (NdefLibrary.Ndef.NdefMessage message)
		{
			if (droidTag == null) 
			{
				throw new Exception("Tag Error: No Tag to write, register to NewTag event before calling WriteTag()");
			}

			Ndef ndef = GetNdef (droidTag);

			if (ndef == null) 
			{
				throw new Exception("Tag Error: NDEF not supported");
			}


			try
			{
				ndef.Connect();
				RaiseTagConnected (nfcTag);
			}

			catch
			{
				throw new Exception("Tag Error: No Tag nearby");
			}

			if(!ndef.IsWritable) 
			{				
				ndef.Close ();
				throw new Exception("Tag Error: Tag is write locked");
			}

			int size = message.ToByteArray ().Length;

			if(ndef.MaxSize < size) 
			{
				ndef.Close ();
				throw new Exception("Tag Error: Tag is too small");
			}

			try 
			{
				List<Android.Nfc.NdefRecord> records = new List<Android.Nfc.NdefRecord>();
				for(int i = 0; i< message.Count;i++)
				{
					if(message[i].CheckIfValid())
						records.Add(new Android.Nfc.NdefRecord(Android.Nfc.NdefRecord.TnfWellKnown,message[i].Type,message[i].Id,message[i].Payload));
					else
					{
						throw new Exception("NDEFRecord number " + i + "is not valid");
					}
				};
				Android.Nfc.NdefMessage msg = new Android.Nfc.NdefMessage(records.ToArray());
				ndef.WriteNdefMessage(msg);
			}

			catch (TagLostException tle) 
			{
				throw new Exception("Tag Lost Error: " + tle.Message);
			} 

			catch (IOException ioe) 
			{
				throw new Exception("Tag IO Error: " +  ioe.ToString());
			}

			catch (Android.Nfc.FormatException fe) 
			{
				throw new Exception("Tag Format Error: " + fe.Message);
			}

			catch (Exception e) 
			{
				throw new Exception("Tag Error: " + e.ToString());
			}

			finally
			{
				ndef.Close ();
				RaiseTagTagDisconnected (nfcTag);
			}

		}