From 7e72d896519e4562f254801f1d427447ed4d4de7 Mon Sep 17 00:00:00 2001 From: 0xloem <0xloem@gmail.com> Date: Thu, 17 Dec 2020 06:23:48 -0500 Subject: [PATCH] tell the user if bluetooth is missing --- demo/src/main.ts | 5 +++-- src/muse.ts | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/demo/src/main.ts b/demo/src/main.ts index 5ae4200..19b6c50 100644 --- a/demo/src/main.ts +++ b/demo/src/main.ts @@ -23,7 +23,7 @@ import { channelNames, EEGReading, MuseClient } from './../../src/muse'; context.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < reading.samples.length; i++) { - const sample = reading.samples[i] / 15.; + const sample = reading.samples[i] / 15; if (sample > 0) { context.fillRect(i * 25, height - sample, width, sample); } else { @@ -50,7 +50,7 @@ import { channelNames, EEGReading, MuseClient } from './../../src/muse'; document.getElementById('batteryLevel')!.innerText = reading.batteryLevel.toFixed(2) + '%'; }); client.accelerometerData.subscribe((accel) => { - const normalize = (v: number) => (v / 16384.).toFixed(2) + 'g'; + const normalize = (v: number) => (v / 16384).toFixed(2) + 'g'; document.getElementById('accelerometer-x')!.innerText = normalize(accel.samples[2].x); document.getElementById('accelerometer-y')!.innerText = normalize(accel.samples[2].y); document.getElementById('accelerometer-z')!.innerText = normalize(accel.samples[2].z); @@ -61,5 +61,6 @@ import { channelNames, EEGReading, MuseClient } from './../../src/muse'; }); } catch (err) { console.error('Connection failed', err); + document.getElementById('headset-name')!.innerText = 'Connection failed ' + err; } }; diff --git a/src/muse.ts b/src/muse.ts index 3de6b54..82402f9 100644 --- a/src/muse.ts +++ b/src/muse.ts @@ -57,6 +57,9 @@ export class MuseClient { if (gatt) { this.gatt = gatt; } else { + if (!navigator.bluetooth) { + throw new Error('Bluetooth support required'); + } const device = await navigator.bluetooth.requestDevice({ filters: [{ services: [MUSE_SERVICE] }], }); @@ -143,7 +146,12 @@ export class MuseClient { } async deviceInfo() { - const resultListener = this.controlResponses.pipe(filter((r) => !!r.fw), take(1)).toPromise(); + const resultListener = this.controlResponses + .pipe( + filter((r) => !!r.fw), + take(1), + ) + .toPromise(); await this.sendCommand('v1'); return resultListener as Promise; }