Esempio n. 1
0
        public async Task WriteNdefAsync(NdefMessage ndefMessage)
        {
            Android.Nfc.Tech.Ndef ndef = null;

            try
            {
                await _lockSemaphore.WaitAsync();

                if (!_isNfcEnabled)
                {
                    throw new Exception("NFC is not enabled");
                }

                ndef = Android.Nfc.Tech.Ndef.Get(_tag);
                ndef.Connect();
                await ndef.WriteNdefMessageAsync(new Android.Nfc.NdefMessage(ndefMessage.ToByteArray()));

                ndef.Close();
            }
            catch (Exception ex)
            {
                if (ndef.IsConnected)
                {
                    ndef.Close();
                }
                throw ex;
            }
            finally
            {
                _lockSemaphore.Release();
            }
        }
Esempio n. 2
0
        public async Task <NdefMessage> ReadNdefAsync()
        {
            Android.Nfc.Tech.Ndef ndef = null;

            try
            {
                await _lockSemaphore.WaitAsync();

                if (!_isNfcEnabled)
                {
                    throw new Exception("NFC is not enabled");
                }

                ndef = Android.Nfc.Tech.Ndef.Get(_tag);
                ndef.Connect();
                var ndefMessage = NdefMessage.FromByteArray(ndef.NdefMessage.ToByteArray());
                ndef.Close();

                return(ndefMessage);
            }
            catch (Exception ex)
            {
                if (ndef.IsConnected)
                {
                    ndef.Close();
                }
                throw ex;
            }
            finally
            {
                _lockSemaphore.Release();
            }
        }