我正在开发一个需要使用 Blink(1) mk2 设备通过浏览器发出通知警报的项目。我已经尝试使用 WEBUSB API 连接到 USB 的以下代码。
const VENDOR_ID = 0x27b8;
navigator.usb.requestDevice({
filters: [{
vendorId: VENDOR_ID
}]
}).then(selectedDevice => {
device = selectedDevice;
console.log("open")
var tOpen = device.open();
console.log("opened")
return tOpen;
}).then(() => {
console.log("selectConfiguration")
return device.selectConfiguration(1);
}).then(() => {
console.log("claimInterface")
return device.claimInterface(0);
}).then(() => {
console.log("controlTransferOut")
const r = Math.floor((Math.random() * 255) + 0);
const g = Math.floor((Math.random() * 255) + 0);
const b = Math.floor((Math.random() * 255) + 0);
// not entirely sure what is going on below...
const fadeMillis = 500;
const th = (fadeMillis / 10) >> 8;
const tl = (fadeMillis / 10) & 0xff;
const data = new Uint8Array([0x01, 0x63, r, g, b, th, tl, 0x00, 0x00]).buffer;
var rgb = new Uint8Array(3);
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
return device.controlTransferOut({
requestType: 'standard',
recipient: 'interface',
request: 0x09,
value: 1,
index: 0
},data);
}).then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});
设置权限弹出窗口后,我可以连接到 USB。在上面的代码打开它之后,selectConfiguration、claimInterface 也可以正常工作,当我调用 controlTransferOut 时,它也会发出命令并将结果返回为:
USBOutTransferResult {bytesWritten: 8, status: "ok"}
但 USB 上的闪烁或颜色变化并未反映。
我是否遗漏了某些东西,或者是否需要使用任何其他配置才能在 USB 上点亮?