From d70d66e3dc9ffa7edd486b86ae5ca2ade159a61d Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 18 Mar 2019 14:57:41 -0700 Subject: [PATCH] Working example of web bluetooth --- docs/BoostMoveHub.html | 16 +++--- docs/DuploTrainBase.html | 12 ++-- docs/Hub.html | 16 +++--- docs/LPF2Hub.html | 16 +++--- docs/PUPHub.html | 16 +++--- docs/PUPRemote.html | 12 ++-- docs/PoweredUP.html | 14 ++--- docs/WeDo2SmartHub.html | 16 +++--- docs/classes.list.html | 22 ++++---- docs/consts.js.html | 18 ++++++ docs/global.html | 16 +++--- docs/hub.js.html | 3 +- docs/poweredup-node.js.html | 20 +------ docs/quicksearch.html | 2 +- docs/wedo2smarthub.js.html | 6 +- test.css => examples/web_bluetooth.css | 27 +++++++++ examples/web_bluetooth.html | 78 ++++++++++++++++++++++++++ src/consts.ts | 24 ++++++++ src/hub.ts | 3 +- src/index-browser.ts | 4 +- src/nobledevice.ts | 5 +- src/poweredup-node.ts | 12 +--- src/utils.ts | 2 +- src/wedo2smarthub.ts | 8 +-- test.html | 47 ---------------- 25 files changed, 245 insertions(+), 170 deletions(-) rename test.css => examples/web_bluetooth.css (51%) create mode 100644 examples/web_bluetooth.html delete mode 100644 test.html diff --git a/docs/BoostMoveHub.html b/docs/BoostMoveHub.html index 49fd8c8..acbbfab 100644 --- a/docs/BoostMoveHub.html +++ b/docs/BoostMoveHub.html @@ -1502,7 +1502,7 @@ @@ -1674,7 +1674,7 @@ @@ -3499,7 +3499,7 @@ @@ -3719,7 +3719,7 @@ @@ -3895,7 +3895,7 @@ @@ -4072,7 +4072,7 @@ @@ -4280,7 +4280,7 @@ @@ -4969,7 +4969,7 @@ diff --git a/docs/DuploTrainBase.html b/docs/DuploTrainBase.html index e1c9657..788b317 100644 --- a/docs/DuploTrainBase.html +++ b/docs/DuploTrainBase.html @@ -1502,7 +1502,7 @@ @@ -1674,7 +1674,7 @@ @@ -3162,7 +3162,7 @@ @@ -3382,7 +3382,7 @@ @@ -3558,7 +3558,7 @@ @@ -3735,7 +3735,7 @@ diff --git a/docs/Hub.html b/docs/Hub.html index 4442788..60e4cb5 100644 --- a/docs/Hub.html +++ b/docs/Hub.html @@ -1208,7 +1208,7 @@ @@ -1366,7 +1366,7 @@ @@ -1525,7 +1525,7 @@ @@ -1731,7 +1731,7 @@ @@ -1893,7 +1893,7 @@ @@ -2056,7 +2056,7 @@ @@ -2250,7 +2250,7 @@ @@ -2388,7 +2388,7 @@ diff --git a/docs/LPF2Hub.html b/docs/LPF2Hub.html index 1bf9dac..a3b7cdc 100644 --- a/docs/LPF2Hub.html +++ b/docs/LPF2Hub.html @@ -1294,7 +1294,7 @@ @@ -1461,7 +1461,7 @@ @@ -2275,7 +2275,7 @@ @@ -2490,7 +2490,7 @@ @@ -2661,7 +2661,7 @@ @@ -2833,7 +2833,7 @@ @@ -3036,7 +3036,7 @@ @@ -3693,7 +3693,7 @@ diff --git a/docs/PUPHub.html b/docs/PUPHub.html index a055c2a..04fb070 100644 --- a/docs/PUPHub.html +++ b/docs/PUPHub.html @@ -1502,7 +1502,7 @@ @@ -1674,7 +1674,7 @@ @@ -3242,7 +3242,7 @@ @@ -3462,7 +3462,7 @@ @@ -3638,7 +3638,7 @@ @@ -3815,7 +3815,7 @@ @@ -4023,7 +4023,7 @@ @@ -4712,7 +4712,7 @@ diff --git a/docs/PUPRemote.html b/docs/PUPRemote.html index ec5551f..1d828c2 100644 --- a/docs/PUPRemote.html +++ b/docs/PUPRemote.html @@ -1340,7 +1340,7 @@ @@ -1512,7 +1512,7 @@ @@ -2359,7 +2359,7 @@ @@ -2579,7 +2579,7 @@ @@ -2755,7 +2755,7 @@ @@ -2932,7 +2932,7 @@ diff --git a/docs/PoweredUP.html b/docs/PoweredUP.html index d2d1ce3..6e25b6f 100644 --- a/docs/PoweredUP.html +++ b/docs/PoweredUP.html @@ -149,7 +149,7 @@ @@ -316,7 +316,7 @@ @@ -427,7 +427,7 @@ @@ -585,7 +585,7 @@ @@ -693,7 +693,7 @@ @@ -781,7 +781,7 @@ @@ -939,7 +939,7 @@ diff --git a/docs/WeDo2SmartHub.html b/docs/WeDo2SmartHub.html index cd2ba69..8b4b380 100644 --- a/docs/WeDo2SmartHub.html +++ b/docs/WeDo2SmartHub.html @@ -1460,7 +1460,7 @@ @@ -1627,7 +1627,7 @@ @@ -3337,7 +3337,7 @@ @@ -3552,7 +3552,7 @@ @@ -3723,7 +3723,7 @@ @@ -3895,7 +3895,7 @@ @@ -4098,7 +4098,7 @@ @@ -4569,7 +4569,7 @@ diff --git a/docs/classes.list.html b/docs/classes.list.html index 14f4f46..3c350f1 100644 --- a/docs/classes.list.html +++ b/docs/classes.list.html @@ -335,7 +335,7 @@ @@ -1024,7 +1024,7 @@ @@ -2065,7 +2065,7 @@ @@ -2203,7 +2203,7 @@ @@ -2374,7 +2374,7 @@ @@ -3031,7 +3031,7 @@ @@ -3853,7 +3853,7 @@ @@ -4029,7 +4029,7 @@ @@ -4718,7 +4718,7 @@ @@ -5621,7 +5621,7 @@ @@ -6092,7 +6092,7 @@ diff --git a/docs/consts.js.html b/docs/consts.js.html index 7630ee5..627e1f4 100644 --- a/docs/consts.js.html +++ b/docs/consts.js.html @@ -104,6 +104,12 @@ var HubType; HubType[HubType["POWERED_UP_REMOTE"] = 4] = "POWERED_UP_REMOTE"; HubType[HubType["DUPLO_TRAIN_HUB"] = 5] = "DUPLO_TRAIN_HUB"; })(HubType = exports.HubType || (exports.HubType = {})); +// tslint:disable-next-line +exports.HubTypeNames = Object.keys(HubType).reduce((result, item) => { + // @ts-ignore + result[HubType[item]] = item; + return result; +}, {}); /** * @typedef DeviceType * @property {number} UNKNOWN 0 @@ -142,6 +148,12 @@ var DeviceType; DeviceType[DeviceType["DUPLO_TRAIN_BASE_SPEEDOMETER"] = 44] = "DUPLO_TRAIN_BASE_SPEEDOMETER"; DeviceType[DeviceType["POWERED_UP_REMOTE_BUTTON"] = 55] = "POWERED_UP_REMOTE_BUTTON"; })(DeviceType = exports.DeviceType || (exports.DeviceType = {})); +// tslint:disable-next-line +exports.DeviceTypeNames = Object.keys(DeviceType).reduce((result, item) => { + // @ts-ignore + result[DeviceType[item]] = item; + return result; +}, {}); /** * @typedef Color * @property {number} BLACK 0 @@ -172,6 +184,12 @@ var Color; Color[Color["WHITE"] = 10] = "WHITE"; Color[Color["NONE"] = 255] = "NONE"; })(Color = exports.Color || (exports.Color = {})); +// tslint:disable-next-line +exports.ColorNames = Object.keys(Color).reduce((result, item) => { + // @ts-ignore + result[Color[item]] = item; + return result; +}, {}); /** * @typedef ButtonState * @property {number} PRESSED 0 diff --git a/docs/global.html b/docs/global.html index ae65fe9..3aa556a 100644 --- a/docs/global.html +++ b/docs/global.html @@ -356,7 +356,7 @@ @@ -571,7 +571,7 @@ @@ -954,7 +954,7 @@ @@ -1337,7 +1337,7 @@ @@ -1816,7 +1816,7 @@ @@ -2295,7 +2295,7 @@ @@ -2510,7 +2510,7 @@ @@ -2725,7 +2725,7 @@ diff --git a/docs/hub.js.html b/docs/hub.js.html index 3d1be18..a7035a0 100644 --- a/docs/hub.js.html +++ b/docs/hub.js.html @@ -195,7 +195,8 @@ class Hub extends events_1.EventEmitter { * @returns {Promise} Resolved upon successful disconnect. */ async disconnect() { - await this._bleDevice.disconnect(); + this.emit("disconnect"); + this._bleDevice.disconnect(); } /** * Subscribe to sensor notifications on a given port. diff --git a/docs/poweredup-node.js.html b/docs/poweredup-node.js.html index 63914e5..9f092ff 100644 --- a/docs/poweredup-node.js.html +++ b/docs/poweredup-node.js.html @@ -85,13 +85,6 @@
"use strict";
-var __importStar = (this && this.__importStar) || function (mod) {
-    if (mod && mod.__esModule) return mod;
-    var result = {};
-    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
-    result["default"] = mod;
-    return result;
-};
 Object.defineProperty(exports, "__esModule", { value: true });
 const boostmovehub_1 = require("./boostmovehub");
 const duplotrainbase_1 = require("./duplotrainbase");
@@ -99,8 +92,6 @@ const nobledevice_1 = require("./nobledevice");
 const puphub_1 = require("./puphub");
 const pupremote_1 = require("./pupremote");
 const wedo2smarthub_1 = require("./wedo2smarthub");
-const utils_1 = require("./utils");
-const Consts = __importStar(require("./consts"));
 const events_1 = require("events");
 const Debug = require("debug");
 const debug = Debug("poweredup");
@@ -109,12 +100,7 @@ let ready = false;
 let wantScan = false;
 let discoveryEventAttached = false;
 const startScanning = () => {
-    if (utils_1.isBrowserContext) {
-        noble.startScanning([Consts.BLEService.WEDO2_SMART_HUB, Consts.BLEService.LPF2_HUB]);
-    }
-    else {
-        noble.startScanning();
-    }
+    noble.startScanning();
 };
 noble.on("stateChange", (state) => {
     ready = (state === "poweredOn");
@@ -214,10 +200,6 @@ class PoweredUP extends events_1.EventEmitter {
             return;
         }
         peripheral.removeAllListeners();
-        // noble.stopScanning();
-        // if (!isBrowserContext) {
-        //     startScanning();
-        // }
         device.on("discoverComplete", () => {
             hub.on("connect", () => {
                 debug(`Hub ${hub.uuid} connected`);
diff --git a/docs/quicksearch.html b/docs/quicksearch.html
index 6d29a66..a2e2fda 100644
--- a/docs/quicksearch.html
+++ b/docs/quicksearch.html
@@ -7,7 +7,7 @@
     
 
     
 
     
+
+
+
+
+
+

Web Bluetooth node-poweredup Example

+
+Add new Hub +
+
+Current Color:
 
+
+
+ + +
NameType
+
+ + + \ No newline at end of file diff --git a/src/consts.ts b/src/consts.ts index eac0348..bd75a3a 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -17,6 +17,14 @@ export enum HubType { } +// tslint:disable-next-line +export let HubTypeNames = Object.keys(HubType).reduce((result: {[hubType: string]: string}, item) => { + // @ts-ignore + result[HubType[item]] = item; + return result; +}, {}); + + /** * @typedef DeviceType * @property {number} UNKNOWN 0 @@ -56,6 +64,14 @@ export enum DeviceType { } +// tslint:disable-next-line +export let DeviceTypeNames = Object.keys(DeviceType).reduce((result: {[deviceType: string]: string}, item) => { + // @ts-ignore + result[DeviceType[item]] = item; + return result; +}, {}); + + /** * @typedef Color * @property {number} BLACK 0 @@ -87,6 +103,14 @@ export enum Color { } +// tslint:disable-next-line +export let ColorNames = Object.keys(Color).reduce((result: {[color: string]: string}, item) => { + // @ts-ignore + result[Color[item]] = item; + return result; +}, {}); + + /** * @typedef ButtonState * @property {number} PRESSED 0 diff --git a/src/hub.ts b/src/hub.ts index 80b2eed..bc135d8 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -133,7 +133,8 @@ export class Hub extends EventEmitter { * @returns {Promise} Resolved upon successful disconnect. */ public async disconnect () { - await this._bleDevice.disconnect(); + this.emit("disconnect"); + this._bleDevice.disconnect(); } diff --git a/src/index-browser.ts b/src/index-browser.ts index 4bed82a..cc455f3 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -7,5 +7,7 @@ import { PUPHub } from "./puphub"; import { PUPRemote } from "./pupremote"; import { WeDo2SmartHub } from "./wedo2smarthub"; +import { isWebBluetooth } from "./utils"; + // @ts-ignore -window.PoweredUP = { PoweredUP, Hub, WeDo2SmartHub, BoostMoveHub, PUPHub, PUPRemote, DuploTrainBase, Consts }; +window.PoweredUP = { PoweredUP, Hub, WeDo2SmartHub, BoostMoveHub, PUPHub, PUPRemote, DuploTrainBase, Consts, isWebBluetooth }; diff --git a/src/nobledevice.ts b/src/nobledevice.ts index ceed5de..d722208 100644 --- a/src/nobledevice.ts +++ b/src/nobledevice.ts @@ -74,9 +74,8 @@ export class NobleDevice extends EventEmitter implements IBLEDevice { public disconnect () { return new Promise((resolve, reject) => { - this._noblePeripheral.connect((err: string) => { - return resolve(); - }); + this._noblePeripheral.disconnect(); + return resolve(); }); } diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index d905020..e02e2af 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -8,8 +8,6 @@ import { PUPHub } from "./puphub"; import { PUPRemote } from "./pupremote"; import { WeDo2SmartHub } from "./wedo2smarthub"; -import { isBrowserContext } from "./utils"; - import * as Consts from "./consts"; import { EventEmitter } from "events"; @@ -23,11 +21,7 @@ let wantScan = false; let discoveryEventAttached = false; const startScanning = () => { - if (isBrowserContext) { - noble.startScanning([Consts.BLEService.WEDO2_SMART_HUB, Consts.BLEService.LPF2_HUB]); - } else { - noble.startScanning(); - } + noble.startScanning(); }; noble.on("stateChange", (state: string) => { @@ -149,10 +143,6 @@ export class PoweredUP extends EventEmitter { } peripheral.removeAllListeners(); - // noble.stopScanning(); - // if (!isBrowserContext) { - // startScanning(); - // } device.on("discoverComplete", () => { diff --git a/src/utils.ts b/src/utils.ts index 7d124f4..629b5ef 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,2 +1,2 @@ // @ts-ignore -export const isBrowserContext = (typeof navigator !== "undefined" && navigator && navigator.bluetooth); +export const isWebBluetooth = (typeof navigator !== "undefined" && navigator && navigator.bluetooth); diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index c43cde1..6cd5537 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -7,7 +7,7 @@ import * as Consts from "./consts"; import Debug = require("debug"); import { IBLEDevice } from "./interfaces"; -import { isBrowserContext } from "./utils"; +import { isWebBluetooth } from "./utils"; const debug = Debug("wedo2smarthub"); @@ -55,7 +55,7 @@ export class WeDo2SmartHub extends Hub { await super.connect(); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_2); - if (!isBrowserContext) { + if (!isWebBluetooth) { await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_3); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_4); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_5); @@ -71,7 +71,7 @@ export class WeDo2SmartHub extends Hub { this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE, this._parsePortMessage.bind(this)); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE, this._parseSensorMessage.bind(this)); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON, this._parseSensorMessage.bind(this)); - if (!isBrowserContext) { + if (!isWebBluetooth) { this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, this._parseBatteryMessage.bind(this)); this._bleDevice.readFromCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, (err, data) => { if (data) { @@ -87,7 +87,7 @@ export class WeDo2SmartHub extends Hub { this._bleDevice.subscribeToCharacteristic("00002a19-0000-1000-8000-00805f9b34fb", this._parseHighCurrentAlert.bind(this)); } this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT, this._parseHighCurrentAlert.bind(this)); - if (!isBrowserContext) { + if (!isWebBluetooth) { this._bleDevice.readFromCharacteristic(Consts.BLECharacteristic.WEDO2_FIRMWARE_REVISION, (err, data) => { if (data) { this._parseFirmwareRevisionString(data); diff --git a/test.html b/test.html deleted file mode 100644 index a7bcd4f..0000000 --- a/test.html +++ /dev/null @@ -1,47 +0,0 @@ - - - - -node-poweredup Web Bluetooth Test - - - - - - -

Web Bluetooth node-poweredup Test Suite

-
-Add new Hub -
-
- - - - - -
HubEventPort/ButtonValues
NK_WeDo2_1AttachATrain Motor
NK_WeDo2_1ButtonGREENDown
NK_WeDo2_1ButtonGREENUp
-
- - - \ No newline at end of file