Can not retrieve human readable device type from devices. Updated web bluetooth example
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
ea20b1bee0
commit
dfd22b1d3d
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>Web Bluetooth node-poweredup Example</title>
|
<title>Web Bluetooth node-poweredup Example</title>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/node-poweredup@latest/dist/browser/poweredup.js"></script>
|
<!-- <script src="https://cdn.jsdelivr.net/npm/node-poweredup@latest/dist/browser/poweredup.js"></script> -->
|
||||||
<!-- <script src="../dist/browser/poweredup.js"></script> -->
|
<script src="../dist/browser/poweredup.js"></script>
|
||||||
<link rel="stylesheet" type="text/css" href="./web_bluetooth.css" />
|
<link rel="stylesheet" type="text/css" href="./web_bluetooth.css" />
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
@ -12,6 +12,14 @@ const poweredUP = new PoweredUP.PoweredUP();
|
|||||||
|
|
||||||
poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
||||||
|
|
||||||
|
hub.on("attach", (device) => {
|
||||||
|
log(`Device ${device.typeName} attached to port ${device.portName}`) ;
|
||||||
|
});
|
||||||
|
|
||||||
|
hub.on("detach", (device) => {
|
||||||
|
log(`Device ${device.typeName} detached to port ${device.portName}`) ;
|
||||||
|
});
|
||||||
|
|
||||||
await hub.connect(); // Connect to hub
|
await hub.connect(); // Connect to hub
|
||||||
log(`Connected to ${hub.name}!`);
|
log(`Connected to ${hub.name}!`);
|
||||||
|
|
||||||
@ -19,37 +27,25 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|||||||
log(`Disconnected ${hub.name}`);
|
log(`Disconnected ${hub.name}`);
|
||||||
})
|
})
|
||||||
|
|
||||||
hub.on("tilt", (port, x, y, z) => {
|
hub.on("tilt", (device, { x, y, z }) => {
|
||||||
log(`Tilt detected on port ${port} (X: ${x}, Y: ${y}${z !== "undefined" ? `, Z: ${z}`: ""})`);
|
console.log(x, y, z);
|
||||||
|
log(`Tilt detected on port ${device.portName} (X: ${x}, Y: ${y}${z !== undefined ? `, Z: ${z}`: ""})`);
|
||||||
});
|
});
|
||||||
|
|
||||||
hub.on("accel", (port, x, y, z) => {
|
hub.on("distance", (device, { distance }) => {
|
||||||
log(`Accelerometer detected on port ${port} (X: ${x}, Y: ${y}, Z: ${z})`);
|
log(`Motion detected on port ${device.portName} (Distance: ${distance})`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
hub.on("rotate", (device, { degrees }) => {
|
||||||
hub.on("distance", (port, distance) => {
|
log(`Rotation detected on port ${device.portName} (Degrees: ${degrees})`);
|
||||||
log(`Motion detected on port ${port} (Distance: ${distance})`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
hub.on("color", (port, color) => {
|
hub.on("button", ({ event }) => {
|
||||||
log(`Color detected on port ${port} (Color: ${color})`);
|
log(`Green button press detected (Event: ${event})`);
|
||||||
});
|
});
|
||||||
|
|
||||||
hub.on("rotate", (port, rotation) => {
|
hub.on("remoteButton", (device, { event }) => {
|
||||||
log(`Rotation detected on port ${port} (Rotation: ${rotation})`);
|
log(`Remote control button press detected on port ${device.portName} (Event: ${event})`);
|
||||||
});
|
|
||||||
|
|
||||||
hub.on("button", (button, state) => {
|
|
||||||
log(`Button press detected (Button: ${button}, State: ${state})`);
|
|
||||||
});
|
|
||||||
|
|
||||||
hub.on("attach", (port, device) => {
|
|
||||||
log(`Device attached to port ${port} (Device ID: ${device})`) ;
|
|
||||||
});
|
|
||||||
|
|
||||||
hub.on("detach", (port) => {
|
|
||||||
log(`Device detached from port ${port}`) ;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
renderHub(hub);
|
renderHub(hub);
|
||||||
@ -59,10 +55,11 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|||||||
let color = 1;
|
let color = 1;
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
|
|
||||||
const hubs = poweredUP.getConnectedHubs(); // Get an array of all connected hubs
|
const hubs = poweredUP.getHubs(); // Get an array of all connected hubs
|
||||||
document.getElementById("color").style.backgroundColor = PoweredUP.Consts.ColorNames[color];
|
document.getElementById("color").style.backgroundColor = PoweredUP.Consts.ColorNames[color];
|
||||||
hubs.forEach((hub) => {
|
hubs.forEach(async (hub) => {
|
||||||
hub.setLEDColor(color); // Set the color
|
const led = await hub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED);
|
||||||
|
led.setColor(color); // Set the color
|
||||||
})
|
})
|
||||||
color++;
|
color++;
|
||||||
if (color > 10) {
|
if (color > 10) {
|
||||||
@ -93,7 +90,7 @@ const scan = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const disconnect = function (uuid) {
|
const disconnect = function (uuid) {
|
||||||
poweredUP.getConnectedHubByUUID(decodeURIComponent(uuid)).disconnect();
|
poweredUP.getHubByUUID(decodeURIComponent(uuid)).disconnect();
|
||||||
document.getElementById(`hub-${uuid}`).remove();
|
document.getElementById(`hub-${uuid}`).remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user