webbluetooth
Advanced tools
Comparing version 1.3.0 to 1.3.1-next.7aed9b0.0
@@ -30,13 +30,10 @@ # Node Web Bluetooth | ||
```JavaScript | ||
var bluetooth = require("webbluetooth").bluetooth; | ||
const bluetooth = require("webbluetooth").bluetooth; | ||
bluetooth.requestDevice({ | ||
const device = await bluetooth.requestDevice({ | ||
filters:[{ services:[ "heart_rate" ] }] | ||
}) | ||
.then(device => { | ||
return device.gatt.connect(); | ||
}) | ||
.then(server => { | ||
... | ||
}) | ||
}); | ||
const server = await device.gatt.connect(); | ||
... | ||
``` | ||
@@ -51,5 +48,5 @@ | ||
```JavaScript | ||
var Bluetooth = require("webbluetooth").Bluetooth; | ||
const Bluetooth = require("webbluetooth").Bluetooth; | ||
function handleDeviceFound(device, selectFn) { | ||
const deviceFound = (device, selectFn) => { | ||
// If device can be automatically selected, do so by returning true | ||
@@ -59,17 +56,12 @@ if (device.name === "myName") return true; | ||
// Otherwise store the selectFn somewhere and execute it later to select this device | ||
} | ||
}; | ||
var bluetooth = new Bluetooth({ | ||
deviceFound: handleDeviceFound | ||
const bluetooth = new Bluetooth({ deviceFound }); | ||
const device = await bluetooth.requestDevice({ | ||
filters:[{ services:[ "heart_rate" ] }] | ||
}); | ||
bluetooth.requestDevice({ | ||
filters:[{ services:[ "heart_rate" ] }] | ||
}) | ||
.then(device => { | ||
return device.gatt.connect(); | ||
}) | ||
.then(server => { | ||
... | ||
}) | ||
const server = await device.gatt.connect(); | ||
... | ||
``` | ||
@@ -76,0 +68,0 @@ |
@@ -26,7 +26,7 @@ /* | ||
var webbluetooth = require("../"); | ||
const webbluetooth = require("../"); | ||
var eddystoneUUID = 0xFEAA; | ||
const eddystoneUUID = 0xFEAA; | ||
var frameTypes = { | ||
const frameTypes = { | ||
"UID": 0x00, | ||
@@ -37,3 +37,3 @@ "URL": 0x10, | ||
var schemes = { | ||
const schemes = { | ||
0x00: "http://www.", | ||
@@ -45,3 +45,3 @@ 0x01: "https://www.", | ||
var expansions = { | ||
const expansions = { | ||
0x00: ".com/", | ||
@@ -63,17 +63,17 @@ 0x01: ".org/", | ||
function handleDeviceFound(bluetoothDevice) { | ||
var uuid = webbluetooth.getServiceUUID(eddystoneUUID); | ||
var eddyData = bluetoothDevice.adData.serviceData.get(uuid); | ||
const deviceFound = bluetoothDevice => { | ||
const uuid = webbluetooth.getServiceUUID(eddystoneUUID); | ||
const eddyData = bluetoothDevice.adData.serviceData.get(uuid); | ||
if (eddyData) { | ||
var decoded = decodeEddystone(eddyData); | ||
const decoded = decodeEddystone(eddyData); | ||
if (decoded) { | ||
switch(decoded.type) { | ||
case frameTypes.UID: | ||
console.log("txPower: " + decoded.txPower); | ||
console.log(`txPower: ${decoded.txPower}`); | ||
break; | ||
case frameTypes.URL: | ||
console.log("url: " + decoded.url); | ||
console.log(`url: ${decoded.url}`); | ||
break; | ||
case frameTypes.TLM: | ||
console.log("version: " + decoded.version); | ||
console.log(`version: ${decoded.version}`); | ||
break; | ||
@@ -83,16 +83,14 @@ } | ||
} | ||
} | ||
}; | ||
var bluetooth = new webbluetooth.Bluetooth({ | ||
deviceFound: handleDeviceFound | ||
}); | ||
const bluetooth = new webbluetooth.Bluetooth({ deviceFound }); | ||
function decodeEddystone(view) { | ||
var type = view.getUint8(0); | ||
if (typeof type === "undefined") return null; | ||
const decodeEddystone = view => { | ||
const type = view.getUint8(0); | ||
if (typeof type === "undefined") return undefined; | ||
if (type === frameTypes.UID) { | ||
var uidArray = []; | ||
for (var i = 2; i < view.byteLength; i++) { | ||
var hex = view.getUint8(i).toString(16); | ||
const uidArray = []; | ||
for (let i = 2; i < view.byteLength; i++) { | ||
const hex = view.getUint8(i).toString(16); | ||
uidArray.push(("00" + hex).slice(-2)); | ||
@@ -109,4 +107,4 @@ } | ||
if (type === frameTypes.URL) { | ||
var url = ""; | ||
for (var i = 2; i < view.byteLength; i++) { | ||
const url = ""; | ||
for (let i = 2; i < view.byteLength; i++) { | ||
if (i === 2) { | ||
@@ -137,14 +135,16 @@ url += schemes[view.getUint8(i)]; | ||
// Recursively scan | ||
function scan() { | ||
// Continuously scan | ||
(async () => { | ||
console.log("scanning..."); | ||
bluetooth.requestDevice({ | ||
filters:[{ services:[ eddystoneUUID ] }] | ||
}) | ||
.then(scan) | ||
.catch(error => { | ||
console.log(error); | ||
process.exit(); | ||
}); | ||
} | ||
scan(); | ||
while (true) { | ||
try { | ||
await bluetooth.requestDevice({ | ||
filters:[{ services:[ eddystoneUUID ] }] | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
process.exit(); | ||
} | ||
} | ||
})(); |
@@ -26,34 +26,32 @@ /* | ||
var bluetooth = require("../").bluetooth; | ||
const bluetooth = require("../").bluetooth; | ||
console.log("Requesting Bluetooth Devices..."); | ||
bluetooth.requestDevice({ | ||
filters:[{ services:[ "heart_rate" ] }] | ||
}) | ||
.then(device => { | ||
console.log("Found device: " + device.name); | ||
return device.gatt.connect(); | ||
}) | ||
.then(server => { | ||
console.log("Gatt server connected: " + server.connected); | ||
return server.getPrimaryService("heart_rate"); | ||
}) | ||
.then(service => { | ||
console.log("Primary service: " + service.uuid); | ||
return service.getCharacteristic("heart_rate_measurement"); | ||
}) | ||
.then(characteristic => { | ||
console.log("Characteristic: " + characteristic.uuid); | ||
return characteristic.startNotifications(); | ||
}) | ||
.then(characteristic => { | ||
console.log("Notifications started"); | ||
(async () => { | ||
try { | ||
console.log("Requesting Bluetooth Devices..."); | ||
characteristic.addEventListener("characteristicvaluechanged", event => { | ||
if (event.value.buffer.byteLength) console.log(event.value.getUint16(0)); | ||
}); | ||
}) | ||
.catch(error => { | ||
console.log(error); | ||
process.exit(); | ||
}); | ||
const device = await bluetooth.requestDevice({ | ||
filters:[{ services:[ "heart_rate" ] }] | ||
}); | ||
console.log(`Found device: ${device.name}`); | ||
const server = await device.gatt.connect(); | ||
console.log(`Gatt server connected: ${server.connected}`); | ||
const service = await server.getPrimaryService("heart_rate"); | ||
console.log(`Primary service: ${service.uuid}`); | ||
const characteristic = await service.getCharacteristic("heart_rate_measurement"); | ||
console.log(`Characteristic: ${characteristic.uuid}`); | ||
await characteristic.startNotifications(); | ||
console.log("Notifications started"); | ||
characteristic.addEventListener("characteristicvaluechanged", event => { | ||
if (event.value.buffer.byteLength) console.log(event.value.getUint16(0)); | ||
}); | ||
} catch(error) { | ||
console.log(error); | ||
process.exit(1); | ||
}; | ||
})(); |
@@ -26,21 +26,22 @@ /* | ||
var Bluetooth = require("../").Bluetooth; | ||
var bluetoothDevices = []; | ||
const Bluetooth = require("../").Bluetooth; | ||
const bluetoothDevices = []; | ||
process.stdin.setEncoding("utf8"); | ||
process.stdin.on("readable", () => { | ||
var input = process.stdin.read(); | ||
const input = process.stdin.read(); | ||
if (input === "\u0003") { | ||
process.exit(); | ||
} else { | ||
var index = parseInt(input); | ||
if (index && index <= bluetoothDevices.length) { | ||
process.stdin.setRawMode(false); | ||
selectDevice(index - 1); | ||
} | ||
} | ||
const index = parseInt(input); | ||
if (index && index <= bluetoothDevices.length) { | ||
process.stdin.setRawMode(false); | ||
const device = bluetoothDevices[index - 1]; | ||
device.select(); | ||
} | ||
}); | ||
function handleDeviceFound(bluetoothDevice, selectFn) { | ||
var discovered = bluetoothDevices.some(device => { | ||
const deviceFound = (bluetoothDevice, selectFn) => { | ||
const discovered = bluetoothDevices.some(device => { | ||
return (device.id === bluetoothDevice.id); | ||
@@ -57,70 +58,47 @@ }); | ||
console.log(bluetoothDevices.length + ": " + bluetoothDevice.name); | ||
console.log(`${bluetoothDevices.length}: ${bluetoothDevice.name}`); | ||
if (bluetoothDevice._serviceUUIDs.length) { | ||
console.log("\tAdvertising: " + bluetoothDevice._serviceUUIDs); | ||
console.log(`\tAdvertising: ${bluetoothDevice._serviceUUIDs}`); | ||
} | ||
} | ||
}; | ||
var bluetooth = new Bluetooth({ | ||
deviceFound: handleDeviceFound | ||
}); | ||
function logError(error) { | ||
console.log(error); | ||
process.exit(); | ||
} | ||
function enumerateGatt(server) { | ||
return server.getPrimaryServices() | ||
.then(services => { | ||
var sPromises = services.map(service => { | ||
return service.getCharacteristics() | ||
.then(characteristics => { | ||
var cPromises = characteristics.map(characteristic => { | ||
return characteristic.getDescriptors() | ||
.then(descriptors => { | ||
descriptors = descriptors.map(descriptor => `\t\t└descriptor: ${descriptor.uuid}`); | ||
descriptors.unshift(`\t└characteristic: ${characteristic.uuid}`); | ||
return descriptors.join("\n"); | ||
}); | ||
}); | ||
return Promise.all(cPromises) | ||
.then(descriptors => { | ||
descriptors.unshift(`service: ${service.uuid}`); | ||
return descriptors.join("\n"); | ||
}); | ||
}); | ||
const enumerateGatt = async server => { | ||
const services = await server.getPrimaryServices(); | ||
const sPromises = services.map(async service => { | ||
const characteristics = await service.getCharacteristics(); | ||
const cPromises = characteristics.map(async characteristic => { | ||
let descriptors = await characteristic.getDescriptors(); | ||
descriptors = descriptors.map(descriptor => `\t\t└descriptor: ${descriptor.uuid}`); | ||
descriptors.unshift(`\t└characteristic: ${characteristic.uuid}`); | ||
return descriptors.join("\n"); | ||
}); | ||
return Promise.all(sPromises) | ||
.then(services => { | ||
console.log(services.join("\n")); | ||
}); | ||
const descriptors = await Promise.all(cPromises); | ||
descriptors.unshift(`service: ${service.uuid}`); | ||
return descriptors.join("\n"); | ||
}); | ||
} | ||
function selectDevice(index) { | ||
var device = bluetoothDevices[index]; | ||
device.select(); | ||
} | ||
const result = await Promise.all(sPromises); | ||
console.log(result.join("\n")); | ||
}; | ||
var server = null; | ||
const bluetooth = new Bluetooth({ deviceFound }); | ||
console.log("scanning..."); | ||
bluetooth.requestDevice() | ||
.then(device => { | ||
console.log("connecting..."); | ||
return device.gatt.connect(); | ||
}) | ||
.then(gattServer => { | ||
console.log("connected"); | ||
server = gattServer; | ||
return enumerateGatt(server); | ||
}) | ||
.then(() => server.disconnect()) | ||
.then(() => { | ||
console.log("\ndisconnected"); | ||
process.exit(); | ||
}) | ||
.catch(logError); | ||
(async () => { | ||
try { | ||
const device = await bluetooth.requestDevice(); | ||
console.log("connecting..."); | ||
const server = await device.gatt.connect(); | ||
console.log("connected"); | ||
await enumerateGatt(server); | ||
await server.disconnect(); | ||
console.log("\ndisconnected"); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
process.exit(0); | ||
})(); |
{ | ||
"name": "webbluetooth", | ||
"version": "1.3.0", | ||
"version": "1.3.1-next.7aed9b0.0", | ||
"description": "Node.js implementation of the Web Bluetooth Specification", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/thegecko/webbluetooth", |
@@ -42,3 +42,3 @@ /// <reference types="node" /> | ||
constructor(); | ||
private readonly state; | ||
private get state(); | ||
private init; | ||
@@ -45,0 +45,0 @@ private checkForError; |
@@ -70,3 +70,3 @@ import { EventDispatcher } from "./dispatcher"; | ||
*/ | ||
readonly value: DataView; | ||
get value(): DataView; | ||
private handle; | ||
@@ -73,0 +73,0 @@ private descriptors; |
@@ -18,3 +18,3 @@ import { BluetoothRemoteGATTCharacteristic } from "./characteristic"; | ||
*/ | ||
readonly value: DataView; | ||
get value(): DataView; | ||
private handle; | ||
@@ -21,0 +21,0 @@ /** |
@@ -67,3 +67,3 @@ import { EventDispatcher } from "./dispatcher"; | ||
*/ | ||
unwatchAdvertisements(): Promise<{}>; | ||
unwatchAdvertisements(): Promise<unknown>; | ||
} |
@@ -15,3 +15,3 @@ import { BluetoothDevice } from "./device"; | ||
*/ | ||
readonly connected: boolean; | ||
get connected(): boolean; | ||
private handle; | ||
@@ -18,0 +18,0 @@ private services; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1360559
5006
3