@amrshbib/react-native-barcode-laser
v0.1.1
Published
React Native bridge for Android handheld laser/infrared barcode scanners (Sunmi L2s Pro, L3, Zebra, Honeywell, Chainway, Urovo, Generic). Broadcast Intent + Keyboard Wedge support, no camera.
Downloads
242
Maintainers
Readme
React Native Barcode Laser
A high-performance React Native bridge for Android handheld laser/infrared barcode scanners. This package integrates directly with physical scan engines (not cameras) for enterprise PDA devices like Sunmi, Zebra, Honeywell, Chainway, and Urovo.
✨ Features
- 🚀 Hardware Native: Zero-latency capture via native Android Broadcast Intents.
- 📡 OEM Support: Pre-configured for Sunmi, Zebra (DataWedge), Honeywell, Newland, Chainway, and more.
- 🏗️ Dual Mode: Supports both Broadcast Intent (API) and Keyboard Wedge modes.
- 🎣 Power Hook:
useBarcodeLaserhook with state management and promise-based one-shot scanning. - 🔔 Feedback Control: Native support for hardware beeps and vibrations.
- 🛠️ Developer Friendly: Inject fake scans for testing, debounce, length filtering, and device info.
- 📝 TypeScript First: Complete type definitions for a better development experience.
📦 Installation
npm install @amrshbib/react-native-barcode-laser
# or
yarn add @amrshbib/react-native-barcode-laserAndroid Configuration
Android autolinking handles the setup. After installation, rebuild your project:
cd android && ./gradlew clean && cd ..
npx react-native run-androidNote: This package is Android-only as it targets handheld hardware scanners. iOS calls will return safe defaults (e.g.,
isAvailable() -> false).
🚀 Quick Start
1. Using the Hook (Recommended)
import { useBarcodeLaser } from "@amrshbib/react-native-barcode-laser";
const ScannerScreen = () => {
const { lastScan, started, available, scan } = useBarcodeLaser({
autoStart: true,
onScan: (event) => {
console.log("Scanned:", event.data);
}
});
// Example of capturing a single scan programmatically
const scanOne = async () => {
const result = await scan();
alert(`Captured: ${result.data}`);
};
if (available === false) return <Text>Hardware scanner not found</Text>;
return (
<View>
<Text>Status: {started ? "Active" : "Stopped"}</Text>
<Text>Last Barcode: {lastScan?.data}</Text>
<Button title="One-shot Scan" onPress={scanOne} />
</View>
);
};2. Imperative API
import BarcodeLaser from "@amrshbib/react-native-barcode-laser";
// Start the listener
const initScanner = async () => {
await BarcodeLaser.configure({
mode: 'broadcast',
beepOnSuccess: true
});
await BarcodeLaser.start();
const sub = BarcodeLaser.addListener((event) => {
console.log(`Scanned: ${event.data} via ${event.source}`);
});
};📖 API Reference
BarcodeLaser.configure(options)
Sets global configuration for the scanner hardware.
| Property | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| mode | string | 'broadcast' | 'broadcast', 'wedge', or 'auto' |
| beepOnSuccess | boolean | true | Play system beep on successful scan |
| vibrateOnSuccess| boolean | false | Vibrate on successful scan |
| vibrateDurationMs | number | 60 | Duration of the success vibration |
| debounceMs | number | 50 | Prevents rapid-fire duplicate scans |
| minLength | number | 1 | Ignore barcodes shorter than this |
| maxLength | number | 0 | Ignore barcodes longer than this (0 = no limit) |
| stripWhitespace | boolean | true | Trims results automatically |
| actions | string[] | (Defaults) | Custom intent actions to listen for |
| extras | string[] | (Defaults) | Custom intent extra keys for data |
BarcodeLaser.start() / stop()
Enables or disables the hardware listener. Returns Promise<boolean>.
BarcodeLaser.trigger() / stopTrigger()
Programmatically toggles the physical laser beam (on supported hardware).
BarcodeLaser.beep() / vibrate(ms)
Manually trigger the handheld's feedback systems.
BarcodeLaser.injectScan(data, type)
Inject a mock scan event into the listener. Great for development on emulators.
BarcodeLaser.getDeviceInfo()
Returns Promise<{ manufacturer: string, model: string, isSunmi: boolean, ... }> with detailed OEM info.
🎣 useBarcodeLaser Hook
Exposes the full power of the scanner as a React state.
| Return Key | Type | Description |
| :--- | :--- | :--- |
| available | boolean \| null | null while detecting, then true if hardware found |
| started | boolean | Current status of the listener |
| lastScan | ScanEvent | The most recently captured barcode object |
| error | ScanError | Last error event |
| scan() | () => Promise | Triggers laser and resolves with the next scan |
| trigger() | Function | Fire the laser manually |
| beep() | Function | Play system beep |
🔧 Platform Setup & OEM Config
Sunmi Devices (L2s Pro, L3, etc.)
- Open Scanner Settings on the device.
- Output Mode: Output via API (or Broadcast).
- Intent Action:
com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED. - Intent Extra:
data.
Zebra (DataWedge)
- Open DataWedge app.
- Select your profile (e.g.,
Profile0). - Intent Output: Enabled.
- Intent Action:
com.symbol.datawedge.api.RESULT_ACTION. - Intent Delivery: Broadcast Intent.
🤝 Contributing & Support
If you have a specific handheld device that requires a new default intent action, please submit a PR or open an issue!
📄 License
The MIT License - Copyright (c) 2024 Amr Shbib
