Skip to content

Commit

Permalink
Merge pull request #1 from viviedu/vivi-5527-parse-service-data
Browse files Browse the repository at this point in the history
VIVI-5527 parse service data
  • Loading branch information
andrewjpritchard authored Feb 22, 2021
2 parents cc9294a + e2068ee commit a1c382d
Show file tree
Hide file tree
Showing 6 changed files with 512 additions and 431 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# VIVI SPECIFIC NOTES

This repository contains a modified version of `noble-winrt` which allows for better BLE scanning. Hopefully this is made redundant by the web bluetooth API, which is partially supported by Chrome. For now, we need this library to retrieve the advertisement data broadcast by all boxes in the vicinity.

The unmodified version of `noble-winrt` does not even attempt to retrieve any service data. This version of `noble-winrt` retrieves service data associated with 16 bit UUIDs from the raw AD structures that comprise the advertisement payload. The prebuilt `BLEServer.exe` comes from the `web-bluetooth-polyfill` repository that vivi also has a copy of. That version of `web-bluetooth-polyfill` has been modified to serialise the raw AD structure data so it can be serialised here.

# Noble (Node.js Bluetooth LE) Bindings for Windows

`noble-winrt` is a small UWP-to-noble bridge based on the [web-bluetooth-polyfill](https://github.com/urish/web-bluetooth-polyfill) project. It supports BLE connectivity on Windows without the need for a dongle and complicated driver set-up. It is similar to [noble-uwp](https://github.com/jasongin/noble-uwp) but may work better for some build processes.
Expand Down
20 changes: 19 additions & 1 deletion bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function fromWindowsUuid(uuid) {
return uuid.replace(/\{|\}/g, '');
}

const adTypeServiceData16BitUUID = 0x16;

class WinrtBindings extends events.EventEmitter {
init() {
this._deviceMap = {};
Expand Down Expand Up @@ -152,7 +154,7 @@ class WinrtBindings extends events.EventEmitter {
txPowerLevel: 0,
manufacturerData: null,
serviceUuids: message.serviceUuids.map(fromWindowsUuid),
serviceData: [],
serviceData: this._retrieveServiceData(message.adStructures),
};
this.emit(
'discover',
Expand Down Expand Up @@ -190,6 +192,17 @@ class WinrtBindings extends events.EventEmitter {
}
}

// This will only retrieve service data with 16 bit UUIDs. If you need
// anything else, parse the relevant adStructures
_retrieveServiceData(adStructures) {
return adStructures
.filter((adStructure) => adStructure.type === adTypeServiceData16BitUUID)
.map((adStructure) => ({
uuid: this._toHex(adStructure.data[1]) + this._toHex(adStructure.data[0]),
data: Buffer.from(adStructure.data.slice(2))
}));
}

_sendMessage(message) {
debug('out:', message);
const dataBuf = Buffer.from(JSON.stringify(message), 'utf-8');
Expand All @@ -206,6 +219,11 @@ class WinrtBindings extends events.EventEmitter {
this._sendMessage(Object.assign({}, message, { _id: requestId }));
});
}

// converts a number in range 0-255 to a two byte hex code like "C4"
_toHex(number) {
return number.toString(16).padStart(2, '0').toUpperCase();
}
}

exports.WinrtBindings = WinrtBindings;
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ if (os.platform() === 'win32') {
if (!(ver[0] > 10 ||
(ver[0] === 10 && ver[1] > 0) ||
(ver[0] === 10 && ver[1] === 0 && ver[2] >= 15014))) {
throw new Error('Noble WinRT bindings require Windows >= 10.0.15014.');
module.exports = require('@abandonware/noble');
}

const Noble = require('noble/lib/noble');
const Noble = require('@abandonware/noble/lib/noble');
const { WinrtBindings } = require('./bindings.js');
module.exports = new Noble(new WinrtBindings());
} else {
module.exports = require('noble');;
module.exports = require('@abandonware/noble');
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"author": "Uri Shaked",
"license": "MIT",
"dependencies": {
"@abandonware/noble": "^1.9.2-11",
"chrome-native-messaging": "^0.2.0",
"debug": "^2.6.8",
"noble": "^1.7.0"
"debug": "^2.6.8"
},
"engines": {
"node": ">=6.4.0"
Expand Down
Binary file modified prebuilt/BLEServer.exe
Binary file not shown.
Loading

0 comments on commit a1c382d

Please sign in to comment.