import type { BLEAdvertisement, BLEScanOptions } from './types/ble.js'; import { Buffer } from 'node:buffer'; import { EventEmitter } from 'node:events'; import { Logger } from './utils/index.js'; /** * BLE Scanner for discovering SwitchBot devices */ export declare class BLEScanner extends EventEmitter { private noble; private logger; private scanning; private discoveredDevices; private discoveredModelCache; private nobleStateHandler?; private nobleDiscoverHandler?; private nobleScanStartHandler?; private nobleScanStopHandler?; private noblePromise; constructor(options?: { noble?: any; logLevel?: number; }); /** * Initialize Noble lazily */ private initializeNoble; /** * Ensure Noble is loaded before operations */ private ensureNoble; /** * Setup Noble event handlers */ private setupNobleHandlers; /** * Remove Noble event handlers */ private removeNobleHandlers; /** * Handle device discovery */ private handleDiscovery; /** * Parse service data from BLE advertisement */ private parseServiceData; /** * Start scanning for devices */ startScan(options?: BLEScanOptions): Promise; /** * Stop scanning */ stopScan(): void; /** * Cleanup all resources */ destroy(): void; /** * Get all discovered devices */ getDiscoveredDevices(): BLEAdvertisement[]; /** * Get discovered device by MAC or BLE ID */ getDevice(mac: string, bleId?: string): BLEAdvertisement | undefined; /** * Check if currently scanning */ isScanning(): boolean; /** * Wait for specific device */ waitForDevice(mac: string, timeoutMs?: number, bleId?: string): Promise; } interface EncryptedSession { mode: 'ctr' | 'gcm'; iv: Buffer; } /** Parse the reply to the encryption init command (pySwitchbot _ensure_encryption_initialized) */ export declare function parseEncryptionInit(reply: Buffer | undefined, mac: string): EncryptedSession; /** Encrypt a command body; returns the ciphertext and the 2-byte packet header */ export declare function encryptPayload(key: Buffer, session: EncryptedSession, data: Buffer): { ciphertext: Buffer; header: Buffer; }; /** Decrypt a reply payload. GCM replies carry only a partial tag, so the tag is not checked. */ export declare function decryptPayload(key: Buffer, session: EncryptedSession, data: Buffer): Buffer; /** * BLE Connection for communicating with SwitchBot devices */ export declare class BLEConnection { private noble; private logger; private connections; private characteristics; private disconnectTimers; private operationLocks; private encryptionConfig; private encryptedSessions; private notificationHandlers; private pendingSubscriptions; private persistentConnectionMs; private noblePromise; private notificationFutures; private notifyListeners; private expectedDisconnects; /** * Mark the next disconnect started by the device as expected (for example after a command that * restarts it), so it is not logged as unexpected */ markExpectedDisconnect(mac: string): void; constructor(options?: { noble?: any; logLevel?: number; logger?: Logger; }); /** * Initialize Noble lazily */ private initializeNoble; /** * Ensure Noble is loaded before operations */ private ensureNoble; private withMacLock; private clearDisconnectTimer; private scheduleDisconnect; setPersistentConnectionTimeout(timeoutMs: number): void; setEncryption(mac: string, keyHex: string, ivHex: string, mode?: 'auto' | 'ctr' | 'gcm'): void; clearEncryption(mac: string): void; private getKnownPeripherals; private incrementIv; private encryptIfConfigured; private validateCommandResult; sendCommand(mac: string, data: Buffer, options?: { expectResponse?: boolean; validateResponse?: boolean; responseTimeoutMs?: number; expectNotification?: boolean; notificationTimeoutMs?: number; }): Promise; /** * Write a packet and return the device's reply. The caller holds the MAC lock. */ private exchangeLocked; /** * Send a command to an encrypted device and return its decrypted reply, using the protocol in * pySwitchbot's SwitchbotEncryptedDevice: * * - Session setup, once per connection: send `57 000000 0F2103 ` unencrypted. The reply is * status (0x01), byte 2 the AES mode (0 CTR, 1 GCM), then the IV (CTR: bytes 4..; GCM: 12 bytes * from byte 4). * - Command `57 ` is sent as `57 <2-byte header> `, where the header is * IV[0..2] for CTR or the first 2 bytes of the GCM tag. * - The reply is ` <3 bytes> `; this returns ` `. In GCM * mode the IV is incremented after every command. */ sendEncryptedCommand(mac: string, command: Buffer, options: { keyId: string; encryptionKey: string; validateResponse?: boolean; responseTimeoutMs?: number; }): Promise; /** * Register a wait for the next notification from a device, before the command is written. * Returns undefined when the notify characteristic emits no events (then the caller reads it). */ private prepareReply; /** * Attach one 'data' listener per notify characteristic: a notification answers the pending * command if there is one, otherwise it goes to subscribeNotifications() handlers. */ private ensureNotifyListener; subscribeNotifications(mac: string, handler: (payload: Buffer) => void): Promise; /** * Enable notifications on the notify characteristic (no-op if the backend has no subscribe) */ private subscribeToNotify; unsubscribeNotifications(mac: string, handler: (payload: Buffer) => void): void; /** * Connect to a device */ connect(mac: string): Promise; /** * Discover service characteristics */ private discoverCharacteristics; /** * Forget the connection state for a device: characteristics, encryption session, notification * handlers and any pending reply. */ private dropConnectionState; /** * Watch for the device dropping the link (out of range, powered off, rebooted). Without this the * stale peripheral stays in `connections` and the next command is sent over a dead link. */ private watchForDisconnect; /** * Disconnect from a device. Disconnects started here (idle timeout, disconnectAll, a failed * connection) are expected and logged at debug level; only a disconnect the device starts is * reported as unexpected. */ disconnect(mac: string): Promise; /** * Write data to device */ write(mac: string, data: Buffer): Promise; private readNotification; /** * Read data from device */ read(mac: string): Promise; /** * Check if connected to device */ isConnected(mac: string): boolean; /** * Disconnect all devices */ disconnectAll(): Promise; /** * Get connected device count */ getConnectionCount(): number; } export {}; //# sourceMappingURL=ble.d.ts.map