UUIDs now web bluetooth compatible

This commit is contained in:
Nathan Kellenicki 2018-10-08 22:46:18 -07:00
parent 9f69c9b93d
commit 4baac6e6a8
9 changed files with 77 additions and 27 deletions

View File

@ -27,7 +27,7 @@ export class BoostMoveHub extends LPF2Hub {
public static IsBoostMoveHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID);
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID);
}

View File

@ -69,22 +69,22 @@ export enum BLEManufacturerData {
export enum BLEServices {
WEDO2_SMART_HUB = "000015231212efde1523785feabcd123",
LPF2_HUB = "000016231212efde1623785feabcd123"
WEDO2_SMART_HUB = "00001523-1212-efde-1523-785feabcd123",
LPF2_HUB = "00001623-1212-efde-1623-785feabcd123"
}
export enum BLECharacteristics {
WEDO2_BATTERY = "2a19",
WEDO2_BUTTON = "000015261212efde1523785feabcd123", // "1526"
WEDO2_PORT_TYPE = "000015271212efde1523785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub
WEDO2_LOW_VOLTAGE_ALERT = "000015281212efde1523785feabcd123", // "1528"
WEDO2_HIGH_CURRENT_ALERT = "000015291212efde1523785feabcd123", // "1529"
WEDO2_LOW_SIGNAL_ALERT = "0000152a1212efde1523785feabcd123", // "152a"
WEDO2_SENSOR_VALUE = "000015601212efde1523785feabcd123", // "1560"
WEDO2_VALUE_FORMAT = "000015611212efde1523785feabcd123", // "1561"
WEDO2_PORT_TYPE_WRITE = "000015631212efde1523785feabcd123", // "1563"
WEDO2_MOTOR_VALUE_WRITE = "000015651212efde1523785feabcd123", // "1565"
WEDO2_NAME_ID = "000015241212efde1523785feabcd123", // "1524"
LPF2_ALL = "000016241212efde1623785feabcd123"
WEDO2_BUTTON = "00001526-1212-efde-1523-785feabcd123", // "1526"
WEDO2_PORT_TYPE = "00001527-1212-efde-1523-785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub
WEDO2_LOW_VOLTAGE_ALERT = "00001528-1212-efde-1523-785feabcd123", // "1528"
WEDO2_HIGH_CURRENT_ALERT = "00001529-1212-efde-1523-785feabcd123", // "1529"
WEDO2_LOW_SIGNAL_ALERT = "0000152a-1212-efde-1523-785feabcd123", // "152a"
WEDO2_SENSOR_VALUE = "00001560-1212-efde-1523-785feabcd123", // "1560"
WEDO2_VALUE_FORMAT = "00001561-1212-efde-1523-785feabcd123", // "1561"
WEDO2_PORT_TYPE_WRITE = "00001563-1212-efde-1523-785feabcd123", // "1563"
WEDO2_MOTOR_VALUE_WRITE = "00001565-1212-efde-1523-785feabcd123", // "1565"
WEDO2_NAME_ID = "00001524-1212-efde-1523-785feabcd123", // "1524"
LPF2_ALL = "00001624-1212-efde-1623-785feabcd123"
}

View File

@ -52,7 +52,7 @@ export class DuploTrainBase extends LPF2Hub {
public static IsDuploTrainBase (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID);
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID);
}

View File

@ -0,0 +1,45 @@
/*
*
* This example allows you to connect Vernie and a Powered UP Remote Control to your laptop, and enables the control of Vernie with the Remote.
*
*/
const PoweredUP = require("..");
const poweredUP = new PoweredUP.PoweredUP();
poweredUP.scan(); // Start scanning
console.log("Looking for Vernie...");
const Modes = {
AVOIDING: 0,
MOVING: 1
}
poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
const vernie = hub;
await vernie.connect();
console.log("Connected to Vernie!");
let mode = Modes.MOVING;
vernie.setMotorSpeed("AB", 50);
vernie.on("distance", async (port, distance) => {
if (distance < 180 && mode === Modes.MOVING) {
mode = Modes.AVOIDING;
await vernie.setMotorSpeed("AB", 0, 1000);
await vernie.setMotorSpeed("AB", -20, 1000);
await vernie.setMotorSpeed("AB", 0, 1000);
vernie.setMotorSpeed("A", 30, 500);
await vernie.setMotorSpeed("B", -30, 500);
await vernie.setMotorSpeed("AB", 0, 1000);
vernie.setMotorSpeed("AB", 50);
mode = Modes.MOVING;
}
});
});

5
hub.ts
View File

@ -216,6 +216,11 @@ export class Hub extends EventEmitter {
}
protected _getCharacteristic (uuid: string) {
return this._characteristics[uuid.replace(/-/g, "")];
}
protected _subscribeToCharacteristic (characteristic: Characteristic, callback: (data: Buffer) => void) {
characteristic.on("data", (data: Buffer) => {
return callback(data);

View File

@ -37,7 +37,7 @@ export class LPF2Hub extends Hub {
public connect () {
return new Promise(async (resolve, reject) => {
await super.connect();
const characteristic = this._characteristics[Consts.BLECharacteristics.LPF2_ALL];
const characteristic = this._getCharacteristic(Consts.BLECharacteristics.LPF2_ALL);
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports
@ -101,7 +101,7 @@ export class LPF2Hub extends Hub {
protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._characteristics[uuid];
const characteristic = this._getCharacteristic(uuid);
if (characteristic) {
message = Buffer.concat([Buffer.alloc(2), message]);
message[0] = message.length;

View File

@ -32,7 +32,7 @@ export class PUPHub extends LPF2Hub {
public static IsPUPHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID);
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID);
}

View File

@ -57,7 +57,7 @@ export class PUPRemote extends LPF2Hub {
public static IsPUPRemote (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID);
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID);
}

View File

@ -26,7 +26,7 @@ export class WeDo2SmartHub extends Hub {
public static IsWeDo2SmartHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB) >= 0);
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0);
}
@ -49,12 +49,12 @@ export class WeDo2SmartHub extends Hub {
return new Promise(async (resolve, reject) => {
debug("Connecting to WeDo 2.0 Smart Hub");
await super.connect();
this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE], this._parsePortMessage.bind(this));
this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_SENSOR_VALUE], this._parseSensorMessage.bind(this));
this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_BUTTON], this._parseSensorMessage.bind(this));
this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_BATTERY], this._parseBatteryMessage.bind(this));
this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_HIGH_CURRENT_ALERT], this._parseHighCurrentAlert.bind(this));
this._characteristics[Consts.BLECharacteristics.WEDO2_BATTERY].read((err, data) => {
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BUTTON), this._parseSensorMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY), this._parseBatteryMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this));
this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY).read((err, data) => {
this._parseBatteryMessage(data);
});
debug("Connect completed");
@ -237,7 +237,7 @@ export class WeDo2SmartHub extends Hub {
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._characteristics[uuid];
const characteristic = this._getCharacteristic(uuid);
if (characteristic) {
characteristic.write(message, false, callback);
}