react-native-esc-pos-printer

repository·main·Indexed 43 days ago

https://github.com/tr3v3r/react-native-esc-pos-printer

An 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.

Tokens
37.7K
Snippets
111
Records
169
Agent score
53%

What's inside react-native-esc-pos-printer

  1. Understand the PrinterStatusResponse interface

    main

    The PrinterStatusResponse interface 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.

  2. Configure Android permissions for Bare React Native

    main

    To use the library in a bare React Native project on Android, you must add specific permissions to your android/app/src/main/AndroidManifest.xml file. 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"/>
  3. Android-specific connection guidelines

    main

    When connecting on Android, keep the following in mind:

    • Cleanup: Always call disconnect to 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_FAILURE occurs, restart the printer.
    • Locking: Devices other than printers are exclusively locked.
  4. Install react-native-esc-pos-printer

    main

    Install 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 use react-native-esc-pos-printer version 4.3.3 or lower.

    Required Step: You must also complete the additional required steps documented in ./docs/INSTALLATION.md for 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-printer

    Expo

    npx expo install react-native-esc-pos-printer
    npx expo prebuild
  5. Configure iOS for Bare React Native

    main

    For iOS, you must perform two configuration steps in your project's *.plist file (e.g., ProjectName-Info.plist) and run pod install.

    1. Bluetooth Usage Description: For iOS 13 or later, add the Privacy-Bluetooth Always Usage Description key and provide a string explaining why the app uses Bluetooth (e.g., "Use this to communicate with the printer.").
    2. External Accessory Protocols: To support Bluetooth or USB communication, add the Supported external accessory protocols key and set the value for Item 0 to com.epson.escpos.
    pod install
  6. Configure Android permissions for Expo

    main

    When using Expo, you must declare the required Android permissions in your app.json file under the android.permissions array.

    {
      "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"
         ]
      }
    }
  7. iOS-specific connection guidelines

    main

    When connecting on iOS, keep the following in mind:

    • Cleanup: Always call disconnect to terminate communication when it is no longer necessary.
    • Bluetooth:
      • Complete pairing before calling the connect command.
      • 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 addImage with BLE connections, as sending/receiving large amounts of data is slow.
    • TM-DT Series: MAC addresses cannot be specified for these devices.
    • Recovery: If ERR_RECOVERY_FAILURE occurs, restart the printer.
    • Locking: Devices other than printers are exclusively locked.