// SPDX-License-Identifier: MIT package com.rnthermalprinter import com.facebook.react.bridge.* import com.facebook.react.module.annotations.ReactModule import com.rnthermalprinter.discovery.BluetoothStateManager /** React Native module for BluetoothStateManager */ @ReactModule(name = BluetoothStateManagerModule.NAME) class BluetoothStateManagerModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { companion object { const val NAME = "BluetoothStateManager" } private val bluetoothStateManager = BluetoothStateManager(reactContext) private var listenerCount = 0 override fun getName() = NAME // Handle JS listeners - auto start/stop monitoring @ReactMethod fun addListener(eventName: String) { listenerCount += 1 if (listenerCount == 1) { // First listener, start monitoring bluetoothStateManager.startListening() } } @ReactMethod fun removeListeners(count: Int) { listenerCount -= count if (listenerCount <= 0) { listenerCount = 0 // No more listeners, stop monitoring bluetoothStateManager.stopListening() } } @ReactMethod fun getState(promise: Promise) { bluetoothStateManager.getState(promise) } @ReactMethod fun requestToEnable(promise: Promise) { bluetoothStateManager.requestToEnable(promise) } override fun onCatalystInstanceDestroy() { super.onCatalystInstanceDestroy() bluetoothStateManager.onDestroy() } }