UNPKG

@sfourdrinier/react-native-ble-plx

Version:
183 lines 5.69 kB
import { Device } from './Device'; import { BleError } from './BleError'; import type { BleManager } from './BleManager'; import type { DeviceId, ConnectionOptions } from './TypeDefinition'; /** * Connection options with retry and timeout support */ export interface ConnectionOptionsWithRetry { /** * Connection options to pass to the device */ connectionOptions?: ConnectionOptions; /** * Maximum number of retry attempts (default: 3) */ maxRetries?: number; /** * Initial delay before first retry in milliseconds (default: 1000) */ initialDelayMs?: number; /** * Maximum delay between retries in milliseconds (default: 30000) */ maxDelayMs?: number; /** * Multiplier for exponential backoff (default: 2) */ backoffMultiplier?: number; /** * Connection timeout in milliseconds (default: 30000) * Set to 0 to disable timeout */ timeoutMs?: number; } /** * Auto-reconnection options */ export interface AutoReconnectOptions extends ConnectionOptionsWithRetry { /** * Enable automatic reconnection on unexpected disconnects */ enabled: boolean; } /** * Event callbacks for connection events */ export interface ConnectionCallbacks { /** * Called when a device successfully connects or reconnects */ onConnect?: (device: Device) => void; /** * Called when connection fails after all retries */ onConnectFailed?: (deviceId: DeviceId, error: BleError) => void; /** * Called when a connection attempt starts (includes retry attempts) */ onConnecting?: (deviceId: DeviceId, attempt: number, maxAttempts: number) => void; /** * Called when a device disconnects unexpectedly */ onDisconnect?: (deviceId: DeviceId, error: BleError | null) => void; } /** * ConnectionManager unifies connection queuing, retry logic, and automatic reconnection. * * This manager replaces both ConnectionQueue and ReconnectionManager with a single, * unified state machine per device that prevents conflicts and connection storms. * * Features: * - Single connection state per device (no competing retry engines) * - Concurrent connections to different devices * - Exponential backoff retry logic * - Configurable connection timeout * - Automatic reconnection on unexpected disconnects * - Comprehensive event callbacks * * @example * const manager = new ConnectionManager(bleManager); * * // Connect with retry logic * const device = await manager.connect('AA:BB:CC:DD:EE:FF', { * maxRetries: 5, * timeoutMs: 15000, * backoffMultiplier: 1.5 * }); * * // Enable auto-reconnect for a device * manager.enableAutoReconnect('AA:BB:CC:DD:EE:FF', { * maxRetries: 10, * initialDelayMs: 2000 * }, { * onConnect: (device) => console.log('Connected!', device.id), * onDisconnect: (deviceId, error) => console.log('Disconnected', deviceId) * }); * * // Cancel a connection * manager.cancel('AA:BB:CC:DD:EE:FF'); */ export declare class ConnectionManager { private _manager; private _devices; private _globalCallbacks; constructor(manager: BleManager); /** * Set global callbacks for all connection events. * * @param callbacks Callback functions for connection events */ setGlobalCallbacks(callbacks: ConnectionCallbacks): void; /** * Connect to a device with retry logic and timeout. * * @param deviceId Device identifier to connect to * @param options Connection and retry options * @returns Promise resolving to connected Device */ connect(deviceId: DeviceId, options?: ConnectionOptionsWithRetry): Promise<Device>; /** * Enable automatic reconnection for a device. * * @param deviceId Device identifier * @param options Connection and retry options for reconnection * @param callbacks Optional callbacks for this specific device */ enableAutoReconnect(deviceId: DeviceId, options?: ConnectionOptionsWithRetry, callbacks?: ConnectionCallbacks): void; /** * Disable automatic reconnection for a device. * * @param deviceId Device identifier * @returns True if auto-reconnect was disabled, false if it wasn't enabled */ disableAutoReconnect(deviceId: DeviceId): boolean; /** * Cancel a pending or in-progress connection attempt. * * @param deviceId Device identifier to cancel connection for * @returns True if a connection was cancelled */ cancel(deviceId: DeviceId): boolean; /** * Cancel all pending connections. */ cancelAll(): void; /** * Get the number of devices with active or pending connections. */ get activeCount(): number; /** * Check if a device has a pending or active connection. */ isConnecting(deviceId: DeviceId): boolean; /** * Check if auto-reconnect is enabled for a device. */ isAutoReconnectEnabled(deviceId: DeviceId): boolean; /** * Get the current retry count for a device. */ getRetryCount(deviceId: DeviceId): number; /** * Cancel state timers and subscriptions */ private _cancelState; /** * Start reconnection after unexpected disconnect */ private _startReconnection; /** * Schedule a connection attempt with delay */ private _scheduleConnection; /** * Attempt to connect to a device with timeout and retry logic */ private _attemptConnection; /** * Destroy the manager and cancel all pending connections. */ destroy(): void; } //# sourceMappingURL=ConnectionManager.d.ts.map