import {NativeEventEmitter, NativeModules} from 'react-native' const {BluetoothStateManager: NativeBluetoothStateManager} = NativeModules const bluetoothEventEmitter = new NativeEventEmitter(NativeBluetoothStateManager) export type BluetoothState = | 'PoweredOn' | 'PoweredOff' | 'Unauthorized' | 'Unsupported' | 'Resetting' | 'Unknown' export interface BluetoothStateManagerInterface { /** * Get current Bluetooth state * @returns Promise Current Bluetooth state */ getState(): Promise /** * Request to enable Bluetooth * - Android: Shows dialog asking user to enable Bluetooth * - iOS: Not supported, will throw error * @returns Promise */ enable(): Promise /** * Listen for Bluetooth state changes * @param callback Function called when Bluetooth state changes * @returns Function to remove listener */ onStateChange(callback: (state: BluetoothState) => void): () => void } export const BluetoothStateManager: BluetoothStateManagerInterface = { getState: () => NativeBluetoothStateManager.getState(), enable: () => NativeBluetoothStateManager.requestToEnable(), onStateChange: (callback: (state: BluetoothState) => void) => { // Get initial state NativeBluetoothStateManager.getState() .then((currentState: BluetoothState) => { callback(currentState) }) .catch((error: any) => { console.error('[JS] BluetoothStateManager.subscribe - ERROR: Failed to get current state:', error) }) // Add event listener const subscription = bluetoothEventEmitter.addListener('BluetoothStateChanged', callback) // Return cleanup function return () => { subscription.remove() } }, }