예제 #1
0
파일: Nfc.cs 프로젝트: melihercan/Xamarinme
        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();
            }
        }
예제 #2
0
파일: Nfc.cs 프로젝트: melihercan/Xamarinme
        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();
            }
        }