コード例 #1
0
ファイル: Nfc.cs プロジェクト: melihercan/Xamarinme
        public async Task WriteNdefAsync(NdefMessage ndefMessage)
        {
            try
            {
                await _lockSemaphore.WaitAsync();

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

                var tcs = new TaskCompletionSource <object>();

                var iosNdefMessage = new NFCNdefMessage(ndefMessage.ToList().Select(record => new NFCNdefPayload
                                                                                    (
                                                                                        format: (NFCTypeNameFormat)record.TypeNameFormat,
                                                                                        type: NSData.FromArray(record.Type),
                                                                                        identifier: NSData.FromArray(record.Id),
                                                                                        payload: NSData.FromArray(record.Payload)
                                                                                    )).ToArray());
                _tag.WriteNdef(iosNdefMessage, (error) =>
                {
                    if (error != null)
                    {
                        throw new Exception(error.Description);
                    }

                    tcs.SetResult(null);
                });

                await tcs.Task;
            }
            finally
            {
                _lockSemaphore.Release();
            }
        }