### Set up the development environment Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Commands to clone the repository, install dependencies, and launch the example application on Android. ```bash git clone https://github.com/your-org/expo-printers-sdk.git cd expo-printers-sdk npm install # Run the example cd example npm install npm run android ``` -------------------------------- ### Run the Example Application Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Commands to navigate to the example directory, install dependencies, and launch the Android application. ```bash cd example npm install npm run android ``` -------------------------------- ### Install expo-printers-sdk Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Use this command to add the package to your Expo project dependencies. ```bash bunx expo install expo-printers-sdk ``` -------------------------------- ### Import SDK Modules Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Import the necessary printer classes and type definitions from the SDK. ```typescript import { EpsonPrinters, RongtaPrinters, StarMicronicsPrinters, type PrinterConnectionType, type EpsonPrinterInfo, type RongtaPrinterInfo, type StarMicronicsPrinterInfo, } from "expo-printers-sdk"; ``` -------------------------------- ### Discover Printers via Auto-Discovery Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Set up an event listener to detect printers and initiate the discovery process. ```typescript import { useEffect, useState } from "react"; const [printers, setPrinters] = useState([]); useEffect(() => { // Listen for discovered printers const listener = EpsonPrinters.addListener("onPrintersFound", (data) => { console.log("Found printers:", data.printers); setPrinters(data.printers); }); return () => listener.remove(); }, []); // Start discovery const discoverPrinters = async () => { const hasPermissions = await requestPermissions(); if (hasPermissions) { await EpsonPrinters.findPrinters("Network"); } }; ``` -------------------------------- ### Print Images Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Fetch an image, convert it to base64, and send it to the printer with error handling. ```typescript import { Alert } from "react-native"; // Function to fetch and convert image to base64 const fetchImageAsBase64 = async (imageUrl: string): Promise => { const response = await fetch(imageUrl); if (!response.ok) { throw new Error(`Failed to fetch image: ${response.statusText}`); } const blob = await response.blob(); return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { const result = reader.result as string; const base64Data = result.split(",")[1] || ""; resolve(base64Data); }; reader.onerror = reject; reader.readAsDataURL(blob); }); }; // Print function with error handling const printImage = async (printer: EpsonPrinterInfo, imageUrl: string) => { try { // Listen for print results const printListener = EpsonPrinters.addListener( "onPrintImage", (result) => { if (result.success) { Alert.alert("Success", "Image printed successfully!"); } else { Alert.alert("Error", result.error || "Print failed"); } printListener.remove(); } ); // Fetch and convert image const base64Image = await fetchImageAsBase64(imageUrl); // Initiate printing const success = await EpsonPrinters.printImage(base64Image, printer); if (!success) { Alert.alert("Error", "Failed to initiate print job"); } } catch (error) { console.error("Print error:", error); Alert.alert("Error", "Failed to print image"); } }; ``` -------------------------------- ### Star Micronics Printer Methods Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md API methods for discovering, connecting, and printing with Star Micronics printers. ```typescript // Discover available printers StarMicronicsPrinters.findPrinters(connectionType: PrinterConnectionType): Promise // Connect manually to a network printer StarMicronicsPrinters.connectManually( connectionType: PrinterConnectionType, connectionDetails: { ipAddress: string; port?: number; modelName: string } ): Promise // Get list of supported printer models StarMicronicsPrinters.getSupportedModels(): Promise // Print base64 image to printer StarMicronicsPrinters.printImage(base64Image: string, deviceData: StarMicronicsPrinterInfo): Promise ``` -------------------------------- ### Configure Android Permissions Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Include these permissions in your Android manifest to enable Bluetooth, location-based discovery, and network connectivity. ```xml ``` -------------------------------- ### Epson Printer Methods Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md API methods for discovering, connecting, and printing with Epson printers. ```typescript // Discover available printers EpsonPrinters.findPrinters(connectionType: PrinterConnectionType): Promise // Connect manually to a network printer EpsonPrinters.connectManually( connectionType: PrinterConnectionType, connectionDetails: { ipAddress: string; port?: number; modelName?: string } ): Promise // Get list of supported printer models EpsonPrinters.getSupportedModels(): Promise // Print base64 image to printer EpsonPrinters.printImage(base64Image: string, deviceData: EpsonPrinterInfo): Promise // Event listener management EpsonPrinters.addListener(eventName: string, listener: Function): EventSubscription ``` -------------------------------- ### Request Android Permissions Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Request required Bluetooth and location permissions for Android devices before attempting printer discovery. ```typescript import { PermissionsAndroid, Platform } from "react-native"; const requestPermissions = async () => { if (Platform.OS === "android") { const permissions = [ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN, PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT, ]; const results = await PermissionsAndroid.requestMultiple(permissions); return Object.values(results).every( (result) => result === PermissionsAndroid.RESULTS.GRANTED ); } return true; }; ``` -------------------------------- ### Define Printer Connection Types Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Defines the supported connection protocols for printers. ```typescript type PrinterConnectionType = "Bluetooth" | "Network" | "USB"; ``` -------------------------------- ### Rongta Printer Methods Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md API methods for discovering, connecting, and printing with Rongta printers. ```typescript // Discover available printers RongtaPrinters.findPrinters(connectionType: PrinterConnectionType): Promise // Connect manually to a network printer RongtaPrinters.connectManually( connectionType: PrinterConnectionType, connectionDetails: { ipAddress: string; port?: number } ): Promise // Print base64 image to printer RongtaPrinters.printImage(base64Image: string, deviceData: RongtaPrinterInfo): Promise ``` -------------------------------- ### Connect to Printers Manually Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Connect directly to a printer using its IP address and port, bypassing the discovery process. ```typescript // Rongta const connectToPrinter = async () => { try { const printer = await RongtaPrinters.connectManually("Network", { ipAddress: "192.168.1.100", port: 9100, }); console.log("Connected to printer:", printer); } catch (error) { console.error("Failed to connect:", error); } }; // Epson & Star require a model identifier so the SDK can select capabilities const epsonPrinter = await EpsonPrinters.connectManually("Network", { ipAddress: "192.168.1.101", port: 9100, modelName: "TM-m30II", }); const starPrinter = await StarMicronicsPrinters.connectManually("Network", { ipAddress: "192.168.1.102", port: 9100, modelName: "mC-Print3", }); ``` -------------------------------- ### Handle Printer Connection Errors Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Use a try-catch block to manage specific printer connection errors such as invalid connection types or permission issues. ```typescript try { const success = await EpsonPrinters.findPrinters("Bluetooth"); if (!success) { console.log("No printers found or search failed"); } } catch (error) { if (error.message.includes("Invalid connection type")) { // Handle invalid connection type } else if (error.message.includes("Permission")) { // Handle permission errors } else { // Handle other errors } } ``` -------------------------------- ### RongtaPrinters.findPrinters Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Initiates a discovery process for available Rongta printers. ```APIDOC ## RongtaPrinters.findPrinters(connectionType: PrinterConnectionType): Promise ### Description Discover available Rongta printers. ``` -------------------------------- ### Epson Printer Types Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Data structures for Epson printer information, print results, and module event listeners. ```typescript type EpsonPrinterInfo = { deviceName: string; // e.g., "TM-T88V" target: string; // Connection target (BT:MAC or TCP:IP) ipAddress: string; // IP address for network printers macAddress: string; // MAC address for Bluetooth bdAddress: string; // Bluetooth device address connectionType: PrinterConnectionType; deviceType: number; // Epson device type identifier }; type EpsonPrintResult = { success: boolean; error?: string; // Detailed error message if failed }; type EpsonPrintersModuleEvents = { onPrintersFound: (data: { printers: EpsonPrinterInfo[] }) => void; onPrintImage: (result: EpsonPrintResult) => void; }; ``` -------------------------------- ### Star Micronics Printer Types Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Data structures for Star Micronics printer information and print results. ```typescript type StarMicronicsPrinterInfo = { deviceName: string; // e.g., "TSP100" portName: string; // Port identifier (TCP:IP or BT:MAC) macAddress: string; // MAC address usbSerialNumber: string; // USB serial (if applicable) connectionType: PrinterConnectionType; }; type StarMicronicsPrintResult = { success: boolean; errorCode?: StarMicronicsPrintErrorCode; error?: string; }; ``` -------------------------------- ### EpsonPrinters.connectManually Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Manually connects to a network-based Epson printer using IP and port details. ```APIDOC ## EpsonPrinters.connectManually(connectionType: PrinterConnectionType, connectionDetails: object): Promise ### Description Establishes a manual connection to a network printer. ### Parameters - **connectionType** (PrinterConnectionType) - Required - The connection type. - **connectionDetails** (object) - Required - Contains ipAddress (string), port (number, optional), and modelName (string, optional). ``` -------------------------------- ### Rongta Printer Types Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Data structures for Rongta printer information and print results. ```typescript type RongtaPrinterInfo = { connectionType: PrinterConnectionType; type: RongtaPrinterType; isSupported: boolean; unsupportedReason?: string; }; type RongtaPrinterType = | { type: "BLUETOOTH"; alias: string; // Display name name: string; // Device name address: string; // Bluetooth MAC address } | { type: "NETWORK"; ipAddress: string; // IP address port: number; // TCP port (default 9100) } | { type: "USB"; name: string; // Device name vendorId: number; // USB vendor ID productId: number; // USB product ID }; type RongtaPrintResult = { success: boolean; error?: string; }; ``` -------------------------------- ### EpsonPrinters.findPrinters Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Initiates a discovery process for available Epson printers based on the specified connection type. ```APIDOC ## EpsonPrinters.findPrinters(connectionType: PrinterConnectionType): Promise ### Description Discover available Epson printers on the network, Bluetooth, or USB. ### Parameters - **connectionType** (PrinterConnectionType) - Required - The type of connection to scan (Bluetooth, Network, or USB). ``` -------------------------------- ### StarMicronicsPrinters.findPrinters Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Initiates a discovery process for available Star Micronics printers. ```APIDOC ## StarMicronicsPrinters.findPrinters(connectionType: PrinterConnectionType): Promise ### Description Discover available Star Micronics printers. ``` -------------------------------- ### RongtaPrinters.printImage Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Sends a base64 encoded image to the specified Rongta printer. ```APIDOC ## RongtaPrinters.printImage(base64Image: string, deviceData: RongtaPrinterInfo): Promise ### Description Prints a base64 image to the target Rongta printer. ``` -------------------------------- ### StarMicronicsPrinters.printImage Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Sends a base64 encoded image to the specified Star Micronics printer. ```APIDOC ## StarMicronicsPrinters.printImage(base64Image: string, deviceData: StarMicronicsPrinterInfo): Promise ### Description Prints a base64 image to the target Star Micronics printer. ``` -------------------------------- ### EpsonPrinters.printImage Source: https://github.com/wiedymi/expo-printers-sdk/blob/main/README.md Sends a base64 encoded image to the specified Epson printer for printing. ```APIDOC ## EpsonPrinters.printImage(base64Image: string, deviceData: EpsonPrinterInfo): Promise ### Description Prints a base64 image to the target printer. ### Parameters - **base64Image** (string) - Required - The image data in base64 format. - **deviceData** (EpsonPrinterInfo) - Required - The printer information object. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.