WellNuo/services/ble/MockBLEManager.ts
Sergei 86e73f004d Add BLE infrastructure for sensor connectivity
Core BLE system:
- BLEManager: Real BLE device scanning and connection (iOS/Android)
- MockBLEManager: Simulator-safe mock for development
- BLEContext: React context for BLE state management
- BLEProvider: Added to app/_layout.tsx

Bluetooth permissions:
- iOS: NSBluetoothAlwaysUsageDescription, NSBluetoothPeripheralUsageDescription
- Android: BLUETOOTH, BLUETOOTH_ADMIN, BLUETOOTH_CONNECT, BLUETOOTH_SCAN, ACCESS_FINE_LOCATION

Dependencies:
- react-native-ble-plx@3.5.0
- expo-device@8.0.10
- react-native-base64@0.2.2

Simulator support:
- Auto-detects iOS simulator via expo-device
- Falls back to MockBLEManager with fake devices
- No crashes or permission errors in development
2026-01-14 19:07:44 -08:00

113 lines
2.8 KiB
TypeScript

// Mock BLE Manager для iOS Simulator (Bluetooth недоступен)
import { IBLEManager, WPDevice, WiFiNetwork, WiFiStatus } from './types';
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
export class MockBLEManager implements IBLEManager {
private connectedDevices = new Set<string>();
private mockDevices: WPDevice[] = [
{
id: 'mock-743',
name: 'WP_497_81a14c',
mac: '142B2F81A14C',
rssi: -55,
wellId: 497,
},
{
id: 'mock-769',
name: 'WP_523_81aad4',
mac: '142B2F81AAD4',
rssi: -67,
wellId: 523,
},
];
async scanDevices(): Promise<WPDevice[]> {
console.log('[MockBLE] Scanning for devices...');
await delay(2000); // Simulate scan delay
return this.mockDevices;
}
stopScan(): void {
console.log('[MockBLE] Scan stopped');
}
async connectDevice(deviceId: string): Promise<boolean> {
console.log(`[MockBLE] Connecting to ${deviceId}...`);
await delay(1000);
this.connectedDevices.add(deviceId);
return true;
}
async disconnectDevice(deviceId: string): Promise<void> {
console.log(`[MockBLE] Disconnecting ${deviceId}`);
await delay(500);
this.connectedDevices.delete(deviceId);
}
isDeviceConnected(deviceId: string): boolean {
return this.connectedDevices.has(deviceId);
}
async sendCommand(deviceId: string, command: string): Promise<string> {
console.log(`[MockBLE] Sending command: ${command}`);
await delay(500);
// Simulate responses
if (command === 'pin|7856') {
return 'pin|ok';
}
if (command === 'w') {
return 'mac,142b2f81a14c|w|3|FrontierTower,-55|HomeNetwork,-67|TP-Link_5G,-75';
}
if (command === 'a') {
return 'mac,142b2f81a14c|a|FrontierTower,-67';
}
if (command.startsWith('W|')) {
return 'mac,142b2f81a14c|W|ok';
}
return 'ok';
}
async getWiFiList(deviceId: string): Promise<WiFiNetwork[]> {
console.log(`[MockBLE] Getting WiFi list for ${deviceId}`);
await delay(1500);
return [
{ ssid: 'FrontierTower', rssi: -55 },
{ ssid: 'HomeNetwork', rssi: -67 },
{ ssid: 'TP-Link_5G', rssi: -75 },
{ ssid: 'Office-WiFi', rssi: -80 },
];
}
async setWiFi(
deviceId: string,
ssid: string,
password: string
): Promise<boolean> {
console.log(`[MockBLE] Setting WiFi: ${ssid}`);
await delay(2000);
return true;
}
async getCurrentWiFi(deviceId: string): Promise<WiFiStatus | null> {
console.log(`[MockBLE] Getting current WiFi for ${deviceId}`);
await delay(1000);
return {
ssid: 'FrontierTower',
rssi: -67,
connected: true,
};
}
async rebootDevice(deviceId: string): Promise<void> {
console.log(`[MockBLE] Rebooting ${deviceId}`);
await delay(500);
this.connectedDevices.delete(deviceId);
}
}