예제 #1
1
		protected override void OnNewIntent(Intent intent)
		{
			if (_inWriteMode)
			{
				_inWriteMode = false;
				var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

				if (tag == null)
				{
					return;
				}

				// These next few lines will create a payload (consisting of a string)
				// and a mimetype. NFC record are arrays of bytes. 
				var message = _inputText.Text += " " + DateTime.Now.ToString("HH:mm:ss dd/M/yyyy");
				_outputText.Text = message;
				var payload = Encoding.ASCII.GetBytes(message);
				var mimeBytes = Encoding.ASCII.GetBytes(ViewIsolationType);
				var isolationRecord = new NdefRecord(NdefRecord.TnfMimeMedia, mimeBytes, new byte[0], payload);
				var ndefMessage = new NdefMessage(new[] { isolationRecord });

				TryAndFormatTagWithMessage(tag, ndefMessage);  
				if (!TryAndWriteToTag(tag, ndefMessage))
				{
					// Maybe the write couldn't happen because the tag wasn't formatted?
					TryAndFormatTagWithMessage(tag, ndefMessage);                    
				}
			}
		}
        /// <summary>
        /// This method is called when an NFC tag is discovered by the application.
        /// </summary>
        /// <param name="intent"></param>
        protected override void OnNewIntent(Intent intent)
        {
            if (_inWriteMode)
            {
                _inWriteMode = false;
                var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

                if (tag == null)
                {
                    return;
                }

                // These next few lines will create a payload (consisting of a string)
                // and a mimetype. NFC record are arrays of bytes.
                var payload = Encoding.ASCII.GetBytes(GetRandomHominid());
                var mimeBytes = Encoding.ASCII.GetBytes(ViewApeMimeType);
                var apeRecord = new NdefRecord(NdefRecord.TnfMimeMedia, mimeBytes, new byte[0], payload);
                var ndefMessage = new NdefMessage(new[] { apeRecord });

                if (!TryAndWriteToTag(tag, ndefMessage))
                {
                    // Maybe the write couldn't happen because the tag wasn't formatted?
                    TryAndFormatTagWithMessage(tag, ndefMessage);
                }
            }
        }
		public NdefMessage CreateNdefMessage (NfcEvent evt) 
		{
			DateTime time = DateTime.Now;
			var text = ("Beam me up!\n\n" + 
			               "Beam Time: " + time.ToString ("HH:mm:ss"));
			NdefMessage msg = new NdefMessage (
			new NdefRecord[] { CreateMimeRecord (
				"application/com.example.android.beam", Encoding.UTF8.GetBytes (text))
			/**
			* The Android Application Record (AAR) is commented out. When a device
			* receives a push with an AAR in it, the application specified in the AAR
			* is guaranteed to run. The AAR overrides the tag dispatch system.
			* You can add it back in to guarantee that this
			* activity starts when receiving a beamed message. For now, this code
			* uses the tag dispatch system.
			*/
			//,NdefRecord.CreateApplicationRecord("com.example.android.beam")
			});
			return msg;
		}
예제 #4
0
		private bool TryAndFormatTagWithMessage(Tag tag, NdefMessage ndefMessage)
		{
			var format = NdefFormatable.Get(tag);
			if (format == null)
			{
				_alertMessage.Text = "Tag does not appear to support NDEF format.";
			}
			else
			{
				try
				{
					format.Connect();
					format.Format(ndefMessage);
					_alertMessage.Text = "Tag successfully written.";
					return true;
				}
				catch (IOException ioex)
				{
					_alertMessage.Text = "There was an error trying to format the tag: "+ioex.Message;
				}
			}
			return false;
		}
예제 #5
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);
			}

		}
예제 #6
0
		private bool TryAndWriteToTag(Tag tag, NdefMessage ndefMessage)
		{

			// This object is used to get information about the NFC tag as 
			// well as perform operations on it.
			var ndef = Ndef.Get(tag); 
			if (ndef != null)
			{
				ndef.Connect();

				// Once written to, a tag can be marked as read-only - check for this.
				if (!ndef.IsWritable)
				{
					_alertMessage.Text = "Tag is read-only.";
				}

				// NFC tags can only store a small amount of data, this depends on the type of tag its.
				var size = ndefMessage.ToByteArray().Length;
				if (ndef.MaxSize < size)
				{
					_alertMessage.Text = "Tag doesn't have enough space.";
				}

				ndef.WriteNdefMessage(ndefMessage);
				_alertMessage.Text = "Succesfully wrote tag.";
				return true;
			}

			return false;
		}
 /// <summary>
 /// 
 /// </summary>
 /// <param name="tag"></param>
 /// <param name="ndefMessage"></param>
 /// <returns></returns>
 private bool TryAndFormatTagWithMessage(Tag tag, NdefMessage ndefMessage)
 {
     var format = NdefFormatable.Get(tag);
     if (format == null)
     {
         DisplayMessage("Tag does not appear to support NDEF format.");
     }
     else
     {
         try
         {
             format.Connect();
             format.Format(ndefMessage);
             DisplayMessage("Tag successfully written.");
             return true;
         }
         catch (IOException ioex)
         {
             var msg = "There was an error trying to format the tag.";
             DisplayMessage(msg);
             Log.Error(Tag, ioex, msg);
         }
     }
     return false;
 }
예제 #8
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);
            }
        }