import 'dart:async'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:qrscanner/services/scan_launch_service.dart'; typedef HardwareScanCallback = void Function(String rawValue); /// Integración con el escáner físico Honeywell vía Intent API nativa (EDA51). class HardwareScannerService { HardwareScannerService._(); static final HardwareScannerService instance = HardwareScannerService._(); final _scanLaunch = ScanLaunchService.instance; HardwareScanCallback? _onScan; void Function(Exception error)? _onError; StreamSubscription? _scanSubscription; bool _active = false; bool get isActive => _active; static Future isAvailable() => instance._scanLaunch.isSupported(); Future activate({ required HardwareScanCallback onScan, void Function(Exception error)? onError, }) async { if (kIsWeb || !Platform.isAndroid) return false; _onScan = onScan; _onError = onError; await _scanLaunch.claimScanner(); await _scanSubscription?.cancel(); _scanSubscription = _scanLaunch.scanStream.listen( (code) => _onScan?.call(code), onError: (Object error) => _onError?.call(Exception(error.toString())), ); _active = true; return _scanLaunch.isSupported(); } Future pause() async { _active = false; } Future deactivate() async { await _scanSubscription?.cancel(); _scanSubscription = null; _onScan = null; _onError = null; _active = false; } Future ensureStopped() async { await deactivate(); await _scanLaunch.releaseScanner(); } }