62 lines
1.6 KiB
Dart
62 lines
1.6 KiB
Dart
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<String>? _scanSubscription;
|
|
bool _active = false;
|
|
|
|
bool get isActive => _active;
|
|
|
|
static Future<bool> isAvailable() => instance._scanLaunch.isSupported();
|
|
|
|
Future<bool> 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<void> pause() async {
|
|
_active = false;
|
|
}
|
|
|
|
Future<void> deactivate() async {
|
|
await _scanSubscription?.cancel();
|
|
_scanSubscription = null;
|
|
_onScan = null;
|
|
_onError = null;
|
|
_active = false;
|
|
}
|
|
|
|
Future<void> ensureStopped() async {
|
|
await deactivate();
|
|
await _scanLaunch.releaseScanner();
|
|
}
|
|
} |