### listen() Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Starts listening for status notifications from the printer, such as paper out or cover open events. ```APIDOC ## listen() ### Description Starts listening for status notifications from the printer. When enabled, the printer will send status updates which can be received via the 'data' event. ### Response - **returns** (boolean) - Returns true if the printer supports status notifications, false otherwise. ``` -------------------------------- ### Constructor Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Initializes a new instance of the WebBluetoothReceiptPrinter library. ```APIDOC ## Constructor ### Description Creates a new WebBluetoothReceiptPrinter instance that initializes internal event handling and sets up disconnect event listeners. ### Request Example ```javascript import WebBluetoothReceiptPrinter from '@point-of-sale/webbluetooth-receipt-printer'; const receiptPrinter = new WebBluetoothReceiptPrinter(); ``` ``` -------------------------------- ### connect() Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Initiates the Bluetooth device selection dialog for the user. ```APIDOC ## connect() ### Description Initiates a Bluetooth device selection dialog allowing the user to choose a receipt printer. Must be called as a result of a user action due to security requirements. ### Request Example ```javascript await receiptPrinter.connect(); ``` ``` -------------------------------- ### Initialize WebBluetoothReceiptPrinter Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Instantiates the printer object using either ES modules or a UMD bundle. ```javascript import WebBluetoothReceiptPrinter from '@point-of-sale/webbluetooth-receipt-printer'; // Create a new printer instance const receiptPrinter = new WebBluetoothReceiptPrinter(); // Or using UMD bundle in browser // // const receiptPrinter = new WebBluetoothReceiptPrinter(); ``` -------------------------------- ### Include Library in Browser Source: https://github.com/nielsleenheer/webbluetoothreceiptprinter/blob/main/README.md Load the library script for browser usage. ```html ``` -------------------------------- ### Implement Web Bluetooth Printer Connection and Printing Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt A complete HTML implementation showing connection, status event handling, and receipt generation using the ReceiptPrinterEncoder. ```html Receipt Printer Demo
Not connected
``` -------------------------------- ### Import Library Module Source: https://github.com/nielsleenheer/webbluetoothreceiptprinter/blob/main/README.md Import the library as an ES module for modern JavaScript environments. ```js import WebHIDBarcodeScanner from 'webbluetooth-receipt-printer.esm.js'; const receiptPrinter = new WebBluetoothReceiptPrinter(); ``` -------------------------------- ### Connect to Receipt Printer Source: https://github.com/nielsleenheer/webbluetoothreceiptprinter/blob/main/README.md Initiate a connection to a Bluetooth receipt printer. This must be triggered by a user action. ```js function handleConnectButtonClick() { receiptPrinter.connect(); } ``` -------------------------------- ### Listen for Printer Connection Events Source: https://github.com/nielsleenheer/webbluetoothreceiptprinter/blob/main/README.md Add an event listener to detect when a receipt printer successfully connects. The event provides device details and can be used to store the device ID for future reconnections. ```js receiptPrinter.addEventListener('connected', device => { console.log(`Connected to ${device.name} (#${device.id})`); printerLanguage = device.language; printerCodepageMapping = device.codepageMapping; /* Store device for reconnecting */ lastUsedDevice = device; }); ``` -------------------------------- ### reconnect(previousDevice) Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Reconnects to a previously paired Bluetooth printer. ```APIDOC ## reconnect(previousDevice) ### Description Reconnects to a previously paired Bluetooth printer without showing the device selection dialog. ### Parameters #### Request Body - **previousDevice** (Object) - Required - The device information object received from a previous connected event. ### Request Example ```javascript await receiptPrinter.reconnect(previousDevice); ``` ``` -------------------------------- ### addEventListener(eventName, callback) Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Registers an event listener for printer lifecycle and data events. ```APIDOC ## addEventListener(eventName, callback) ### Description Registers an event listener for printer events. Supported events include 'connected', 'disconnected', and 'data'. ### Parameters #### Path Parameters - **eventName** (string) - Required - The name of the event to listen for ('connected', 'disconnected', 'data'). - **callback** (function) - Required - The function to execute when the event is triggered. ``` -------------------------------- ### print(commands) Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Sends encoded receipt data to the connected printer. Handles automatic chunking for Bluetooth packet size limitations. ```APIDOC ## print(commands) ### Description Sends encoded receipt data to the connected printer. The method accepts an array or typed array of bytes that have been properly encoded for the printer. Large print jobs are automatically chunked to handle Bluetooth packet size limitations. ### Parameters #### Request Body - **commands** (Array/TypedArray) - Required - Encoded receipt data bytes. ``` -------------------------------- ### Register event listeners Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Handles printer lifecycle events such as connection, disconnection, and incoming status data. ```javascript const receiptPrinter = new WebBluetoothReceiptPrinter(); // Listen for connection events receiptPrinter.addEventListener('connected', device => { console.log(`Connected to: ${device.name}`); console.log(`Device ID: ${device.id}`); console.log(`Connection type: ${device.type}`); // 'bluetooth' console.log(`Printer language: ${device.language}`); // 'esc-pos' or 'star-prnt' console.log(`Codepage mapping: ${device.codepageMapping}`); // 'epson', 'star', 'zjiang', etc. // Update UI document.getElementById('printer-name').textContent = device.name; document.getElementById('connect-btn').disabled = true; document.getElementById('print-btn').disabled = false; }); // Listen for disconnection events receiptPrinter.addEventListener('disconnected', () => { console.log('Printer disconnected'); // Update UI document.getElementById('printer-name').textContent = 'Not connected'; document.getElementById('connect-btn').disabled = false; document.getElementById('print-btn').disabled = true; }); // Listen for data events (printer status) receiptPrinter.addEventListener('data', data => { console.log('Received data from printer:', data); }); ``` -------------------------------- ### Connect to a Printer Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Triggers the browser's Bluetooth device selection dialog. Must be invoked within a user-initiated event handler. ```javascript const receiptPrinter = new WebBluetoothReceiptPrinter(); // Must be called from a user-initiated event (e.g., button click) document.getElementById('connect-btn').addEventListener('click', async () => { try { await receiptPrinter.connect(); console.log('Printer connection initiated'); } catch (error) { console.error('Connection failed:', error); } }); ``` -------------------------------- ### Reconnect to a Previous Device Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Restores a connection to a previously paired printer using stored device information, bypassing the selection dialog. ```javascript const receiptPrinter = new WebBluetoothReceiptPrinter(); // Store device info when connected let lastUsedDevice = null; receiptPrinter.addEventListener('connected', device => { // Save device for later reconnection lastUsedDevice = device; localStorage.setItem('lastPrinter', JSON.stringify(device)); }); // On page load, try to reconnect to the last used printer window.addEventListener('load', async () => { const savedDevice = localStorage.getItem('lastPrinter'); if (savedDevice) { const previousDevice = JSON.parse(savedDevice); await receiptPrinter.reconnect(previousDevice); } }); ``` -------------------------------- ### disconnect() Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Disconnects from the currently connected Bluetooth printer. ```APIDOC ## disconnect() ### Description Disconnects from the currently connected Bluetooth printer and cleans up all internal references. Emits a disconnected event upon completion. ### Request Example ```javascript await receiptPrinter.disconnect(); ``` ``` -------------------------------- ### Listen for printer status Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Enables status notifications from the printer. Returns a boolean indicating if the printer supports this feature. ```javascript const receiptPrinter = new WebBluetoothReceiptPrinter(); receiptPrinter.addEventListener('connected', async device => { console.log(`Connected to ${device.name}`); // Start listening for printer status const supportsStatus = await receiptPrinter.listen(); if (supportsStatus) { console.log('Printer status notifications enabled'); } else { console.log('Printer does not support status notifications'); } }); // Handle status data from printer receiptPrinter.addEventListener('data', statusData => { console.log('Printer status received:', statusData); // Parse status bytes to determine printer state // (paper status, cover status, error conditions, etc.) }); ``` -------------------------------- ### Print receipt data Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Sends encoded byte data to the printer. Requires an encoder library to format data according to the printer's specific language and codepage. ```javascript import WebBluetoothReceiptPrinter from '@point-of-sale/webbluetooth-receipt-printer'; import ReceiptPrinterEncoder from '@point-of-sale/receipt-printer-encoder'; const receiptPrinter = new WebBluetoothReceiptPrinter(); let printerLanguage = null; let printerCodepageMapping = null; // Store printer settings when connected receiptPrinter.addEventListener('connected', device => { printerLanguage = device.language; printerCodepageMapping = device.codepageMapping; }); // Print a receipt async function printReceipt() { // Create encoder with printer-specific settings const encoder = new ReceiptPrinterEncoder({ language: printerLanguage, codepageMapping: printerCodepageMapping }); // Build the receipt const data = encoder .initialize() .align('center') .bold(true) .text('ACME STORE') .bold(false) .newline() .text('123 Main Street') .newline() .newline() .align('left') .text('Item 1 $10.00') .newline() .text('Item 2 $15.50') .newline() .text('Item 3 $8.99') .newline() .newline() .bold(true) .text('TOTAL $34.49') .bold(false) .newline() .newline() .align('center') .qrcode('https://example.com/receipt/12345') .newline() .text('Thank you for shopping!') .newline() .cut() .encode(); // Send to printer await receiptPrinter.print(data); console.log('Receipt printed successfully'); } ``` -------------------------------- ### Print Receipt Data Source: https://github.com/nielsleenheer/webbluetoothreceiptprinter/blob/main/README.md Send encoded receipt data (raw bytes) to the connected printer. Ensure the data is correctly encoded using a library like ReceiptPrinterEncoder. ```js /* Encode the receipt */ let encoder = new ReceiptPrinterEncoder({ language: printerLanguage, codepageMapping: printerCodepageMapping }); let data = encoder .initialize() .text('The quick brown fox jumps over the lazy dog') .newline() .qrcode('https://nielsleenheer.com') .encode(); /* Print the receipt */ receiptPrinter.print(data); ``` -------------------------------- ### Reconnect to Previously Connected Printer Source: https://github.com/nielsleenheer/webbluetoothreceiptprinter/blob/main/README.md Attempt to reconnect to a printer using its stored ID. This is useful for automatic reconnections on page load. ```js receiptPrinter.reconnect(lastUsedDevice); ``` -------------------------------- ### Disconnect from a Printer Source: https://context7.com/nielsleenheer/webbluetoothreceiptprinter/llms.txt Terminates the active Bluetooth connection and cleans up internal references. ```javascript const receiptPrinter = new WebBluetoothReceiptPrinter(); // Disconnect button handler document.getElementById('disconnect-btn').addEventListener('click', async () => { await receiptPrinter.disconnect(); console.log('Printer disconnected'); }); // Listen for disconnection events receiptPrinter.addEventListener('disconnected', () => { console.log('Printer has been disconnected'); document.getElementById('status').textContent = 'Disconnected'; }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.