Unitech Scanner for EA510
*Note: This package is still under beta version*
yarn add react-native-unitech-scanner
or
npm install react-native-unitech-scanner
import ScanManager from 'react-native-unitech-scanner';
// ...
// Called when barcode is scanned
const onScan = (data) => {
// Handle the barcode data
const { barcode, length, type } = data;
console.log('barcode', barcode);
console.log('length', length);
console.log('type', type);
};
useEffect(() => {
// Subscribe to barcode scan event
const listener = ScanManager.addEventListener(
ScanManager.constants.events.SCANNER_BARCODE,
onScan
);
// Make sure to unsubscribe from event when finished
return () => {
listener.remove();
// or
// ScanManager.removeEventListener(listener.key);
};
}, []);
Method | Return Type | iOS | Android |
---|---|---|---|
addEventListener() | EmmitterSubscription |
❌ | ✅ |
removeEventListener() | void |
❌ | ✅ |
getScannerState() | Promise<boolean> |
❌ | ✅ |
openScanner() | Promise<boolean> |
❌ | ✅ |
closeScanner() | Promise<boolean> |
❌ | ✅ |
startDecode() | Promise<boolean> |
❌ | ✅ |
stopDecode() | Promise<boolean> |
❌ | ✅ |
getTriggerLockState() | Promise<boolean> |
❌ | ✅ |
lockTrigger() | Promise<boolean> |
❌ | ✅ |
unlockTrigger() | Promise<boolean> |
❌ | ✅ |
Attaches a listener to an event.
ScanManager.addEventListener(
ScanManager.constants.events.SCANNER_BARCODE,
(data) => {
console.log('eventData', data);
}
);
Removes the event listener. Do this in componentWillUnmount to prevent memory leaks
ScanManager.removeEventListener(subscriptionId);
Get the scanner power states. Returns true if the scanner power on
ScanManager.getScannerState().then((response) => {
console.log(response);
});
Turn on the power for the bar code reader. Returns false if failed, true if successful
ScanManager.openScanner().then((response) => {
console.log(response);
});
Turn off the power for the bar code reader. Returns false if failed, true if successful
ScanManager.closeScanner().then((response) => {
console.log(response);
});
Call this method to start decoding. Returns true if the scanner and the trigger is already active
ScanManager.startDecode().then((response) => {
console.log(response);
});
This stops any data acquisition currently in progress. Returns true if stop successful
ScanManager.stopDecode().then((response) => {
console.log(response);
});
Get the scan trigger status. Returns true if the scan trigger is already active
ScanManager.getTriggerLockState().then((response) => {
console.log(response);
});
Set the scan trigger inactive (disable the scan button). Returns true if successful. Returns false if failed.
ScanManager.lockTrigger().then((response) => {
console.log(response);
});
Set the scan trigger active (enable the scan button). Returns true if successful. Returns false if failed.
ScanManager.unlockTrigger().then((response) => {
console.log(response);
});
If you are developing an application for a Unitech scanner device but do not have the physical device, you can mock a barcode scan using the following:
import { DeviceEventEmitter } from 'react-native';
//...
DeviceEventEmitter.emit('scanner-barcode', {
barcode: '0123456789',
length: 10,
type: 11,
});
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT