Implement comprehensive BLE cleanup functionality that properly disconnects all devices and releases resources when user logs out. Changes: - Add cleanup() method to BLEManager and MockBLEManager - Update IBLEManager interface to include cleanup - Add cleanupBLE() to BLEContext to disconnect all devices - Implement callback mechanism in api.ts for BLE cleanup on logout - Wire up BLE cleanup in app layout to trigger on logout - Add unit tests for BLE cleanup functionality This ensures no BLE connections remain active after logout, preventing resource leaks and potential connection issues. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
44 lines
1.8 KiB
TypeScript
44 lines
1.8 KiB
TypeScript
// 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) {
|
|
console.log('[BLE] Creating BLEManager instance (lazy)...');
|
|
// Dynamic import to prevent crash on Android startup
|
|
if (isBLEAvailable) {
|
|
const { RealBLEManager } = require('./BLEManager');
|
|
_bleManager = new RealBLEManager();
|
|
} else {
|
|
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),
|
|
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(),
|
|
};
|
|
|
|
// Re-export types
|
|
export * from './types';
|