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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Examples/TRETNFCKitExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -356,7 +356,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
44 changes: 43 additions & 1 deletion Examples/TRETNFCKitExample/NFCFeliCaTagReaderExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//

import SwiftUI
import TRETNFCKit_Async
import TRETNFCKit_FeliCa

struct NFCFeliCaTagReaderExampleView: View {
@State private var isPresented = false
@ObservedObject var viewModel = ViewModel()
@State private var readerSession: AsyncNFCTagReaderSession?

var body: some View {
List {
Expand All @@ -24,8 +26,14 @@ struct NFCFeliCaTagReaderExampleView: View {
try await viewModel.read()
}
} label: {
Text("Read")
Text("Read (using reader)")
}
Button {
readerSession = AsyncNFCTagReaderSession(pollingOption: .iso18092)
} label: {
Text("Read (using async stream)")
}
.disabled(readerSession != nil)
}
.feliCaTagReader(
isPresented: $isPresented,
Expand All @@ -47,6 +55,40 @@ struct NFCFeliCaTagReaderExampleView: View {
return .success
}
)
.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")
}
}
Expand Down
43 changes: 42 additions & 1 deletion Examples/TRETNFCKitExample/NFCISO15693TagReaderExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//

import SwiftUI
import TRETNFCKit_Async
import TRETNFCKit_ISO15693

struct NFCISO15693TagReaderExampleView: View {
@State private var isPresented = false
@ObservedObject var viewModel = ViewModel()
@State private var readerSession: AsyncNFCTagReaderSession?

var body: some View {
List {
Expand All @@ -24,8 +26,14 @@ struct NFCISO15693TagReaderExampleView: View {
try await viewModel.read()
}
} label: {
Text("Read")
Text("Read (using reader)")
}
Button {
readerSession = AsyncNFCTagReaderSession(pollingOption: .iso15693)
} label: {
Text("Read (using async stream)")
}
.disabled(readerSession != nil)
}
.iso15693TagReader(
isPresented: $isPresented,
Expand All @@ -46,6 +54,39 @@ struct NFCISO15693TagReaderExampleView: View {
return .success
}
)
.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 .iso15693(let iso15693Tag) = tag else {
throw NFCReaderError(.readerErrorInvalidParameter)
}
try await readerSession.connect(to: tag)
readerSession.alertMessage = "\(iso15693Tag.identifier as NSData)"
readerSession.stop()
} catch {
readerSession.stop(errorMessage: error.localizedDescription)
}
case .sessionCreationFailed(let reason):
print(reason)
case .sessionInvalidated(let reason):
print(reason)
}
}
}
.navigationTitle("ISO 15693-compatible")
}
}
Expand Down
43 changes: 42 additions & 1 deletion Examples/TRETNFCKitExample/NFCISO7816TagReaderExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//

import SwiftUI
import TRETNFCKit_Async
import TRETNFCKit_ISO7816

struct NFCISO7816TagReaderExampleView: View {
@State private var isPresented = false
@ObservedObject var viewModel = ViewModel()
@State private var readerSession: AsyncNFCTagReaderSession?

var body: some View {
List {
Expand All @@ -24,8 +26,14 @@ struct NFCISO7816TagReaderExampleView: View {
try await viewModel.read()
}
} label: {
Text("Read")
Text("Read (using reader)")
}
Button {
readerSession = AsyncNFCTagReaderSession(pollingOption: .iso14443)
} label: {
Text("Read (using async stream)")
}
.disabled(readerSession != nil)
}
.iso7816TagReader(
isPresented: $isPresented,
Expand All @@ -46,6 +54,39 @@ struct NFCISO7816TagReaderExampleView: View {
return .success
}
)
.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 .iso7816(let iso7816Tag) = tag else {
throw NFCReaderError(.readerErrorInvalidParameter)
}
try await readerSession.connect(to: tag)
readerSession.alertMessage = "\(iso7816Tag.identifier as NSData)"
readerSession.stop()
} catch {
readerSession.stop(errorMessage: error.localizedDescription)
}
case .sessionCreationFailed(let reason):
print(reason)
case .sessionInvalidated(let reason):
print(reason)
}
}
}
.navigationTitle("ISO 7816-compatible")
}
}
Expand Down
43 changes: 42 additions & 1 deletion Examples/TRETNFCKitExample/NFCMiFareTagReaderExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//

import SwiftUI
import TRETNFCKit_Async
import TRETNFCKit_MiFare

struct NFCMiFareTagReaderExampleView: View {
@State private var isPresented = false
@ObservedObject var viewModel = ViewModel()
@State private var readerSession: AsyncNFCTagReaderSession?

var body: some View {
List {
Expand All @@ -24,8 +26,14 @@ struct NFCMiFareTagReaderExampleView: View {
try await viewModel.read()
}
} label: {
Text("Read")
Text("Read (using reader)")
}
Button {
readerSession = AsyncNFCTagReaderSession(pollingOption: .iso14443)
} label: {
Text("Read (using async stream)")
}
.disabled(readerSession != nil)
}
.miFareTagReader(
isPresented: $isPresented,
Expand All @@ -46,6 +54,39 @@ struct NFCMiFareTagReaderExampleView: View {
return .success
}
)
.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 .miFare(let miFareTag) = tag else {
throw NFCReaderError(.readerErrorInvalidParameter)
}
try await readerSession.connect(to: tag)
readerSession.alertMessage = "\(miFareTag.identifier as NSData)"
readerSession.stop()
} catch {
readerSession.stop(errorMessage: error.localizedDescription)
}
case .sessionCreationFailed(let reason):
print(reason)
case .sessionInvalidated(let reason):
print(reason)
}
}
}
.navigationTitle("MiFare")
}
}
Expand Down
35 changes: 33 additions & 2 deletions Examples/TRETNFCKitExample/NFCNDEFMessageReaderExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@
//

import SwiftUI
import TRETNFCKit_Async
import TRETNFCKit_NDEFMessage

struct NFCNDEFMessageReaderExampleView: View {
@State private var isPresented = false
@ObservedObject var viewModel = ViewModel()
@State private var readerSession: AsyncNFCNDEFMessageReaderSession?

var body: some View {
List {
Button {
isPresented = true
} label: {
Text("Read (using view modifier)")
Text("Read (using reader view modifier)")
}
Button {
Task {
try await viewModel.read()
}
} label: {
Text("Read")
Text("Read (using reader)")
}
Button {
readerSession = AsyncNFCNDEFMessageReaderSession(invalidateAfterFirstRead: false)
} label: {
Text("Read (using async stream)")
}
.disabled(readerSession != nil)
}
.nfcNDEFMessageReader(
isPresented: $isPresented,
Expand All @@ -45,6 +53,29 @@ struct NFCNDEFMessageReaderExampleView: View {
return .success(alertMessage: "Done!")
}
)
.task(id: readerSession == nil) {
defer { readerSession = nil }
guard let readerSession else { return }
guard AsyncNFCNDEFMessageReaderSession.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 messages):
print(messages)
readerSession.alertMessage = "Done!"
readerSession.stop()
case .sessionInvalidated(let reason):
print(reason)
}
}
}
.navigationTitle("NDEF Messages")
}
}
Expand Down
Loading
Loading