react-native-esc-pos-printer
repository·main·Indexed 43 days ago
https://github.com/tr3v3r/react-native-esc-pos-printerAn unofficial React Native library for printing on EPSON TM printers using the Epson ePOS SDK for iOS and Android. Version 4.5.0 supports BLE, LAN, USB, and Wi-Fi connections. It provides a Printer class for sending data and a usePrintersDiscovery hook (or PrintersDiscovery class) for scanning available devices.
What's inside react-native-esc-pos-printer
- An unofficial React Native library designed for printing on EPSON TM printers. It leverages the Epson ePOS SDK for both iOS and Android platforms to facilitate communication with compatible thermal printers.
Understand the PrinterStatusResponse interface
mainThe
PrinterStatusResponseinterface provides a comprehensive snapshot of the printer's current state. It is composed of several status objects (e.g.,connection,online,paper,errorStatus), each following a consistent structure:{ statusCode: number, status: string, message: string }Developers can use these fields to monitor hardware health, paper levels, battery life, and connectivity to ensure reliable printing operations.
Understand the DeviceInfo interface
mainTheDeviceInfointerface represents the metadata and connection details of a discovered device (such as a printer, scanner, or display). It is used to identify and target specific hardware when initiating a connection via theconnectAPI.Configure Android permissions for Bare React Native
mainTo use the library in a bare React Native project on Android, you must add specific permissions to your
android/app/src/main/AndroidManifest.xmlfile. This includes permissions for Internet, Bluetooth (scanning and connecting), legacy Bluetooth support for older devices, location services, and storage access.<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/> <!-- Request legacy permissions on older devices. --> <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>Android-specific connection guidelines
mainWhen connecting on Android, keep the following in mind:
- Cleanup: Always call
disconnectto terminate communication when it is no longer necessary. - DHCP/IP: If using DHCP to assign the printer's IP, use the printer's MAC address or host name as the identifier.
- USB Connection:
- Identifiers depend on the USB connector shape.
- If the identifier is omitted, Android connects to the first USB device found.
- If the Android terminal is in developer mode, you may be unable to connect via USB-A Device Charging.
- Recovery: If
ERR_RECOVERY_FAILUREoccurs, restart the printer. - Locking: Devices other than printers are exclusively locked.
- Cleanup: Always call
Install react-native-esc-pos-printer
mainInstall the library using your preferred package manager.
Important Version Note: If you are using a React Native version lower than
0.76.x, you must usereact-native-esc-pos-printerversion4.3.3or lower.Required Step: You must also complete the additional required steps documented in
./docs/INSTALLATION.mdfor the library to function correctly.### React Native > [!WARNING] > For react-native lower than 0.76.x version, use react-native-esc-pos-printer 4.3.3 or lower ```sh yarn add react-native-esc-pos-printerExpo
npx expo install react-native-esc-pos-printer npx expo prebuildConfigure Android permissions for TCP printing
mainIf your application targets Android 4.3.1 or lower and uses TCP for printing, you must add storage permissions to your
AndroidManifest.xml.<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>Configure iOS for Bare React Native
mainFor iOS, you must perform two configuration steps in your project's
*.plistfile (e.g.,ProjectName-Info.plist) and runpod install.- Bluetooth Usage Description: For iOS 13 or later, add the
Privacy-Bluetooth Always Usage Descriptionkey and provide a string explaining why the app uses Bluetooth (e.g., "Use this to communicate with the printer."). - External Accessory Protocols: To support Bluetooth or USB communication, add the
Supported external accessory protocolskey and set the value forItem 0tocom.epson.escpos.
pod install- Bluetooth Usage Description: For iOS 13 or later, add the
Start the Metro Server
mainBefore running the application, you must start Metro, the JavaScript bundler that ships with React Native. Run this command from the root of your project.
# using npm npm start # OR using Yarn yarn startConfigure Android permissions for Expo
mainWhen using Expo, you must declare the required Android permissions in your
app.jsonfile under theandroid.permissionsarray.{ "android": { "permissions": [ "android.permission.INTERNET", "android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH", "android.permission.BLUETOOTH_ADMIN", "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE" ] } }Start the iOS application
mainTo launch the application on an iOS simulator or device, run the following command from the root of your project. Ensure the Metro server is already running in a separate terminal.
# using npm npm run ios # OR using Yarn yarn iosiOS-specific connection guidelines
mainWhen connecting on iOS, keep the following in mind:
- Cleanup: Always call
disconnectto terminate communication when it is no longer necessary. - Bluetooth:
- Complete pairing before calling the
connectcommand. - Bluetooth communication may disconnect if the application runs in the background (depending on project settings).
- If both Bluetooth and USB are used simultaneously, USB takes priority.
- BLE Limitation: Do not use
addImagewith BLE connections, as sending/receiving large amounts of data is slow.
- Complete pairing before calling the
- TM-DT Series: MAC addresses cannot be specified for these devices.
- Recovery: If
ERR_RECOVERY_FAILUREoccurs, restart the printer. - Locking: Devices other than printers are exclusively locked.
- Cleanup: Always call