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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
return (
|
| 6 |
<div className="App">
|
| 7 |
<header className="App-header">
|
| 8 |
-
<
|
| 9 |
-
<
|
| 10 |
-
|
| 11 |
-
</p>
|
| 12 |
-
<a
|
| 13 |
-
className="App-link"
|
| 14 |
-
href="https://reactjs.org"
|
| 15 |
-
target="_blank"
|
| 16 |
-
rel="noopener noreferrer"
|
| 17 |
-
>
|
| 18 |
-
Learn React
|
| 19 |
-
</a>
|
| 20 |
</header>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
</div>
|
| 22 |
);
|
| 23 |
-
}
|
| 24 |
|
| 25 |
export default App;
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
import './App.css';
|
| 3 |
|
| 4 |
+
const App = () => {
|
| 5 |
+
const [isScanning, setIsScanning] = useState(false);
|
| 6 |
+
const [scanResults, setScanResults] = useState(null);
|
| 7 |
+
const [error, setError] = useState(null);
|
| 8 |
+
const [selectedSense, setSelectedSense] = useState('');
|
| 9 |
+
const [connectedDevice, setConnectedDevice] = useState(null);
|
| 10 |
+
|
| 11 |
+
const senses = {
|
| 12 |
+
aural: {
|
| 13 |
+
name: 'Aural Sense',
|
| 14 |
+
description: 'Scanning for audio/ear biofeedback sensors...',
|
| 15 |
+
icon: '👂',
|
| 16 |
+
color: '#FF6B6B'
|
| 17 |
+
},
|
| 18 |
+
neural: {
|
| 19 |
+
name: 'Neural Sense',
|
| 20 |
+
description: 'Scanning for brainwave/neural activity sensors...',
|
| 21 |
+
icon: '🧠',
|
| 22 |
+
color: '#4ECDC4'
|
| 23 |
+
},
|
| 24 |
+
mental: {
|
| 25 |
+
name: 'Mental Sense',
|
| 26 |
+
description: 'Scanning for cognitive/mental state sensors...',
|
| 27 |
+
icon: '🧘',
|
| 28 |
+
color: '#45B7D1'
|
| 29 |
+
}
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
const checkBluetoothSupport = () => {
|
| 33 |
+
if (!navigator.bluetooth) {
|
| 34 |
+
setError('Web Bluetooth API not supported. Please use Chrome, Edge, or Opera browser.');
|
| 35 |
+
return false;
|
| 36 |
+
}
|
| 37 |
+
return true;
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
const scanBluetooth = async (senseKey) => {
|
| 41 |
+
if (!checkBluetoothSupport()) return;
|
| 42 |
+
|
| 43 |
+
setIsScanning(true);
|
| 44 |
+
setError(null);
|
| 45 |
+
setSelectedSense(senseKey);
|
| 46 |
+
setScanResults(null);
|
| 47 |
+
setConnectedDevice(null);
|
| 48 |
+
|
| 49 |
+
try {
|
| 50 |
+
console.log(`Starting ${senses[senseKey].name} scan...`);
|
| 51 |
+
|
| 52 |
+
// Request Bluetooth device with filters
|
| 53 |
+
const device = await navigator.bluetooth.requestDevice({
|
| 54 |
+
acceptAllDevices: true,
|
| 55 |
+
optionalServices: [
|
| 56 |
+
'battery_service',
|
| 57 |
+
'device_information',
|
| 58 |
+
'heart_rate',
|
| 59 |
+
'0000180f-0000-1000-8000-00805f9b34fb' // Battery Service UUID
|
| 60 |
+
]
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
if (!device) {
|
| 64 |
+
throw new Error('No device selected by user.');
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
console.log(`Connecting to device: ${device.name || device.id}`);
|
| 68 |
+
setConnectedDevice(device.name || `Device ${device.id.slice(-4)}`);
|
| 69 |
+
|
| 70 |
+
// Connect to GATT server
|
| 71 |
+
const server = await device.gatt.connect();
|
| 72 |
+
console.log('Connected to GATT server');
|
| 73 |
+
|
| 74 |
+
let senseData = {};
|
| 75 |
+
|
| 76 |
+
try {
|
| 77 |
+
// Try to read battery service as mock sensor data
|
| 78 |
+
const batteryService = await server.getPrimaryService('battery_service');
|
| 79 |
+
const batteryCharacteristic = await batteryService.getCharacteristic('battery_level');
|
| 80 |
+
const batteryValue = await batteryCharacteristic.readValue();
|
| 81 |
+
const batteryLevel = batteryValue.getUint8(0);
|
| 82 |
+
|
| 83 |
+
// Generate mock sense data based on battery level and sense type
|
| 84 |
+
senseData = generateMockSenseData(senseKey, batteryLevel);
|
| 85 |
+
|
| 86 |
+
} catch (serviceError) {
|
| 87 |
+
console.log('Battery service not available, generating random data');
|
| 88 |
+
// If no battery service, generate random mock data
|
| 89 |
+
senseData = generateMockSenseData(senseKey, Math.floor(Math.random() * 100));
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// Simulate processing delay
|
| 93 |
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
| 94 |
+
|
| 95 |
+
const scanResult = {
|
| 96 |
+
deviceName: device.name || `BLE Device ${device.id.slice(-4)}`,
|
| 97 |
+
deviceId: device.id,
|
| 98 |
+
senseType: senses[senseKey].name,
|
| 99 |
+
...senseData,
|
| 100 |
+
status: 'Scan Complete',
|
| 101 |
+
timestamp: new Date().toLocaleTimeString(),
|
| 102 |
+
connection: 'Bluetooth Low Energy'
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
setScanResults(scanResult);
|
| 106 |
+
|
| 107 |
+
// Disconnect after scan
|
| 108 |
+
await server.disconnect();
|
| 109 |
+
console.log('Disconnected from device');
|
| 110 |
+
|
| 111 |
+
} catch (err) {
|
| 112 |
+
console.error('Scan error:', err);
|
| 113 |
+
let errorMessage = 'Scan failed. ';
|
| 114 |
+
|
| 115 |
+
if (err.name === 'NotFoundError') {
|
| 116 |
+
errorMessage += 'No compatible devices found. Make sure your device is discoverable.';
|
| 117 |
+
} else if (err.name === 'SecurityError') {
|
| 118 |
+
errorMessage += 'Permission denied. Please allow Bluetooth access.';
|
| 119 |
+
} else if (err.name === 'NetworkError') {
|
| 120 |
+
errorMessage += 'Connection failed. Check if device is in range.';
|
| 121 |
+
} else {
|
| 122 |
+
errorMessage += err.message;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
setError(errorMessage);
|
| 126 |
+
} finally {
|
| 127 |
+
setIsScanning(false);
|
| 128 |
+
setConnectedDevice(null);
|
| 129 |
+
}
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
const generateMockSenseData = (senseKey, baseValue) => {
|
| 133 |
+
const timestamp = Date.now();
|
| 134 |
+
|
| 135 |
+
switch (senseKey) {
|
| 136 |
+
case 'aural':
|
| 137 |
+
return {
|
| 138 |
+
hearingLevel: `${Math.max(10, Math.min(100, baseValue + Math.floor(Math.random() * 20 - 10)))}%`,
|
| 139 |
+
frequency: `${440 + Math.floor(Math.random() * 1000)}Hz`,
|
| 140 |
+
soundPressure: `${60 + Math.floor(Math.random() * 40)}dB`,
|
| 141 |
+
leftEar: `${Math.floor(Math.random() * 100)}%`,
|
| 142 |
+
rightEar: `${Math.floor(Math.random() * 100)}%`
|
| 143 |
+
};
|
| 144 |
+
|
| 145 |
+
case 'neural':
|
| 146 |
+
return {
|
| 147 |
+
brainwaveLevel: `${Math.max(5, Math.min(95, baseValue + Math.floor(Math.random() * 30 - 15)))}%`,
|
| 148 |
+
alpha: `${8 + Math.random() * 4}Hz`,
|
| 149 |
+
beta: `${13 + Math.random() * 17}Hz`,
|
| 150 |
+
theta: `${4 + Math.random() * 4}Hz`,
|
| 151 |
+
gamma: `${30 + Math.random() * 70}Hz`,
|
| 152 |
+
focus: Math.random() > 0.5 ? 'High' : 'Medium'
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
case 'mental':
|
| 156 |
+
return {
|
| 157 |
+
mentalState: `${Math.max(20, Math.min(100, baseValue + Math.floor(Math.random() * 25 - 12)))}%`,
|
| 158 |
+
stressLevel: Math.random() > 0.6 ? 'Low' : Math.random() > 0.3 ? 'Medium' : 'High',
|
| 159 |
+
relaxation: `${Math.floor(Math.random() * 100)}%`,
|
| 160 |
+
attention: `${Math.floor(Math.random() * 100)}%`,
|
| 161 |
+
meditation: Math.random() > 0.5 ? 'Active' : 'Passive'
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
default:
|
| 165 |
+
return { level: `${baseValue}%` };
|
| 166 |
+
}
|
| 167 |
+
};
|
| 168 |
+
|
| 169 |
+
const getSenseButton = (key) => (
|
| 170 |
+
<button
|
| 171 |
+
key={key}
|
| 172 |
+
onClick={() => scanBluetooth(key)}
|
| 173 |
+
disabled={isScanning}
|
| 174 |
+
className="sense-button"
|
| 175 |
+
style={{ backgroundColor: isScanning && selectedSense === key ? '#6c757d' : senses[key].color }}
|
| 176 |
+
>
|
| 177 |
+
<span className="button-icon">{senses[key].icon}</span>
|
| 178 |
+
{isScanning && selectedSense === key ? 'Scanning...' : `Scan ${senses[key].name}`}
|
| 179 |
+
</button>
|
| 180 |
+
);
|
| 181 |
+
|
| 182 |
+
const clearResults = () => {
|
| 183 |
+
setScanResults(null);
|
| 184 |
+
setError(null);
|
| 185 |
+
setSelectedSense('');
|
| 186 |
+
};
|
| 187 |
+
|
| 188 |
return (
|
| 189 |
<div className="App">
|
| 190 |
<header className="App-header">
|
| 191 |
+
<h1>nCONNECT OS</h1>
|
| 192 |
+
<h2>Sense Scanner</h2>
|
| 193 |
+
<p>Advanced Bluetooth Bio-Sensor Interface</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
</header>
|
| 195 |
+
|
| 196 |
+
<main className="App-main">
|
| 197 |
+
<div className="scan-info">
|
| 198 |
+
<p>Select a sense type to scan for compatible Bluetooth devices:</p>
|
| 199 |
+
</div>
|
| 200 |
+
|
| 201 |
+
<div className="senses-grid">
|
| 202 |
+
{Object.keys(senses).map(key => (
|
| 203 |
+
<div key={key} className="sense-card">
|
| 204 |
+
<div className="sense-icon" style={{ color: senses[key].color }}>
|
| 205 |
+
{senses[key].icon}
|
| 206 |
+
</div>
|
| 207 |
+
<h3>{senses[key].name}</h3>
|
| 208 |
+
<p>{senses[key].description}</p>
|
| 209 |
+
{getSenseButton(key)}
|
| 210 |
+
</div>
|
| 211 |
+
))}
|
| 212 |
+
</div>
|
| 213 |
+
|
| 214 |
+
{isScanning && (
|
| 215 |
+
<div className="scanning-status">
|
| 216 |
+
<div className="loading-spinner"></div>
|
| 217 |
+
<div className="loading-text">
|
| 218 |
+
<p>Scanning for {senses[selectedSense]?.name} devices...</p>
|
| 219 |
+
{connectedDevice && <p>Connected to: {connectedDevice}</p>}
|
| 220 |
+
</div>
|
| 221 |
+
</div>
|
| 222 |
+
)}
|
| 223 |
+
|
| 224 |
+
{error && (
|
| 225 |
+
<div className="error-container">
|
| 226 |
+
<div className="error">
|
| 227 |
+
<strong>Error:</strong> {error}
|
| 228 |
+
</div>
|
| 229 |
+
<button onClick={clearResults} className="retry-button">
|
| 230 |
+
Try Again
|
| 231 |
+
</button>
|
| 232 |
+
</div>
|
| 233 |
+
)}
|
| 234 |
+
|
| 235 |
+
{scanResults && (
|
| 236 |
+
<div className="results-container">
|
| 237 |
+
<div className="results">
|
| 238 |
+
<h3>📊 Scan Results</h3>
|
| 239 |
+
<div className="result-header">
|
| 240 |
+
<strong>{scanResults.senseType}</strong>
|
| 241 |
+
<span className="status-badge">{scanResults.status}</span>
|
| 242 |
+
</div>
|
| 243 |
+
|
| 244 |
+
<div className="result-details">
|
| 245 |
+
<div className="detail-row">
|
| 246 |
+
<span className="label">Device:</span>
|
| 247 |
+
<span className="value">{scanResults.deviceName}</span>
|
| 248 |
+
</div>
|
| 249 |
+
<div className="detail-row">
|
| 250 |
+
<span className="label">Connection:</span>
|
| 251 |
+
<span className="value">{scanResults.connection}</span>
|
| 252 |
+
</div>
|
| 253 |
+
<div className="detail-row">
|
| 254 |
+
<span className="label">Time:</span>
|
| 255 |
+
<span className="value">{scanResults.timestamp}</span>
|
| 256 |
+
</div>
|
| 257 |
+
</div>
|
| 258 |
+
|
| 259 |
+
<div className="sensor-data">
|
| 260 |
+
<h4>Sensor Readings:</h4>
|
| 261 |
+
{Object.entries(scanResults).map(([key, value]) => {
|
| 262 |
+
if (['deviceName', 'deviceId', 'senseType', 'status', 'timestamp', 'connection'].includes(key)) {
|
| 263 |
+
return null;
|
| 264 |
+
}
|
| 265 |
+
return (
|
| 266 |
+
<div key={key} className="sensor-reading">
|
| 267 |
+
<span className="sensor-label">{key.charAt(0).toUpperCase() + key.slice(1)}:</span>
|
| 268 |
+
<span className="sensor-value">{value}</span>
|
| 269 |
+
</div>
|
| 270 |
+
);
|
| 271 |
+
})}
|
| 272 |
+
</div>
|
| 273 |
+
|
| 274 |
+
<div className="result-actions">
|
| 275 |
+
<button onClick={clearResults} className="clear-button">
|
| 276 |
+
Clear Results
|
| 277 |
+
</button>
|
| 278 |
+
<button onClick={() => scanBluetooth(selectedSense)} className="rescan-button">
|
| 279 |
+
Scan Again
|
| 280 |
+
</button>
|
| 281 |
+
</div>
|
| 282 |
+
</div>
|
| 283 |
+
</div>
|
| 284 |
+
)}
|
| 285 |
+
</main>
|
| 286 |
+
|
| 287 |
+
<footer className="App-footer">
|
| 288 |
+
<div className="footer-content">
|
| 289 |
+
<p><strong>Copyright © nanoSOFT TECH</strong></p>
|
| 290 |
+
<p>Operating System: <strong>nCONNECT OS</strong></p>
|
| 291 |
+
<p className="version">Version 1.0.0 | Bluetooth BLE Compatible</p>
|
| 292 |
+
</div>
|
| 293 |
+
</footer>
|
| 294 |
</div>
|
| 295 |
);
|
| 296 |
+
};
|
| 297 |
|
| 298 |
export default App;
|