import { IPrinterAdapter, IAdapterOptions, PrinterState } from '../types'; import { BluetoothPrintError } from '../errors/baseError'; export type { IPrinterAdapter, IAdapterOptions } from '../types'; export { PrinterState } from '../types'; export { Logger } from '../utils/logger'; export { normalizeError } from '../utils/normalizeError'; export { withTimeout } from '../utils/withTimeout'; export { BluetoothPrintError, ErrorCode } from '../errors/baseError'; export { ChunkWriteStrategy, type ChunkWriteContext, type ChunkWriteResult, DEFAULT_ADAPTIVE_CONFIG, } from './ChunkWriteStrategy'; /** * Service information cache entry */ export interface ServiceInfo { serviceId: string; characteristicId: string; } /** * BLE characteristic properties (common across all mini-program platforms) */ export interface BLECharacteristicProperties { write?: boolean; writeWithoutResponse?: boolean; read?: boolean; notify?: boolean; indicate?: boolean; } /** * BLE characteristic (common across all mini-program platforms) */ export interface BLECharacteristic { uuid: string; properties: BLECharacteristicProperties; } /** * Unified mini-program BLE API interface. * All mini-program platforms (Taro/WeChat, Alipay, Baidu, ByteDance) * share the same API shape, only the global object differs. */ export interface MiniProgramBLEApi { createBLEConnection(options: { deviceId: string; }): Promise; closeBLEConnection(options: { deviceId: string; }): Promise; getBLEConnectionState(options: { deviceId: string; }): Promise<{ connected: boolean; }>; writeBLECharacteristicValue(options: { deviceId: string; serviceId: string; characteristicId: string; value: ArrayBuffer | ArrayBufferLike; }): Promise; getBLEDeviceServices(options: { deviceId: string; }): Promise<{ services: Array<{ uuid: string; }>; }>; getBLEDeviceCharacteristics(options: { deviceId: string; serviceId: string; }): Promise<{ characteristics: BLECharacteristic[]; }>; onBLEConnectionStateChange(callback: (res: { deviceId: string; connected: boolean; }) => void): void; } /** * Base adapter class that provides common functionality for all platform-specific adapters */ export declare abstract class BaseAdapter implements IPrinterAdapter { protected stateCallback?: (state: PrinterState) => void; protected serviceCache: Map; protected readonly logger: { debug: (message: string, ...args: unknown[]) => void; info: (message: string, ...args: unknown[]) => void; warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void; }; /** * Connect to a Bluetooth device * @param deviceId - Unique identifier of the device to connect to */ abstract connect(deviceId: string): Promise; /** * Disconnect from a Bluetooth device * @param deviceId - Unique identifier of the device to disconnect from */ abstract disconnect(deviceId: string): Promise; /** * Write data to a connected Bluetooth device * @param deviceId - Unique identifier of the connected device * @param buffer - Data to write as ArrayBuffer * @param options - Optional settings for the write operation */ abstract write(deviceId: string, buffer: ArrayBuffer, options?: IAdapterOptions): Promise; /** * Start discovering nearby Bluetooth devices (optional) */ startDiscovery?(): Promise; /** * Stop discovering nearby Bluetooth devices (optional) */ stopDiscovery?(): Promise; /** * Register a callback for connection state changes * @param callback - Function to call when the state changes */ onStateChange(callback: (state: PrinterState) => void): void; /** * Updates the internal state and notifies callbacks * @param state - New printer state */ protected updateState(state: PrinterState): void; /** * Validates device ID * @param deviceId - Device ID to validate * @throws BluetoothPrintError if device ID is invalid */ protected validateDeviceId(deviceId: string): void; /** * Validates buffer data * @param buffer - Buffer to validate * @throws BluetoothPrintError if buffer is invalid */ protected validateBuffer(buffer: ArrayBuffer): void; /** * Validates adapter options * @param options - Options to validate * @returns Validated options with default values */ protected validateOptions(options?: IAdapterOptions): Required; /** * Gets service info from cache * @param deviceId - Device ID * @returns Service info if found, otherwise throws an error * @throws BluetoothPrintError if service info not found */ protected getServiceInfo(deviceId: string): ServiceInfo; /** * Checks if device is connected * @param deviceId - Device ID * @returns True if device is connected, false otherwise */ protected isDeviceConnected(deviceId: string): boolean; /** * Cleans up resources for a device * @param deviceId - Device ID */ protected cleanupDevice(deviceId: string): void; /** * Classifies a connection error into the appropriate BluetoothPrintError. * Examines the error message to determine whether it was a timeout, * device-not-found, or generic connection failure. * * @param error - The original error * @param deviceId - The device ID that failed to connect * @returns A classified BluetoothPrintError */ protected classifyConnectionError(error: unknown, deviceId: string): BluetoothPrintError; /** * Template method for discovering BLE services and caching the writeable characteristic. * Subclasses provide a platform-specific discovery function that returns a normalised * list of services with their characteristics. * * @param deviceId - Bluetooth device ID * @param discoverFn - Function that returns normalised service/characteristic info * @throws {BluetoothPrintError} When no writeable characteristic is found */ protected discoverAndCacheServices(deviceId: string, discoverFn: () => Promise; }>>): Promise; /** * Cleanup resources and destroy the adapter instance. * Removes all event listeners and releases resources. */ destroy(): void; } /** * Base adapter for mini-program platforms (Taro/WeChat, Alipay, Baidu, ByteDance). * * Implements connect/disconnect/write/discoverServices once with adaptive transmission. * Subclasses only need to implement `getApi()` to return the platform-specific BLE API object. */ export declare abstract class MiniProgramAdapter extends BaseAdapter { private writeStrategy; /** * Returns the platform-specific BLE API object. * Subclasses must implement this to return the appropriate global (Taro, my, swan, tt). */ protected abstract getApi(): MiniProgramBLEApi; /** * Lazy-init the write strategy using the adapter's API */ private getWriteStrategy; /** * Connects to a Bluetooth device and discovers services * * @param deviceId - Bluetooth device ID * @throws {BluetoothPrintError} When connection fails */ connect(deviceId: string): Promise; /** * Disconnects from a Bluetooth device * * @param deviceId - Bluetooth device ID */ disconnect(deviceId: string): Promise; /** * Writes data to the Bluetooth device in chunks with adaptive transmission. * * Features: * - Automatic chunk size adjustment based on success/failure rate * - Dynamic delay adjustment for congestion control * - Periodic connection state checks * - Exponential backoff for retries * - Write timeout per chunk * * @param deviceId - Bluetooth device ID * @param buffer - Data to write as ArrayBuffer * @param options - Write options (chunk size, delay, retries) * @throws {BluetoothPrintError} When write fails after retries */ write(deviceId: string, buffer: ArrayBuffer, options?: IAdapterOptions): Promise; /** * Discovers services and characteristics for a device. * Caches the writeable characteristic for future writes. * * @param deviceId - Bluetooth device ID * @throws {BluetoothPrintError} When no writeable characteristic is found */ private discoverServices; }