aclec / expo-zebra-scanner

14 stars 6 forks source link

Scanner do not showing data. #5

Closed creen-aps closed 1 year ago

creen-aps commented 1 year ago

Scanner do not showing data. It is probably something with DataWage. My settings in Profile0.

Screenshot_20230919-161150~2 Screenshot_20230919-161127~2

My eas react native component .

import React, { useState, useEffect } from 'react'
import { View, Text, Button } from 'react-native'
import * as ExpoZebraScanner from 'expo-zebra-scanner'

type ScanEvent = {
  scanData?: string
}

const App: React.FC = () => {
  const [scanData, setScanData] = useState<string[]>([])
  const [isScanning, setIsScanning] = useState<boolean>(false)
  const [scanStatus, setScanStatus] = useState<string | null>(null)

  useEffect(() => {
    const listener = ExpoZebraScanner.addListener((event: ScanEvent) => {
      console.log('Received event from scanner:', event)

      const newScanData = event.scanData
      if (newScanData) {
        setScanStatus('success')
        setScanData((prevData) => [newScanData, ...prevData])
      } else {
        setScanStatus('fail')
      }
    })

    return () => {
      ExpoZebraScanner.removeListener(listener)
    }
  }, [])

  const handleToggleScan = () => {
    if (isScanning) {
      // If currently scanning, stop the scanner
      console.log('isScanning', isScanning)
      setIsScanning(false)
    } else {
      // If not scanning, start the scanner
      setIsScanning(true)
      setScanStatus(null)
      console.log('isScanning', isScanning)
    }
  }
  console.log('scanData', scanData)
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Zebra Barcode Scanner</Text>
      {isScanning ? <Text>Scanning...</Text> : <Text>Scanner is inactive</Text>}
      {scanStatus && <Text>Status: {scanStatus}</Text>}
      <Button
        title={isScanning ? 'Stop Scanning' : 'Start Scanning'}
        onPress={handleToggleScan}
      />
      {scanData.slice(0, 5).map((data, index) => (
        <Text key={index}>Scanned Data: {data}</Text>
      ))}
    </View>
  )
}

export default App
aclec commented 1 year ago

Hi, disable Profile0 and create a new one for the app (and enable the app inside). Let Intent Category empty.

And you haven't call "ExpoZebraScanner.startScan();" so the scan can't start.

Which SDK of Expo are you using ?

creen-aps commented 1 year ago

"expo": "~49.0.8",

aclec commented 1 year ago

Currently, the problmen is that you not call "ExpoZebraScanner.startScan();" And call "ExpoZebraScanner.stopScan();" in the return function.

creen-aps commented 1 year ago

That was problem.