From aa541253bf6ead3c7490b98f1a76dcc04f491210 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 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Components/QrScanner/QrScannerPlugin.tsx b/src/Components/QrScanner/QrScannerPlugin.tsx index 3c4cf56..44808fb 100644 --- a/src/Components/QrScanner/QrScannerPlugin.tsx +++ b/src/Components/QrScanner/QrScannerPlugin.tsx @@ -54,6 +54,18 @@ export default function QrScannerPlugin({ const html5CustomScanner: MutableRefObject = useRef(null); const [canUseCamera, setCanUseCamera] = useState(true); + async function disableTorch(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 +98,7 @@ export default function QrScannerPlugin({ return () => { const stopQrScanner = async () => { if (html5CustomScanner.current?.isScanning) { + disableTorch(html5CustomScanner); await html5CustomScanner.current.stop(); } html5CustomScanner.current?.clear();