All files / services/ble index.ts

0% Statements 0/30
0% Branches 0/4
0% Functions 0/21
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                                                                                 
// BLE Service entry point
 
import * as Device from 'expo-device';
import { IBLEManager } from './types';
 
// Determine if BLE is available (real device vs simulator)
export const isBLEAvailable = Device.isDevice;
 
// Lazy singleton - only create BLEManager when first accessed
let _bleManager: IBLEManager | null = null;
 
function getBLEManager(): IBLEManager {
  if (!_bleManager) {
    // Dynamic import to prevent crash on Android startup
    if (isBLEAvailable) {
      // eslint-disable-next-line @typescript-eslint/no-require-imports
      const { RealBLEManager } = require('./BLEManager');
      _bleManager = new RealBLEManager();
    } else {
      // eslint-disable-next-line @typescript-eslint/no-require-imports
      const { MockBLEManager } = require('./MockBLEManager');
      _bleManager = new MockBLEManager();
    }
  }
  return _bleManager!; // Non-null assertion - we just assigned it above
}
 
// Proxy object that lazily initializes the real manager
export const bleManager: IBLEManager = {
  scanDevices: () => getBLEManager().scanDevices(),
  stopScan: () => getBLEManager().stopScan(),
  connectDevice: (deviceId: string) => getBLEManager().connectDevice(deviceId),
  disconnectDevice: (deviceId: string) => getBLEManager().disconnectDevice(deviceId),
  isDeviceConnected: (deviceId: string) => getBLEManager().isDeviceConnected(deviceId),
  getConnectionState: (deviceId: string) => getBLEManager().getConnectionState(deviceId),
  getAllConnections: () => getBLEManager().getAllConnections(),
  addEventListener: (listener) => getBLEManager().addEventListener(listener),
  removeEventListener: (listener) => getBLEManager().removeEventListener(listener),
  sendCommand: (deviceId: string, command: string) => getBLEManager().sendCommand(deviceId, command),
  getWiFiList: (deviceId: string) => getBLEManager().getWiFiList(deviceId),
  setWiFi: (deviceId: string, ssid: string, password: string) => getBLEManager().setWiFi(deviceId, ssid, password),
  getCurrentWiFi: (deviceId: string) => getBLEManager().getCurrentWiFi(deviceId),
  rebootDevice: (deviceId: string) => getBLEManager().rebootDevice(deviceId),
  cleanup: () => getBLEManager().cleanup(),
  getSensorHealth: (wellId: number, mac: string) => getBLEManager().getSensorHealth(wellId, mac),
  getAllSensorHealth: () => getBLEManager().getAllSensorHealth(),
  bulkDisconnect: (deviceIds: string[]) => getBLEManager().bulkDisconnect(deviceIds),
  bulkReboot: (deviceIds: string[]) => getBLEManager().bulkReboot(deviceIds),
  bulkSetWiFi: (devices, ssid, password, onProgress) => getBLEManager().bulkSetWiFi(devices, ssid, password, onProgress),
};
 
// Re-export types
export * from './types';
 
// Re-export permission utilities
export * from './permissions';