Skip to content

philpettican/react-native-unitech-scanner

Repository files navigation

react-native-unitech-scanner

Unitech Scanner for EA510

*Note: This package is still under beta version*

Installation

yarn add react-native-unitech-scanner

or

npm install react-native-unitech-scanner

Usage

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);
	};
}, []);

API:

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>

addEventListener(eventType, handler)

Attaches a listener to an event.

Examples

ScanManager.addEventListener(
	ScanManager.constants.events.SCANNER_BARCODE,
	(data) => {
		console.log('eventData', data);
	}
);

removeEventListener(subscriptionId)

Removes the event listener. Do this in componentWillUnmount to prevent memory leaks

Examples

ScanManager.removeEventListener(subscriptionId);

getScannerState()

Get the scanner power states. Returns true if the scanner power on

Examples

ScanManager.getScannerState().then((response) => {
	console.log(response);
});

openScanner()

Turn on the power for the bar code reader. Returns false if failed, true if successful

Examples

ScanManager.openScanner().then((response) => {
	console.log(response);
});

closeScanner()

Turn off the power for the bar code reader. Returns false if failed, true if successful

Examples

ScanManager.closeScanner().then((response) => {
	console.log(response);
});

startDecode()

Call this method to start decoding. Returns true if the scanner and the trigger is already active

Examples

ScanManager.startDecode().then((response) => {
	console.log(response);
});

stopDecode()

This stops any data acquisition currently in progress. Returns true if stop successful

Examples

ScanManager.stopDecode().then((response) => {
	console.log(response);
});

getTriggerLockState()

Get the scan trigger status. Returns true if the scan trigger is already active

Examples

ScanManager.getTriggerLockState().then((response) => {
	console.log(response);
});

lockTrigger()

Set the scan trigger inactive (disable the scan button). Returns true if successful. Returns false if failed.

Examples

ScanManager.lockTrigger().then((response) => {
	console.log(response);
});

unlockTrigger()

Set the scan trigger active (enable the scan button). Returns true if successful. Returns false if failed.

Examples

ScanManager.unlockTrigger().then((response) => {
	console.log(response);
});

Mock a barcode scan

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,
});

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT