From f4887c39319eb09f279d7c7d7b80b2afcd1de282 Mon Sep 17 00:00:00 2001 From: Dominic H Date: Wed, 31 Jul 2024 13:15:15 +0100 Subject: [PATCH] Turn off the torch when navigating away from the scan page --- src/Components/QrScanner/QrScannerPlugin.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Components/QrScanner/QrScannerPlugin.tsx b/src/Components/QrScanner/QrScannerPlugin.tsx index 3c4cf56..eef03a9 100644 --- a/src/Components/QrScanner/QrScannerPlugin.tsx +++ b/src/Components/QrScanner/QrScannerPlugin.tsx @@ -54,6 +54,19 @@ export default function QrScannerPlugin({ const html5CustomScanner: MutableRefObject = useRef(null); const [canUseCamera, setCanUseCamera] = useState(true); + // Turn off the torch (if it is on) when navigating away from the scan page + async function switchOffTorch(html5CustomScanner: MutableRefObject) { + try { + const track = html5CustomScanner?.current?.getRunningTrackCameraCapabilities(); + console.log('torch', track?.torchFeature().value()); + if (track && track.torchFeature().value()) { + await track.torchFeature().apply(false); + } + } catch (error) { + console.warn('Failed to disable torch:', error); + } + } + useEffect(() => { const showQRCode = async () => { const hasCamPerm: boolean = await checkCameraPermissions(); @@ -86,6 +99,7 @@ export default function QrScannerPlugin({ return () => { const stopQrScanner = async () => { if (html5CustomScanner.current?.isScanning) { + switchOffTorch(html5CustomScanner); await html5CustomScanner.current.stop(); } html5CustomScanner.current?.clear();