- Add webBluetooth.ts with browser detection and compatibility checks - Add WebBLEManager implementing IBLEManager for Web Bluetooth API - Add BrowserNotSupported component showing clear error for Safari/Firefox - Update services/ble/index.ts to use WebBLEManager on web platform - Add comprehensive tests for browser detection and WebBLEManager Works in Chrome/Edge/Opera, shows user-friendly error in unsupported browsers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
83 lines
4.0 KiB
TypeScript
83 lines
4.0 KiB
TypeScript
// BLE Service entry point
|
|
|
|
import { Platform } from 'react-native';
|
|
import * as Device from 'expo-device';
|
|
import { IBLEManager } from './types';
|
|
|
|
// Check if running on web platform
|
|
const isWeb = Platform.OS === 'web';
|
|
|
|
// Determine if BLE is available (real device vs simulator)
|
|
// On web, we use Web Bluetooth API which is always "available" (browser will show error if not)
|
|
export const isBLEAvailable = isWeb ? true : Device.isDevice;
|
|
|
|
// Lazy singleton - only create BLEManager when first accessed
|
|
let _bleManager: IBLEManager | null = null;
|
|
|
|
function getBLEManager(): IBLEManager {
|
|
if (!_bleManager) {
|
|
if (isWeb) {
|
|
// Web platform - use Web Bluetooth API
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const { WebBLEManager } = require('./WebBLEManager');
|
|
_bleManager = new WebBLEManager();
|
|
} else if (isBLEAvailable) {
|
|
// Native platform with real BLE (physical device)
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const { RealBLEManager } = require('./BLEManager');
|
|
_bleManager = new RealBLEManager();
|
|
} else {
|
|
// Native platform without real BLE (simulator/emulator)
|
|
// 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),
|
|
// Reconnect functionality
|
|
setReconnectConfig: (config) => getBLEManager().setReconnectConfig(config),
|
|
getReconnectConfig: () => getBLEManager().getReconnectConfig(),
|
|
enableAutoReconnect: (deviceId, deviceName) => getBLEManager().enableAutoReconnect(deviceId, deviceName),
|
|
disableAutoReconnect: (deviceId) => getBLEManager().disableAutoReconnect(deviceId),
|
|
manualReconnect: (deviceId) => getBLEManager().manualReconnect(deviceId),
|
|
getReconnectState: (deviceId) => getBLEManager().getReconnectState(deviceId),
|
|
getAllReconnectStates: () => getBLEManager().getAllReconnectStates(),
|
|
cancelReconnect: (deviceId) => getBLEManager().cancelReconnect(deviceId),
|
|
};
|
|
|
|
// Re-export types
|
|
export * from './types';
|
|
|
|
// Re-export permission utilities
|
|
export * from './permissions';
|
|
|
|
// Re-export error types and utilities
|
|
export * from './errors';
|
|
|
|
// Re-export Web Bluetooth utilities
|
|
export * from './webBluetooth';
|