Пример #1
0
        /// <summary>
        /// Sends an NDEF message to the remote device.
        /// </summary>
        /// <param name="type">The type of NDEF Record</param>
        /// <param name="ID">The ID of NDEF Record</param>
        /// <param name="payload">The payload of NDEF Record</param>
        /// <param name="payloadLength">The length of payload</param>
        /// <returns>Returns true if initialize succeeded, false otherwise</returns>
        public bool SendNdef(byte[] type, byte[] ID, byte[] payload)
        {
            NfcNdefRecord  ndefRecord  = new NfcNdefRecord(NfcRecordTypeNameFormat.WellKnown, type, ID, payload, (uint)payload.Length);
            NfcNdefMessage ndefMessage = new NfcNdefMessage();

            try
            {
                ndefMessage.AppendRecord(ndefRecord);
            }
            catch (Exception e)
            {
                Tizen.Log.Debug("NFC_APP", "Failed append record " + e.Message);
                return(false);
            }

            try
            {
                p2pDevice.SendNdefMessageAsync(ndefMessage);
                return(true);
            }
            catch (Exception e)
            {
                Tizen.Log.Debug("NFC_APP", "Failed send message " + e.Message);
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Send an NDEF message to the remote device
        /// </summary>
        /// <param name="type">The type of NDEF Record</param>
        /// <param name="ID">The ID of NDEF Record</param>
        /// <param name="payload">The payload of NDEF Record</param>
        /// <param name="payloadLength">The length of payload</param>
        /// <returns>Returns true if initialize succeeded, false otherwise</returns>
        public bool SendNDEF(byte[] type, byte[] ID, byte[] payload, uint payloadLength)
        {
            NfcNdefRecord  NfcNdefRecord  = new NfcNdefRecord(NfcRecordTypeNameFormat.WellKnown, type, ID, payload, payloadLength);
            NfcNdefMessage NfcNdefMessage = new NfcNdefMessage();

            try
            {
                NfcNdefMessage.AppendRecord(NfcNdefRecord);
            }
            catch (Exception e)
            {
                LogImplementation.DLog("Failed append record " + e.Message);
                return(false);
            }

            try
            {
                nfcP2P.SendNdefMessageAsync(NfcNdefMessage);
                return(true);
            }
            catch (Exception e)
            {
                LogImplementation.DLog("Failed send message " + e.Message);
                return(false);
            }
        }
Пример #3
0
        private void ManageWriteMimeOperation()
        {
            NfcNdefRecord record = new NfcNdefRecord
            {
                TypeFormat = NfcNdefTypeFormat.Mime,
                Payload    = NfcUtils.EncodeToByteArray(mimeToWrite)
            };

            OnNfcTagMimeWriten?.Invoke(this, WriteNfcNdefRecord(record));
        }
Пример #4
0
        private void ManageWriteUriOperation()
        {
            NfcNdefRecord record = new NfcNdefRecord
            {
                TypeFormat = NfcNdefTypeFormat.Uri,
                Payload    = NfcUtils.EncodeToByteArray(uriToWrite)
            };

            OnNfcTagUriWriten?.Invoke(this, WriteNfcNdefRecord(record));
        }
Пример #5
0
        private void ManageWriteTextOperation()
        {
            NfcNdefRecord record = new NfcNdefRecord
            {
                TypeFormat = NfcNdefTypeFormat.WellKnown,
                Payload    = NfcUtils.EncodeToByteArray(textToWrite),
            };

            OnNfcTagTextWriten?.Invoke(this, WriteNfcNdefRecord(record));
        }
Пример #6
0
        private void ManageCleanOperation()
        {
            //TODO 3. WRITING CARD 5
            Ndef          ndef   = null;
            NfcNdefRecord record = null;

            try
            {
                ndef = Ndef.Get(currentTag);
                if (!CheckWriteOperation(ndef, record))
                {
                    OnNfcTagCleaned?.Invoke(this, false);
                    return;
                }

                ndef.Connect();

                byte[]      empty   = Array.Empty <byte>();
                NdefMessage message = new NdefMessage(new NdefRecord[1] {
                    new NdefRecord(NdefRecord.TnfEmpty, empty, empty, empty)
                });

                ndef.WriteNdefMessage(message);
                OnNfcTagCleaned?.Invoke(this, true);
                return;
            }
            catch (Android.Nfc.TagLostException tlex)
            {
                Debug.WriteLine($"Tag Lost Error: {tlex.Message}");
            }
            catch (Java.IO.IOException ioex)
            {
                Debug.WriteLine($"Tag IO Error: {ioex.Message}");
            }
            catch (Android.Nfc.FormatException fe)
            {
                Debug.WriteLine($"Tag Format Error: {fe.Message}");
            }
            catch
            {
                Debug.WriteLine($"Tag Error");
            }
            finally
            {
                if (ndef?.IsConnected == true)
                {
                    ndef.Close();
                }

                currentTag    = null;
                currentIntent = null;
            }

            OnNfcTagCleaned?.Invoke(this, false);
        }
Пример #7
0
 /// <summary>
 /// Event handler called when the NDEF message is received
 /// </summary>
 /// <param name="sender">The sender object</param>
 /// <param name="e">Argument of Event</param>
 private void Received(object sender, P2pDataReceivedEventArgs e)
 {
     try
     {
         NfcNdefRecord record = e.NdefMessage.GetRecord(0);
         record_count = e.NdefMessage.RecordCount;
         messageReceiveCallback(record_count);
     }
     catch (Exception exc)
     {
         Tizen.Log.Debug("NFC_APP", "Failed getting record " + exc.Message);
     }
 }
Пример #8
0
        /// <summary>
        /// Callback function to be called when the NDEF message is received
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">Argument of Event</param>
        void Received(object sender, P2pDataReceivedEventArgs e)
        {
            try
            {
                NfcNdefRecord record = e.NdefMessage.GetRecord(0);
            }
            catch (Exception exc)
            {
                LogImplementation.DLog("Failed getting record " + exc.Message);
            }

            record_count = e.NdefMessage.RecordCount;
            LogImplementation.DLog("Record Count " + e.NdefMessage.RecordCount);

            callback(record_count);
        }
Пример #9
0
        private NfcNdefRecord[] GetRecords(NdefRecord[] records)
        {
            var results = new NfcNdefRecord[records.Length];

            for (var i = 0; i < records.Length; i++)
            {
                var ndefRecord = new NfcNdefRecord
                {
                    TypeFormat = (NfcNdefTypeFormat)records[i].Tnf,
                    Uri        = records[i].ToUri()?.ToString(),
                    MimeType   = records[i].ToMimeType(),
                    Payload    = records[i].GetPayload()
                };
                results.SetValue(ndefRecord, i);
            }
            return(results);
        }
Пример #10
0
 private NfcNdefRecord[] GetRecords(NFCNdefPayload[] records)
 {
     NfcNdefRecord[] results = new NfcNdefRecord[records.Length];
     for (var i = 0; i < records.Length; i++)
     {
         NFCNdefPayload record     = records[i];
         var            ndefRecord = new NfcNdefRecord
         {
             TypeFormat = (NfcNdefTypeFormat)record.TypeNameFormat,
             Uri        = records[i].ToUri()?.ToString(),
             MimeType   = records[i].ToMimeType(),
             Payload    = record.Payload.ToByteArray()
         };
         results.SetValue(ndefRecord, i);
     }
     return(results);
 }
Пример #11
0
        private async Task ShowNfcMessage(NfcNdefRecord record)
        {
            if (record == null)
            {
                return;
            }

            string message = $"Message: {record.Message}";

            message += Environment.NewLine;
            message += $"RawMessage: {Encoding.UTF8.GetString(record.Payload)}";
            message += Environment.NewLine;
            message += $"Type: {record.TypeFormat}";

            if (!string.IsNullOrWhiteSpace(record.MimeType))
            {
                message += Environment.NewLine;
                message += $"MimeType: {record.MimeType}";
            }

            await DisplayAlert("NFC card read", message, "Ok");
        }
Пример #12
0
        private bool CheckWriteOperation(Ndef ndef, NfcNdefRecord record)
        {
            //TODO 3. WRITING CARD 6
            if (ndef == null)
            {
                return(false);
            }

            if (!ndef.IsWritable)
            {
                Debug.WriteLine("NFC tag is readonly");
                return(false);
            }

            if (ndef.MaxSize < NfcUtils.GetSize(new NfcNdefRecord[] { record }))
            {
                Debug.WriteLine("NFC tag size is less than the message to write");
                return(false);
            }

            return(true);
        }
Пример #13
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);
        }
Пример #14
0
        private bool WriteNfcNdefRecord(NfcNdefRecord record)
        {
            //TODO 3. WRITING CARD 4
            Ndef ndef = null;

            try
            {
                ndef = Ndef.Get(currentTag);
                if (!CheckWriteOperation(ndef, record))
                {
                    return(false);
                }

                ndef.Connect();

                NdefMessage       message = null;
                List <NdefRecord> records = new List <NdefRecord>();
                if (GetAndroidNdefRecord(record) is NdefRecord ndefRecord)
                {
                    records.Add(ndefRecord);
                }

                if (records.Any())
                {
                    message = new NdefMessage(records.ToArray());
                }

                if (message == null)
                {
                    Debug.WriteLine("NFC tag can not be writen with null message");
                    return(false);
                }

                ndef.WriteNdefMessage(message);
                return(true);
            }
            catch (Android.Nfc.TagLostException tlex)
            {
                Debug.WriteLine($"Tag Lost Error: {tlex.Message}");
            }
            catch (Java.IO.IOException ioex)
            {
                Debug.WriteLine($"Tag IO Error: {ioex.Message}");
            }
            catch (Android.Nfc.FormatException fe)
            {
                Debug.WriteLine($"Tag Format Error: {fe.Message}");
            }
            catch
            {
                Debug.WriteLine($"Tag Error");
            }
            finally
            {
                if (ndef?.IsConnected == true)
                {
                    ndef.Close();
                }

                currentTag    = null;
                currentIntent = null;
            }

            return(false);
        }