Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[barcode-scanner]call scan but never returned #2238

Open
wingsfly opened this issue Dec 26, 2024 · 1 comment
Open

[barcode-scanner]call scan but never returned #2238

wingsfly opened this issue Dec 26, 2024 · 1 comment
Labels
bug Something isn't working platform: android Android specific issues plugin: barcode-scanner

Comments

@wingsfly
Copy link

I followed the sample at https://v2.tauri.app/plugin/barcode-scanner/,but never got Scanned result after calling scan.
The camera icon showed on the top-right corner of the screen, and keeps while the app runs, but no any result returns.
Also I tried to set scan({ windowed: false, formats: [Format.QRCode] });, it shows the camera screen, and never response, no matter how i scan the qr code.
I'm using XiaoMi 13 with HyperOS, android version 14


The project using automatic install with npm run tauri add barcode-scanner, then the related code as follows:
src-tauri/Cargo.toml:

[dependencies]
...
[target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies]
tauri-plugin-barcode-scanner = "2"


src-tauri/src/lib.rs

pub fn run() {
    let mut app = tauri::Builder::default();
   ...
   
    #[cfg(any(target_os = "android", target_os = "ios"))]
    {
        app = app.plugin(tauri_plugin_barcode_scanner::init());
    }
   ...
}

src-tauri/capabilities/mobile.json

...
  "permissions": [
    "biometric:default",
    "barcode-scanner:allow-scan",
    "barcode-scanner:allow-cancel",
    "barcode-scanner:allow-check-permissions",
    "barcode-scanner:allow-request-permissions"
  ]

src/Features/Scanner.tsx

import { useEffect, useState } from "react";
import { platform } from "@tauri-apps/plugin-os";
import { scan, cancel, Format, checkPermissions, requestPermissions } from "@tauri-apps/plugin-barcode-scanner";
import "./Scanner.css";

function Scanner() {
  const [scanResult, setScanResult] = useState<string>("");
  const [currentPlatform, setCurrentPlatform] = useState<string>("");

  useEffect(() => {
    const fetchPlatform = async () => {
      const platformName = await platform();
      setCurrentPlatform(platformName);
    };

    fetchPlatform();
  }, [currentPlatform]);

  const handleScan = async () => {
    //alert("Current Platform: " + currentPlatform);
    if (["android", "ios"].includes(currentPlatform)) {
      try {
        const permissions = await checkPermissions();
        if (permissions !== "granted") {
          const requestResult = await requestPermissions();
          if (requestResult !== "granted") {
            alert("Camera permission is required to scan QR codes.");
            return;
          }
        }

        const result = await scan({ windowed: true, formats: [Format.QRCode] });
        //await cancel();
        //alert("QR code scanning result: " + result.content);
        setScanResult(result.content);
      } catch (error) {
        alert("QR code scanning failed: " + error);
        console.error("QR code scanning failed:", error);
      }
    } else {
      alert("QR code scanning is only supported on Android and iOS platforms.");
    }
  };

  const handleCopy = () => {
    navigator.clipboard.writeText(scanResult);
    alert("Text copied to clipboard!");
  };

  return (
    <div className="scanner-container">
      <button onClick={handleScan}>Scan QR Code</button>
      <textarea
        value={scanResult}
        rows={5}
        readOnly
        className="scan-result"
      />
      <button onClick={handleCopy}>Copy Text</button>
    </div>
  );
}

export default Scanner;
@FabianLars FabianLars added bug Something isn't working platform: android Android specific issues plugin: barcode-scanner labels Dec 26, 2024
@xiaoshanyangcode
Copy link

I also encountered the same problem, my device is android 14。tauri-plugin-barcode-scanner = "2"。
The old version of andorid 10 works fine, but android 14 doesn't. The model of my problematic device is iqoo neo9, andorid 14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working platform: android Android specific issues plugin: barcode-scanner
Projects
None yet
Development

No branches or pull requests

3 participants