webusb - Android Chrome 上的 WebUSB
问题描述
我有一个使用 WebUSB 连接到标签打印机并打印标签的网站。一切都在计算机上运行良好,但不幸的是它不适用于 Android。Android 找到我的打印机“LP 433”,但什么也没发生,它应该打印。我使用 OTG 电缆连接到打印机。谷歌浏览器版本:83。在其他浏览器上WebUSB没有任何反应,除了firefox,直接说不支持WebUSB。我的网站在 https 上工作。
async findDevices() {
try {
const navi: any = navigator;
const devices = await navi.usb.getDevices();
console.log(devices);
devices.forEach(dev => {
console.log(dev);
});
const device = await navi.usb.requestDevice({filters: [{vendorId: 15923}]});
await device.open(); // Begin a session.
await device.selectConfiguration(1); // Select configuration #1 for the device.
await device.claimInterface(0); // Request exclusive control over interface #2.
await device.controlTransferOut({
requestType: 'vendor',
recipient: 'interface',
request: 0x01, // vendor-specific request: enable channels
value: 0x0013, // 0b00010011 (channels 1, 2 and 5)
index: 0x0001 // Interface 1 is the recipient
})
.then(() => device.transferIn(1, 64)) // Waiting for 64 bytes of data from endpoint #5.
.then(result => {
const decoder = new TextDecoder();
console.log('Received: ' + decoder.decode(result.data));
// document.getElementById('target').innerHTML = 'Received: ' + decoder.decode(result.data);
})
.catch(error => {
console.log(error);
});
const imgData: any = await this.toPrintFormat();
for (let p = 0; p < this.pcs; p++) {
for (let i = 0; i < imgData.length; i++) {
// setTimeout(() => {
const string2 =
'sLABEL,' + imgData[i].width + ',' + imgData[i].height + '\n' +
'sGAP,0,0\n' +
'sTPHY,0\n' +
'sDENSITY,7\n' +
'sSPEED,' + this.speedp + '\n' +
'sTHERMAL,' + this.thermop + '\n' +
'sDIRECTION,1\n' +
'sORIGIN,0,0\n' +
'wSAVEB64,' + imgData[i].src.length + ',\"WLP000\",' + imgData[i].src + '\n' +
'wLOADIMG,0,' + 0 + ',1,\"WLP000\"\n' +
'wPRINT,1\n';
console.log(string2);
const encoder = new TextEncoder();
const data = encoder.encode(string2);
device.transferOut(2, data)
.catch(error => {
console.log(error);
});
}
}
// document.getElementById('target').innerHTML = 'Received: ' + decoder.decode(result.data);
} catch (error) {
// document.getElementById('target').innerHTML = error;
}
}
解决方案
推荐阅读
- javascript - 没有表列时如何传递参数 (GeomFromText('Polygon (( ? ?,))) node-maria
- excel - Xl.generateReport 文件位置不在服务器上创建 excel 报告
- python - 字节索引必须是整数或切片,而不是 str
- swift - 如何在类型上使用属性包装器抽象单例?
- r - ggplot 条形图在 Shiny 上无法正确显示
- vba - 在 VBA 中检查 pscp 传输的结果
- android - 在应用程序关闭或打开时检测来电或响铃
- sql-server-2019 - Synapse Analytics 与 SQL Server 2019 大数据集群
- reactjs - 我想在主页上显示一个变量
- c - 为什么这些函数具有不同的时间复杂度?