Skip to content

Commit

Permalink
Turn off the torch when navigating away from the scan page
Browse files Browse the repository at this point in the history
  • Loading branch information
GovernmentPlates committed Jul 31, 2024
1 parent b67fa72 commit f4887c3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Components/QrScanner/QrScannerPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ export default function QrScannerPlugin({
const html5CustomScanner: MutableRefObject<Html5Qrcode | null> = 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<Html5Qrcode | null>) {
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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f4887c3

Please sign in to comment.