nfc manager improvements

main_production_upgrade_ios
Sikander Saleem 17 hours ago
parent e4b84bb3e0
commit bdaae11c36

@ -20,7 +20,5 @@
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1.0</string> <string>1.0</string>
<key>MinimumOSVersion</key>
<string>12.0</string>
</dict> </dict>
</plist> </plist>

@ -40,7 +40,7 @@ post_install do |installer|
installer.pods_project.targets.each do |target| installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target) flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config| target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0' config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
end end
target.build_configurations.each do |config| target.build_configurations.each do |config|
flutter_additional_ios_build_settings(target) flutter_additional_ios_build_settings(target)
@ -53,7 +53,7 @@ post_install do |installer|
installer.generated_projects.each do |project| installer.generated_projects.each do |project|
project.targets.each do |target| project.targets.each do |target|
target.build_configurations.each do |config| target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0' config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)', '$(inherited)',
## dart: PermissionGroup.microphone ## dart: PermissionGroup.microphone

@ -375,7 +375,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0; IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO; MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos; SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos; SUPPORTED_PLATFORMS = iphoneos;
@ -433,8 +433,6 @@
"-framework", "-framework",
"\"GoogleUtilities\"", "\"GoogleUtilities\"",
"-framework", "-framework",
"\"MTBBarcodeScanner\"",
"-framework",
"\"MediaPlayer\"", "\"MediaPlayer\"",
"-framework", "-framework",
"\"QuartzCore\"", "\"QuartzCore\"",
@ -443,10 +441,6 @@
"-framework", "-framework",
"\"SystemConfiguration\"", "\"SystemConfiguration\"",
"-framework", "-framework",
"\"Toast\"",
"-framework",
"\"Try\"",
"-framework",
"\"UIKit\"", "\"UIKit\"",
"-framework", "-framework",
"\"audioplayers_darwin\"", "\"audioplayers_darwin\"",
@ -471,8 +465,6 @@
"-framework", "-framework",
"\"speech_to_text\"", "\"speech_to_text\"",
"-framework", "-framework",
"\"sqflite\"",
"-framework",
"\"url_launcher_ios\"", "\"url_launcher_ios\"",
"-weak_framework", "-weak_framework",
"\"UserNotifications\"", "\"UserNotifications\"",
@ -538,7 +530,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0; IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = YES; MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES; ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos; SDKROOT = iphoneos;
@ -591,7 +583,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0; IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO; MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos; SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos; SUPPORTED_PLATFORMS = iphoneos;
@ -651,8 +643,6 @@
"-framework", "-framework",
"\"GoogleUtilities\"", "\"GoogleUtilities\"",
"-framework", "-framework",
"\"MTBBarcodeScanner\"",
"-framework",
"\"MediaPlayer\"", "\"MediaPlayer\"",
"-framework", "-framework",
"\"QuartzCore\"", "\"QuartzCore\"",
@ -661,10 +651,6 @@
"-framework", "-framework",
"\"SystemConfiguration\"", "\"SystemConfiguration\"",
"-framework", "-framework",
"\"Toast\"",
"-framework",
"\"Try\"",
"-framework",
"\"UIKit\"", "\"UIKit\"",
"-framework", "-framework",
"\"audioplayers_darwin\"", "\"audioplayers_darwin\"",
@ -689,8 +675,6 @@
"-framework", "-framework",
"\"speech_to_text\"", "\"speech_to_text\"",
"-framework", "-framework",
"\"sqflite\"",
"-framework",
"\"url_launcher_ios\"", "\"url_launcher_ios\"",
"-weak_framework", "-weak_framework",
"\"UserNotifications\"", "\"UserNotifications\"",
@ -755,8 +739,6 @@
"-framework", "-framework",
"\"GoogleUtilities\"", "\"GoogleUtilities\"",
"-framework", "-framework",
"\"MTBBarcodeScanner\"",
"-framework",
"\"MediaPlayer\"", "\"MediaPlayer\"",
"-framework", "-framework",
"\"QuartzCore\"", "\"QuartzCore\"",
@ -765,10 +747,6 @@
"-framework", "-framework",
"\"SystemConfiguration\"", "\"SystemConfiguration\"",
"-framework", "-framework",
"\"Toast\"",
"-framework",
"\"Try\"",
"-framework",
"\"UIKit\"", "\"UIKit\"",
"-framework", "-framework",
"\"audioplayers_darwin\"", "\"audioplayers_darwin\"",
@ -793,8 +771,6 @@
"-framework", "-framework",
"\"speech_to_text\"", "\"speech_to_text\"",
"-framework", "-framework",
"\"sqflite\"",
"-framework",
"\"url_launcher_ios\"", "\"url_launcher_ios\"",
"-weak_framework", "-weak_framework",
"\"UserNotifications\"", "\"UserNotifications\"",

@ -26,6 +26,7 @@
buildConfiguration = "Debug" buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
customLLDBInitFile = "$(SRCROOT)/Flutter/ephemeral/flutter_lldbinit"
shouldUseLaunchSchemeArgsEnv = "YES"> shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion> <MacroExpansion>
<BuildableReference <BuildableReference
@ -43,11 +44,13 @@
buildConfiguration = "Debug" buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
customLLDBInitFile = "$(SRCROOT)/Flutter/ephemeral/flutter_lldbinit"
launchStyle = "0" launchStyle = "0"
useCustomWorkingDirectory = "NO" useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO" ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES" debugDocumentVersioning = "YES"
debugServiceExtension = "internal" debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES"> allowLocationSimulation = "YES">
<BuildableProductRunnable <BuildableProductRunnable
runnableDebuggingMode = "0"> runnableDebuggingMode = "0">

@ -20,6 +20,8 @@ import 'package:google_api_availability/google_api_availability.dart';
// import 'package:mohem_flutter_app/widgets/dialogs/confirm_dialog.dart'; // import 'package:mohem_flutter_app/widgets/dialogs/confirm_dialog.dart';
// import 'package:mohem_flutter_app/widgets/loading_dialog.dart'; // import 'package:mohem_flutter_app/widgets/loading_dialog.dart';
import 'package:nfc_manager/nfc_manager.dart'; import 'package:nfc_manager/nfc_manager.dart';
import 'package:nfc_manager/nfc_manager_android.dart' show NfcAAndroid, NfcBAndroid;
import 'package:nfc_manager/nfc_manager_ios.dart' show MiFareIos, Iso15693Ios;
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:test_sa/new_views/common_widgets/app_lazy_loading.dart'; import 'package:test_sa/new_views/common_widgets/app_lazy_loading.dart';
import 'package:test_sa/views/widgets/dialogs/confirm_dialog.dart'; import 'package:test_sa/views/widgets/dialogs/confirm_dialog.dart';
@ -416,29 +418,41 @@ class Utils {
// return selectedDate; // return selectedDate;
// } // }
static void readNFc({required Function(String) onRead}) { static void readNFc({required Function(String) onRead}) async {
// Stop any existing session before starting a new one
try {
await NfcManager.instance.stopSession();
} catch (e) {
print('No active session to stop: $e');
}
NfcManager.instance.startSession( NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async { onDiscovered: (NfcTag tag) async {
String identifier = ''; String identifier = '';
try { try {
final dynamic tagDynamic = tag;
final Map<String, dynamic> tagData = Map<String, dynamic>.from(tagDynamic.data as Map);
if (Platform.isAndroid) { if (Platform.isAndroid) {
if (tagData.containsKey('nfca')) { // Try NfcA first (most common)
final nfcaData = tagData['nfca'] as Map<dynamic, dynamic>; final nfcA = NfcAAndroid.from(tag);
if (nfcaData.containsKey('identifier')) { if (nfcA != null) {
final List<int> idBytes = List<int>.from(nfcaData['identifier'] as List); identifier = nfcA.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); } else {
// Fallback to NfcB
final nfcB = NfcBAndroid.from(tag);
if (nfcB != null) {
identifier = nfcB.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
} else { } else {
if (tagData.containsKey('mifare')) { // For iOS, try MiFare first
final mifareData = tagData['mifare'] as Map<dynamic, dynamic>; final mifare = MiFareIos.from(tag);
if (mifareData.containsKey('identifier')) { if (mifare != null) {
final List<int> idBytes = List<int>.from(mifareData['identifier'] as List); identifier = mifare.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); } else {
// Fallback to Iso15693 for iOS
final iso15693 = Iso15693Ios.from(tag);
if (iso15693 != null) {
identifier = iso15693.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
} }
@ -446,12 +460,20 @@ class Utils {
print('Error reading NFC: $e'); print('Error reading NFC: $e');
} }
NfcManager.instance.stopSession(); try {
await NfcManager.instance.stopSession();
} catch (e) {
print('Error stopping NFC session: $e');
}
onRead(identifier); onRead(identifier);
}, },
pollingOptions: {NfcPollingOption.iso14443}, pollingOptions: {NfcPollingOption.iso14443},
).catchError((err) { ).catchError((err) {
print(err); print('NFC session error: $err');
// Stop session on error
NfcManager.instance.stopSession().catchError((e) {
print('Error stopping session after error: $e');
});
}); });
} }

@ -3,6 +3,8 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:nfc_manager/nfc_manager.dart'; import 'package:nfc_manager/nfc_manager.dart';
import 'package:nfc_manager/nfc_manager_android.dart' show NfcAAndroid, NfcBAndroid;
import 'package:nfc_manager/nfc_manager_ios.dart' show MiFareIos, Iso15693Ios;
import 'package:test_sa/dashboard_latest/dashboard_view.dart'; import 'package:test_sa/dashboard_latest/dashboard_view.dart';
import 'package:test_sa/extensions/context_extension.dart'; import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/new_views/app_style/app_color.dart'; import 'package:test_sa/new_views/app_style/app_color.dart';
@ -42,29 +44,53 @@ class _NfcLayoutState extends State<NfcLayout> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_startNfcSession();
}
@override
void dispose() {
// Ensure session is stopped when widget is disposed
NfcManager.instance.stopSession().catchError((e) {
print('Error stopping NFC session in dispose: $e');
});
super.dispose();
}
void _startNfcSession() async {
// Stop any existing session before starting a new one
try {
await NfcManager.instance.stopSession();
} catch (e) {
print('No active session to stop: $e');
}
NfcManager.instance.startSession( NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async { onDiscovered: (NfcTag tag) async {
String? identifier; String? identifier;
try { try {
final dynamic tagDynamic = tag;
final Map<String, dynamic> tagData = Map<String, dynamic>.from(tagDynamic.data as Map);
if (Platform.isAndroid) { if (Platform.isAndroid) {
if (tagData.containsKey('nfca')) { // Try NfcA first (most common)
final nfcaData = tagData['nfca'] as Map<dynamic, dynamic>; final nfcA = NfcAAndroid.from(tag);
if (nfcaData.containsKey('identifier')) { if (nfcA != null) {
final List<int> idBytes = List<int>.from(nfcaData['identifier'] as List); identifier = nfcA.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); } else {
// Fallback to NfcB
final nfcB = NfcBAndroid.from(tag);
if (nfcB != null) {
identifier = nfcB.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
} else { } else {
if (tagData.containsKey('mifare')) { // For iOS, try MiFare first
final mifareData = tagData['mifare'] as Map<dynamic, dynamic>; final mifare = MiFareIos.from(tag);
if (mifareData.containsKey('identifier')) { if (mifare != null) {
final List<int> idBytes = List<int>.from(mifareData['identifier'] as List); identifier = mifare.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); } else {
// Fallback to Iso15693 for iOS
final iso15693 = Iso15693Ios.from(tag);
if (iso15693 != null) {
identifier = iso15693.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
} }
@ -74,20 +100,32 @@ class _NfcLayoutState extends State<NfcLayout> {
nfcId = identifier; nfcId = identifier;
if (!mounted) return;
setState(() { setState(() {
_reading = true; _reading = true;
mainWidget = doneNfc(); mainWidget = doneNfc();
}); });
Future.delayed(const Duration(seconds: 1), () { Future.delayed(const Duration(seconds: 1), () async {
NfcManager.instance.stopSession(); try {
Navigator.pop(context); await NfcManager.instance.stopSession();
widget.onNcfScan!(nfcId!); } catch (e) {
print('Error stopping session: $e');
}
if (mounted) {
Navigator.pop(context);
widget.onNcfScan!(nfcId!);
}
}); });
}, },
pollingOptions: {NfcPollingOption.iso14443}, pollingOptions: {NfcPollingOption.iso14443},
).catchError((err) { ).catchError((err) {
print(err); print('NFC session error: $err');
// Stop session on error
NfcManager.instance.stopSession().catchError((e) {
print('Error stopping session after error: $e');
});
}); });
} }
@ -144,9 +182,15 @@ class _NfcLayoutState extends State<NfcLayout> {
borderRadius: BorderRadius.circular(6), borderRadius: BorderRadius.circular(6),
), ),
child: TextButton( child: TextButton(
onPressed: () { onPressed: () async {
NfcManager.instance.stopSession(); try {
Navigator.pop(context); await NfcManager.instance.stopSession();
} catch (e) {
print('Error stopping NFC session: $e');
}
if (mounted) {
Navigator.pop(context);
}
}, },
// elevation: 0, // elevation: 0,
child: const Text("CANCEL"), child: const Text("CANCEL"),

@ -10,6 +10,8 @@ import 'package:google_api_availability/google_api_availability.dart';
import 'package:huawei_location/huawei_location.dart'; import 'package:huawei_location/huawei_location.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:nfc_manager/nfc_manager.dart'; import 'package:nfc_manager/nfc_manager.dart';
import 'package:nfc_manager/nfc_manager_android.dart' show NfcAAndroid, NfcBAndroid;
import 'package:nfc_manager/nfc_manager_ios.dart' show MiFareIos, Iso15693Ios;
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -299,6 +301,7 @@ class SwipeGeneralUtils {
if (Platform.isIOS) { if (Platform.isIOS) {
readNFc(onRead: (String nfcId) async { readNFc(onRead: (String nfcId) async {
print("nfcId:$nfcId");
await _processNfcAttendance(nfcId, latitude, longitude, context); await _processNfcAttendance(nfcId, latitude, longitude, context);
}); });
} else { } else {
@ -456,29 +459,41 @@ class SwipeGeneralUtils {
); );
} }
void readNFc({Function(String)? onRead}) { void readNFc({Function(String)? onRead}) async {
// Stop any existing session before starting a new one
try {
await NfcManager.instance.stopSession();
} catch (e) {
print('No active session to stop: $e');
}
NfcManager.instance.startSession( NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async { onDiscovered: (NfcTag tag) async {
String identifier = ''; String identifier = '';
try { try {
final dynamic tagDynamic = tag;
final Map<String, dynamic> tagData = Map<String, dynamic>.from(tagDynamic.data as Map);
if (Platform.isAndroid) { if (Platform.isAndroid) {
if (tagData.containsKey('nfca')) { // Try NfcA first (most common)
final nfcaData = tagData['nfca'] as Map<dynamic, dynamic>; final nfcA = NfcAAndroid.from(tag);
if (nfcaData.containsKey('identifier')) { if (nfcA != null) {
final List<int> idBytes = List<int>.from(nfcaData['identifier'] as List); identifier = nfcA.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); } else {
// Fallback to NfcB
final nfcB = NfcBAndroid.from(tag);
if (nfcB != null) {
identifier = nfcB.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
} else { } else {
if (tagData.containsKey('mifare')) { // For iOS, try MiFare first
final mifareData = tagData['mifare'] as Map<dynamic, dynamic>; final mifare = MiFareIos.from(tag);
if (mifareData.containsKey('identifier')) { if (mifare != null) {
final List<int> idBytes = List<int>.from(mifareData['identifier'] as List); identifier = mifare.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); } else {
// Fallback to Iso15693 for iOS
final iso15693 = Iso15693Ios.from(tag);
if (iso15693 != null) {
identifier = iso15693.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
} }
@ -486,12 +501,20 @@ class SwipeGeneralUtils {
print('Error reading NFC: $e'); print('Error reading NFC: $e');
} }
NfcManager.instance.stopSession(); try {
await NfcManager.instance.stopSession();
} catch (e) {
print('Error stopping NFC session: $e');
}
onRead!(identifier); onRead!(identifier);
}, },
pollingOptions: {NfcPollingOption.iso14443}, pollingOptions: {NfcPollingOption.iso14443},
).catchError((err) { ).catchError((err) {
print(err); print('NFC session error: $err');
// Stop session on error
NfcManager.instance.stopSession().catchError((e) {
print('Error stopping session after error: $e');
});
}); });
} }

@ -3,6 +3,8 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:nfc_manager/nfc_manager.dart'; import 'package:nfc_manager/nfc_manager.dart';
import 'package:nfc_manager/nfc_manager_android.dart' show NfcAAndroid, NfcBAndroid;
import 'package:nfc_manager/nfc_manager_ios.dart' show MiFareIos, Iso15693Ios;
void showNfcReader(BuildContext context, {required Function(String? nfcId) onNcfScan}) { void showNfcReader(BuildContext context, {required Function(String? nfcId) onNcfScan}) {
showModalBottomSheet( showModalBottomSheet(
@ -38,60 +40,55 @@ class _NfcLayoutState extends State<NfcLayout> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_startNfcSession();
}
@override
void dispose() {
// Ensure session is stopped when widget is disposed
NfcManager.instance.stopSession().catchError((e) {
print('Error stopping NFC session in dispose: $e');
});
super.dispose();
}
void _startNfcSession() async {
// Stop any existing session before starting a new one
try {
await NfcManager.instance.stopSession();
} catch (e) {
print('No active session to stop: $e');
}
NfcManager.instance.startSession( NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async { onDiscovered: (NfcTag tag) async {
String? identifier; String? identifier;
// Access tag identifier from the tag data
// In nfc_manager 4.1.1, access data via dynamic cast to bypass protected member
try { try {
final dynamic tagDynamic = tag;
final Map<String, dynamic> tagData = Map<String, dynamic>.from(tagDynamic.data as Map);
if (Platform.isAndroid) { if (Platform.isAndroid) {
// For Android, try NfcA technology first // Try NfcA first (most common)
if (tagData.containsKey('nfca')) { final nfcA = NfcAAndroid.from(tag);
final nfcaData = tagData['nfca'] as Map<dynamic, dynamic>; if (nfcA != null) {
if (nfcaData.containsKey('identifier')) { identifier = nfcA.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
final List<int> idBytes = List<int>.from(nfcaData['identifier'] as List); } else {
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); // Fallback to NfcB
} final nfcB = NfcBAndroid.from(tag);
} if (nfcB != null) {
// Fallback to other technologies if NfcA is not available identifier = nfcB.tag.id.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
if (identifier == null && tagData.containsKey('nfcb')) {
final nfcbData = tagData['nfcb'] as Map<dynamic, dynamic>;
if (nfcbData.containsKey('identifier')) {
final List<int> idBytes = List<int>.from(nfcbData['identifier'] as List);
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
} else { } else {
// For iOS, try MiFare technology // For iOS, try MiFare first
if (tagData.containsKey('mifare')) { final mifare = MiFareIos.from(tag);
final mifareData = tagData['mifare'] as Map<dynamic, dynamic>; if (mifare != null) {
if (mifareData.containsKey('identifier')) { identifier = mifare.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
final List<int> idBytes = List<int>.from(mifareData['identifier'] as List); } else {
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(''); // Fallback to Iso15693 for iOS
final iso15693 = Iso15693Ios.from(tag);
if (iso15693 != null) {
identifier = iso15693.identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
} }
} }
// Fallback to iso15693 for iOS
if (identifier == null && tagData.containsKey('iso15693')) {
final iso15693Data = tagData['iso15693'] as Map<dynamic, dynamic>;
if (iso15693Data.containsKey('identifier')) {
final List<int> idBytes = List<int>.from(iso15693Data['identifier'] as List);
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
}
}
}
// Universal fallback: try Ndef if available
if (identifier == null && tagData.containsKey('ndef')) {
final ndefData = tagData['ndef'] as Map<dynamic, dynamic>;
if (ndefData.containsKey('identifier')) {
final List<int> idBytes = List<int>.from(ndefData['identifier'] as List);
identifier = idBytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join('');
}
} }
} catch (e) { } catch (e) {
print('Error reading NFC tag: $e'); print('Error reading NFC tag: $e');
@ -99,20 +96,32 @@ class _NfcLayoutState extends State<NfcLayout> {
nfcId = identifier; nfcId = identifier;
if (!mounted) return;
setState(() { setState(() {
_reading = true; _reading = true;
mainWidget = doneNfc(); mainWidget = doneNfc();
}); });
Future.delayed(const Duration(seconds: 1), () { Future.delayed(const Duration(seconds: 1), () async {
NfcManager.instance.stopSession(); try {
Navigator.pop(context); await NfcManager.instance.stopSession();
widget.onNcfScan(nfcId); } catch (e) {
print('Error stopping session: $e');
}
if (mounted) {
Navigator.pop(context);
widget.onNcfScan(nfcId);
}
}); });
}, },
pollingOptions: {NfcPollingOption.iso14443}, pollingOptions: {NfcPollingOption.iso14443},
).catchError((err) { ).catchError((err) {
print(err); print('NFC session error: $err');
// Stop session on error
NfcManager.instance.stopSession().catchError((e) {
print('Error stopping session after error: $e');
});
}); });
} }
@ -166,9 +175,15 @@ class _NfcLayoutState extends State<NfcLayout> {
borderRadius: BorderRadius.circular(6), borderRadius: BorderRadius.circular(6),
), ),
child: TextButton( child: TextButton(
onPressed: () { onPressed: () async {
NfcManager.instance.stopSession(); try {
Navigator.pop(context); await NfcManager.instance.stopSession();
} catch (e) {
print('Error stopping NFC session: $e');
}
if (mounted) {
Navigator.pop(context);
}
}, },
// elevation: 0, // elevation: 0,
child: Text("CANCEL"), child: Text("CANCEL"),

@ -1030,6 +1030,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.0" version: "2.0.0"
mobile_scanner:
dependency: "direct main"
description:
name: mobile_scanner
sha256: c92c26bf2231695b6d3477c8dcf435f51e28f87b1745966b1fe4c47a286171ce
url: "https://pub.dev"
source: hosted
version: "7.2.0"
native_toolchain_c: native_toolchain_c:
dependency: transitive dependency: transitive
description: description:

Loading…
Cancel
Save