Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsyncNFCReaderSessions #132

Merged
merged 17 commits into from
Jan 28, 2024
Merged

Add AsyncNFCReaderSessions #132

merged 17 commits into from
Jan 28, 2024

Conversation

treastrain
Copy link
Owner

The new reader session wrappers include a new instance property that provides the reader session's events via AsyncSequence, inspired by the CardSession.eventStream, the new API provided for card emulation in Core NFC on iOS 17.4+.

Example

import SwiftUI
import TRETNFCKit_Async

struct FeliCaReaderView: View {
    @State private var readerSession: AsyncNFCTagReaderSession?
    
    var body: some View {
        List {
            Button {
                readerSession = AsyncNFCTagReaderSession(pollingOption: .iso18092)
            } label: {
                Text("Read")
            }
            .disabled(readerSession != nil)
        }
        .task(id: readerSession == nil) {
            defer { readerSession = nil }
            guard let readerSession else { return }
            guard AsyncNFCTagReaderSession.readingAvailable else { return }
            
            for await event in readerSession.eventStream {
                switch event {
                case .sessionIsReady:
                    readerSession.alertMessage = "Place the tag on a flat, non-metal surface and rest your iPhone on the tag."
                    readerSession.start()
                case .sessionStarted:
                    break
                case .sessionBecomeActive:
                    break
                case .sessionDetected(let tags):
                    do {
                        let tag = tags.first!
                        guard case .feliCa(let feliCaTag) = tag else { throw NFCReaderError(.readerErrorInvalidParameter) }
                        try await readerSession.connect(to: tag)
                        let (idm, systemCode) = try await feliCaTag.polling(systemCode: Data([0xFE, 0x00]), requestCode: .systemCode, timeSlot: .max1)
                        readerSession.alertMessage = "\(systemCode as NSData)\n\(idm as NSData)"
                        readerSession.stop()
                    } catch {
                        readerSession.stop(errorMessage: error.localizedDescription)
                    }
                case .sessionCreationFailed(let reason):
                    print(reason)
                case .sessionInvalidated(let reason):
                    print(reason)
                }
            }
        }
        .navigationTitle("FeliCa Reader")
    }
}

@treastrain treastrain self-assigned this Jan 28, 2024
@treastrain treastrain merged commit a661043 into tretnfckit-main Jan 28, 2024
21 checks passed
@treastrain treastrain deleted the async-stream branch January 28, 2024 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant