Xamarin iOS-NFC Reader-INFCNdefReaderSessionDelegate-DidDetect没有被调用

时间:2018-09-28 11:16:23

标签: xamarin.ios

我有一个跨平台Xamarin应用程序iOS,想添加nfc-scan功能。 我点击了链接-https://docs.microsoft.com/en-us/xamarin/ios/platform/introduction-to-ios11/corenfc

这些已正确配置-

  • Info.plist隐私密钥。
  • Entitlements.plist条目。
  • 具有NFC标签读取功能的
  • 配置文件。

我将所有内容放入iOS类库中提供的新RFIDReader类中,并且从我的ViewModel类中调用了此方法。 如果我运行此代码,一切看起来都会很好。当我按下手机上的按钮时,扫描开始,它显示了蓝色的勾号,但随后却没有进入已实现的方法DidDetect,DidInvalidate中的一种。

您知道可能是什么原因吗?

这是我的代码-

/// <summary>
/// Class for RFID form control
/// </summary>
public sealed class RFIDReader : UIViewController, IRFIDReader, INFCNdefReaderSessionDelegate
{
    /// <summary>
    /// The scanning session
    /// </summary>
    private NFCNdefReaderSession session;

    /// <summary>
    /// The task completion source
    /// </summary>
    private TaskCompletionSource<string> tcs;

    /// <summary>
    /// Gets the handle
    /// </summary>
    /// <value>The handle</value>
    public new IntPtr Handle { get; }

    /// <summary>
    /// Read the string characters from scanning
    /// </summary>
    /// <returns>Task status</returns>
    public Task<string> ScanCode()
    {
        if (!NFCNdefReaderSession.ReadingAvailable)
        {
            //// NFC is not available or disabled
            throw new Exception(FormsErrorConstants.NearFieldCommunicationDisabled);
        }

        this.tcs = new TaskCompletionSource<string>();
        this.session = new NFCNdefReaderSession(this, DispatchQueue.CurrentQueue, true);
        this.session.BeginSession();

        return this.tcs.Task;
    }

    /// <summary>
    /// Called when a tag is successfully read
    /// </summary>
    /// <param name="session">Session</param>
    /// <param name="messages">Messages</param>
    public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
    {
        var bytes = messages[0].Records[0].Payload.Skip(3).ToArray();
        var message = Encoding.UTF8.GetString(bytes);

        this.tcs.SetResult(message);
    }

    /// <summary>
    /// Called when an error occurs or the 60 second timeout is reached
    /// </summary>
    /// <param name="session">Session</param>
    /// <param name="error">Error</param>
    public void DidInvalidate(NFCNdefReaderSession session, NSError error)
    {
        var readerError = (NFCReaderError)(long)error.Code;
        if (readerError != NFCReaderError.ReaderSessionInvalidationErrorFirstNDEFTagRead &&
            readerError != NFCReaderError.ReaderSessionInvalidationErrorUserCanceled)
        {
            // some error handling
        }

        this.tcs.SetResult(string.Empty);
    }

    /// <summary>
    /// Clean up method
    /// </summary>
    public new void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    /// <summary>
    /// Clean up method
    /// </summary>
    /// <param name="isDisposing">is disposing</param>
    private new void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            // Dispose
            this.session.InvalidateSession();
            this.session = null;
        }
    }
}

0 个答案:

没有答案