Example #1
0
        /**
         * Callback when a new tag is discovered by the system.
         *
         * <p>Communication with the card should take place here.
         *
         * @param tag Discovered tag
         */
        public void OnTagDiscovered(Android.Nfc.Tag tag)
        {
            Console.WriteLine("New tag discovered");
            IsoDep isoDep = IsoDep.Get(tag);

            if (isoDep != null)
            {
                try
                {
                    // Connect to the remote NFC device
                    isoDep.Connect();

                    byte[] command = new byte[] {
                        (byte)0x00,      /* CLA = 00 (first interindustry command set) */
                        (byte)0xA4,      /* INS = A4 (SELECT) */
                        (byte)0x04,      /* P1  = 04 (select file by DF name) */
                        (byte)0x0C,      /* P2  = 0C (first or only file; no FCI) */
                        (byte)0x07,      /* Lc  = 7  (data/AID has 7 bytes) */
                                         /* AID = D6 16 00 00 30 01 01: */
                        (byte)0xD6, (byte)0x16, (byte)0x00, (byte)0x00,
                        (byte)0x30, (byte)0x01, (byte)0x01,
                        (byte)0x00
                    };
                    byte[] result = isoDep.Transceive(command);
                    // If AID is successfully selected, 0x9000 is returned as the status word (last 2
                    // bytes of the result) by convention. Everything before the status word is
                    // optional payload, which is used here to hold the account number.
                    int    resultLength = result.Length;
                    byte[] statusWord   = { result[resultLength - 2], result[resultLength - 1] };
                    byte[] payload      = new byte[resultLength - 2];
                    Array.Copy(result, payload, resultLength - 2);
                    bool arrayEquals = SELECT_OK_SW.Length == statusWord.Length;

                    for (int i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++)
                    {
                        arrayEquals = (SELECT_OK_SW[i] == statusWord[i]);
                        if (!arrayEquals)
                        {
                            break;
                        }
                    }
                    if (arrayEquals)
                    {
                        // The remote NFC device will immediately respond with its stored account number
                        string info = System.Text.Encoding.UTF8.GetString(result);
                        Console.WriteLine("Received: " + info);
                        // Inform CardReaderFragment of received account number
                        InfoCallback ourCallback;
                        if (callback.TryGetTarget(out ourCallback))
                        {
                            ourCallback.InfoRecieved(info);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error communicating with card: " + e.Message);
                }
            }
        }
Example #2
0
		private Ndef GetNdef(Tag tag)
		{
			Ndef ndef = Ndef.Get(tag);
			if (ndef == null)
				return null;
			else
				return ndef;
		}
        /**
         * Callback when a new tag is discovered by the system.
         *
         * <p>Communication with the card should take place here.
         *
         * @param tag Discovered tag
         */
        public void OnTagDiscovered(Android.Nfc.Tag tag)
        {
            Log.Info(TAG, "New tag discovered");
            IsoDep isoDep = IsoDep.Get(tag);

            if (isoDep != null)
            {
                try{
                    // Connect to the remote NFC device
                    isoDep.Connect();
                    // Build SELECT AID command for our loyalty card service.
                    // This command tells the remote device which service we wish to communicate with.
                    Log.Info(TAG, "Requesting remote AID: " + SAMPLE_LOYALTY_CARD_AID);
                    byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);
                    // Send command to remote device
                    Log.Info(TAG, "Sending: " + ByteArrayToHexString(command));
                    byte[] result = isoDep.Transceive(command);
                    // If AID is successfully selected, 0x9000 is returned as the status word (last 2
                    // bytes of the result) by convention. Everything before the status word is
                    // optional payload, which is used here to hold the account number.
                    int    resultLength = result.Length;
                    byte[] statusWord   = { result[resultLength - 2], result[resultLength - 1] };
                    byte[] payload      = new byte[resultLength - 2];
                    Array.Copy(result, payload, resultLength - 2);
                    bool arrayEquals = SELECT_OK_SW.Length == statusWord.Length;

                    for (int i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++)
                    {
                        arrayEquals = (SELECT_OK_SW[i] == statusWord[i]);
                        if (!arrayEquals)
                        {
                            break;
                        }
                    }
                    if (arrayEquals)
                    {
                        // The remote NFC device will immediately respond with its stored account number
                        string accountNumber = System.Text.Encoding.UTF8.GetString(payload);
                        Log.Info(TAG, "Received: " + accountNumber);
                        // Inform CardReaderFragment of received account number
                        AccountCallback accountCallback;
                        if (mAccountCallback.TryGetTarget(out accountCallback))
                        {
                            accountCallback.OnAccountRecieved(accountNumber);
                        }
                    }
                } catch (Exception e) {
                    Log.Error(TAG, "Error communicating with card: " + e.Message);
                }
            }
        }
Example #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;
		}
Example #5
0
		public void OnNewIntent (object sender, Intent e)
		{
		    droidTag = e.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

			nfcTag.TechList = new List<string>(droidTag.GetTechList());
			nfcTag.Id = droidTag.GetId();

			if (GetNdef (droidTag) == null) 
			{
				nfcTag.IsNdefSupported = false;
			}
			else 
			{
				nfcTag.IsNdefSupported = true;
				Ndef ndef = GetNdef (droidTag);
				nfcTag.NdefMessage = ReadNdef (ndef);
				nfcTag.IsWriteable = ndef.IsWritable;
				nfcTag.MaxSize = ndef.MaxSize;
			}

			RaiseNewTag(nfcTag);
		}
Example #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;
 }
Example #8
0
//		public LoyaltyCardReader (WeakReference<AccountCallback> accountCallback)
//		{
//			mAccountCallback = accountCallback;
//		}


        /**
         * Callback when a new tag is discovered by the system.
         *
         * <p>Communication with the card should take place here.
         *
         * @param tag Discovered tag
         */
        public void OnTagDiscovered(Android.Nfc.Tag tag)
        {
            isoDep = IsoDep.Get(tag);
            if (isoDep != null)
            {
                try{
                    // Connect to the remote NFC device
                    isoDep.Connect();

                    Log.Info(TAG, "Tag connected");
                    // Build SELECT AID command for our loyalty card service.
                    // This command tells the remote device which service we wish to communicate with.

                    //byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);
                    // Send command to remote device

                    //byte[] result = isoDep.Transceive(command);
                    // If AID is successfully selected, 0x9000 is returned as the status word (last 2
                    // bytes of the result) by convention. Everything before the status word is
                    // optional payload, which is used here to hold the account number.
//					int resultLength = result.Length;
//					byte[] statusWord = {result[resultLength-2], result[resultLength-1]};
//					byte[] payload = new byte[resultLength-2];
//					Array.Copy(result, payload, resultLength-2);
//					bool arrayEquals = SELECT_OK_SW.Length == statusWord.Length;
//
//					for (int i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++)
//					{
//						arrayEquals = (SELECT_OK_SW[i] == statusWord[i]);
//						if (!arrayEquals)
//							break;
//					}
//					if (arrayEquals) {
//						//Console.WriteLine ("aaa");
//						// The remote NFC device will immediately respond with its stored account number
//						string accountNumber = System.Text.Encoding.UTF8.GetString (payload);
//						MainActivity.chatService.Write (payload);
//						//bluetoothChat.conversationArrayAdapter.Add ("Account: " + accountNumber);
//						// Inform CardReaderFragment of received account number
//						AccountCallback accountCallback;
//						if(mAccountCallback.TryGetTarget(out accountCallback))
//						{
//							accountCallback.OnAccountRecieved(accountNumber);
//						}
//					}
//					else {
                    //command = HexStringToByteArray("00A40000023F00");


                    // Send command to remote device

                    /*Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));
                     *
                     *
                     * string strCommand = StringToHexString(PPSE_CARD_AID);
                     * command = BuildSelectApdu(strCommand);
                     * // Send command to remote device
                     * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));
                     *
                     * command = HexStringToByteArray("00B2010C00");
                     * // Send command to remote device
                     * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));
                     * //}*/

//						command = BuildSelectApdu(UPAY2_CARD_AID);
//						// Send command to remote device
//						Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
//						result = isoDep.Transceive(command);
//						Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));


                    //MainActivity.conversationArrayAdapter.Add ("command: " + command.ToString());

                    /*command = HexStringToByteArray("00B2011400");
                     * // Send command to remote device
                     * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));
                     *
                     * command = BuildSelectApdu(UPAY1_CARD_AID);
                     * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));
                     *
                     * command = BuildSelectApdu(SPTC_CARD_AID);
                     * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));
                     *
                     * command = HexStringToByteArray("00B2011400");
                     * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));*/

                    /*command = BuildSelectApdu("A000000333010106");
                     * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command));
                     * result = isoDep.Transceive(command);
                     * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));*/
                    //MainActivity.chatService.Write (Encoding.UTF8.GetBytes(ByteArrayToHexString(result)));

                    /*string accountNumber = System.Text.Encoding.UTF8.GetString (result);
                     * AccountCallback accountCallback;
                     * if(mAccountCallback.TryGetTarget(out accountCallback))
                     * {
                     *      accountCallback.OnAccountRecieved(accountNumber);
                     * }*/

//					}
                } catch (Exception e) {
                }
            }
        }