diff --git a/ios/Classes/SwiftFlutterNfcKitPlugin.swift b/ios/Classes/SwiftFlutterNfcKitPlugin.swift index 50b6f6f..cb7f73e 100644 --- a/ios/Classes/SwiftFlutterNfcKitPlugin.swift +++ b/ios/Classes/SwiftFlutterNfcKitPlugin.swift @@ -50,6 +50,13 @@ public class SwiftFlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSess } else { result("not_supported") } + } else if call.method == "restartPolling" { + if let session = session { + self.result = result + session.restartPolling() + } else { + result(FlutterError(code: "404", message: "No active session", details: nil)) + } } else if call.method == "poll" { if session != nil { result(FlutterError(code: "406", message: "Cannot invoke poll in a active session", details: nil)) @@ -508,8 +515,8 @@ public class SwiftFlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSess case let .feliCa(tag): result["type"] = "iso18092" result["standard"] = "ISO 18092 (FeliCa)" + result["id"] = tag.currentIDm.hexEncodedString() result["systemCode"] = tag.currentSystemCode.hexEncodedString() - result["manufacturer"] = tag.currentIDm.hexEncodedString() case let .iso15693(tag): result["type"] = "iso15693" result["standard"] = "ISO 15693" @@ -518,6 +525,7 @@ public class SwiftFlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSess default: result["type"] = "unknown" result["standard"] = "unknown" + result["id"] = "unknown" } session.connect(to: firstTag) { (error: Error?) in @@ -555,10 +563,27 @@ public class SwiftFlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSess result["ndefCapacity"] = capacity } // ignore error, just return with ndef disabled - let jsonData = try! JSONSerialization.data(withJSONObject: result) - let jsonString = String(data: jsonData, encoding: .utf8) - self.result?(jsonString) - self.result = nil + switch self.tag { + case let .feliCa(tag): + tag.polling(systemCode: tag.currentSystemCode, requestCode: .noRequest, timeSlot: .max16) { (pmm: Data, _: Data, error: Error?) in + if let error = error { + self.result?(FlutterError(code: "500", message: "Communication error on connect", details: error.localizedDescription)) + self.result = nil + } else { + result["manufacturer"] = pmm.hexEncodedString() + + let jsonData = try! JSONSerialization.data(withJSONObject: result) + let jsonString = String(data: jsonData, encoding: .utf8) + self.result?(jsonString) + self.result = nil + } + } + default: + let jsonData = try! JSONSerialization.data(withJSONObject: result) + let jsonString = String(data: jsonData, encoding: .utf8) + self.result?(jsonString) + self.result = nil + } } } else { let jsonData = try! JSONSerialization.data(withJSONObject: result) diff --git a/lib/flutter_nfc_kit.dart b/lib/flutter_nfc_kit.dart index 28ba264..a4c98f9 100644 --- a/lib/flutter_nfc_kit.dart +++ b/lib/flutter_nfc_kit.dart @@ -69,7 +69,7 @@ class NFCTag { /// The standard that the tag complies with (can be `unknown`) final String standard; - /// Tag ID + /// Tag ID (can be `unknown`) final String id; /// ATQA (Type A only, Android only) @@ -343,6 +343,13 @@ class FlutterNfcKit { return NFCTag.fromJson(jsonDecode(data)); } + /// Works only on iOS + /// Calls NFCTagReaderSession.restartPolling() + /// Call this if you have received "Tag connection lost" exception + /// This will allow to reconnect to tag without closing system popup + static Future iosRestartPolling() async => + await _channel.invokeMethod("restartPolling"); + /// Transceive data with the card / tag in the format of APDU (iso7816) or raw commands (other technologies). /// The [capdu] can be either of type Uint8List or hex string. /// Return value will be in the same type of [capdu].