Files
trainer/src/utils/bluetoothDiagnostics.ts
2026-01-25 09:56:41 -06:00

109 lines
3.1 KiB
TypeScript

export interface BluetoothDiagnosticResult {
supported: boolean;
available: boolean;
systemCheck?: {
platform: string;
available: boolean;
adapter: string | null;
error: string | null;
guidance: string[];
};
errors: string[];
warnings: string[];
recommendations: string[];
}
export async function checkBluetoothSupport(): Promise<BluetoothDiagnosticResult> {
const result: BluetoothDiagnosticResult = {
supported: false,
available: false,
errors: [],
warnings: [],
recommendations: [],
};
if (!navigator.bluetooth) {
result.errors.push('Web Bluetooth API is not available');
result.recommendations.push('Make sure you are running in a supported browser (Chrome, Edge, Opera, Brave)');
result.recommendations.push('Firefox does not support Web Bluetooth');
return result;
}
result.supported = true;
try {
const available = await navigator.bluetooth.getAvailability();
result.available = available;
if (!available) {
result.errors.push('Bluetooth is not available on this system');
result.recommendations.push('Check if Bluetooth is enabled in your system settings');
result.recommendations.push('Make sure you have a Bluetooth adapter');
}
} catch (error) {
result.warnings.push('Could not check Bluetooth availability');
result.recommendations.push('Try enabling Bluetooth in system settings');
}
if (window.electronAPI) {
try {
const electronCheck = await window.electronAPI.bluetooth.checkAvailability();
if (!electronCheck.flags.webBluetooth) {
result.warnings.push('Web Bluetooth flag is not enabled in Electron');
}
const systemCheck = await window.electronAPI.bluetooth.checkSystem();
result.systemCheck = systemCheck;
if (!systemCheck.available) {
result.errors.push('Bluetooth system service is not available');
result.recommendations.push(...systemCheck.guidance);
}
if (systemCheck.adapter === 'disabled') {
result.warnings.push('Bluetooth adapter is not powered on');
result.recommendations.push(...systemCheck.guidance);
}
if (systemCheck.error) {
result.warnings.push(`System check error: ${systemCheck.error}`);
}
} catch (error) {
result.warnings.push(`Electron diagnostics failed: ${(error as Error).message}`);
}
}
return result;
}
export function formatDiagnosticMessage(result: BluetoothDiagnosticResult): string {
const lines: string[] = [];
lines.push('=== Bluetooth Diagnostics ===\n');
if (result.errors.length > 0) {
lines.push('❌ Errors:');
result.errors.forEach(err => lines.push(`${err}`));
lines.push('');
}
if (result.warnings.length > 0) {
lines.push('⚠️ Warnings:');
result.warnings.forEach(warn => lines.push(`${warn}`));
lines.push('');
}
if (result.recommendations.length > 0) {
lines.push('💡 Recommendations:');
result.recommendations.forEach(rec => lines.push(`${rec}`));
lines.push('');
}
if (result.supported && result.available) {
lines.push('✅ Bluetooth is ready to use');
}
return lines.join('\n');
}