flutter-nfc-manager
flutter-nfc-manager copied to clipboard
Issue : callback injection fails at second NFC transceive in NFCV
Hi,
As mentionned in issue 25, when having particular callback implementation, a second transceive command in NfcV fails, independently from the command content.
This minimal code demonstrates the issue :
final Uint8List command = Uint8List.fromList([0x02, 0x2B]);
Future<void> launchNfcSequence1() async {
await NfcManager.instance.startSession(onDiscovered: (tag) async {
final nfcV = NfcV.from(tag);
await sequence(nfcV!);
});
}
Future<void> launchNfcSequence2(
Future<void> Function(NfcV nfcV) sequence) async {
await NfcManager.instance.startSession(onDiscovered: (tag) async {
final nfcV = NfcV.from(tag);
await sequence(nfcV!);
});
}
Future<void> launchNfcSequence3(
Future<void> Function(NfcV nfcV) sequence) async {
await NfcManager.instance.startSession(onDiscovered: (tag) async {
_onDiscovered(sequence, tag);
});
}
// Transceive command twice
Future<void> sequence(NfcV nfcV) async {
try {
final answer1 = await nfcV.transceive(data: command);
print(answer1.toString());
final answer2 = await nfcV.transceive(data: command);
print(answer2.toString());
} catch (e) {
print(e);
rethrow;
}
Future<void> _onDiscovered(
Future<void> Function(NfcV nfcV) sequence, NfcTag tag) async {
final nfcV = NfcV.from(tag);
await sequence(nfcV!);
}
}
This code implements 3 NFC implementations called launchSequence1-2-3.
- 1 : onDiscovered calls double commands directly after instantiating NfcVTag => Works
- 2 : a callback is passed to onDiscovered after instanting NfcVTag => Works
- 3 : onDiscovered calls another function to instantiate NfcVTag with a callback passed to it => Fails
It seems that instantiating NfcVTag.from(tag) outside the onDiscovered callback is what causes the issue. I don't know the library enough to explain why the first command works and not the second.
Resolving the issue would make this library better suited for dependency injection.