敬告:此 DEMO 演示为开放测试页面,仅用于开发者快速测试体验应用功能,请严格遵守开发者协议,了解更多。
♦ JS-SDK 引用方式:
♦ 普通网页 script 方式加载:下载最新版 jsBridge-v20251019.zip,请在页面上调用 jsBridge 接口之前引用 jsbridge-mini.js 库;
♦ js module 方式引用:npm install ym-jsbridge 具体请参考 npm package
♦ USB Host 主机模式;
监听器
• 如需处理接口调用事件,请设置此监听器;
jsBridge.usb.setListener(function(event, res) {
    switch (event) {
        //USB 串口消息
        case "serial": {
            //res.deviceId
            //res.serialPortIndex
            switch (res.action) {
                //收到设备发来的数据
                case "onReceive": {
                    console.log(res.hexData);
                    break;
                }
                //出错
                case "onError": {
                    console.error(res.message);
                    break;
                }
            }
            break;
        }
    }
    //此函数仅用于显示回调参数在本 DEMO 页面上
    showResult({
        event: event,
        res  : res
    });
});
//请拉到页面底部查看回调数据信息
$('html,body').animate({ scrollTop: $('#view').offset().top }, 500);
            //移除监听器,不会再收到回调通知 //在需要时可重新调用 setListener jsBridge.usb.removeListener();
设备
jsBridge.usb.devices(function(success, res) {
  if (success) {
    //成功,拉到本页面底部查看
    //此函数仅用于显示回调参数在本 DEMO 页面上
    showResult({
      devices: res
    });
  } else {
    alert("失败\n" + JSON.stringify(res));
  }
});
/**
成功时 res 为数组类型
[
  //示例
  {
    deviceId: 1,
    deviceName: "",
    deviceClass: 1,
    deviceSubclass: 1,
    deviceProtocol: 1,
    manufacturerName: "",
    vendorId: 1,
    productId: 1,
    productName: "",
    serialNumber: "",
    interfaceCount: 1,
    configurationCount: 1,
    version: ""
  },
  //...
]
**/
            SerialPort 串口
deviceId:
vendorId:
productId:
jsBridge.usb.serialPorts({
  //必须,devices() 接口获取到的已接入设备 deviceId
  deviceId: {{serial.deviceId}},
  //可选
  vendorId: {{serial.vendorId}},
  //可选
  productId: {{serial.productId}}
}, function(success, res) {
  if (success) {
    //成功,拉到本页面底部查看
    //此函数仅用于显示回调参数在本 DEMO 页面上
    showResult({
      serialPorts: res
    });
  } else {
    alert("失败\n" + JSON.stringify(res));
  }
});
/**
成功时 res 为数组类型
[
  //示例
  {
    serialPortIndex: 0,
    portNumber: 1,
    isOpen: false,
    serial: "",
    cd: false,
    cts: false,
    dsr: false,
    dtr: false,
    ri: false,
    rts: false
  },
  //...
]
**/
            deviceId:
serialPortIndex:
baudRate:
jsBridge.usb.openSerialPort({
  //必须,devices() 接口获取到的已接入设备 deviceId
  deviceId: {{serial.deviceId}},
  //必须,serialPorts() 接口获取的端口序号 serialPortIndex
  serialPortIndex: {{serial.serialPortIndex}},
  //必须,波特率
  baudRate: {{serial.baudRate}},
  //可选
  //dtr: false,
  //可选
  //rts: false
}, function(success, res) {
  if (success) {
    alert("打开成功");
  } else {
    alert("失败\n" + JSON.stringify(res));
  }
});
            deviceId:
serialPortIndex:
jsBridge.usb.closeSerialPort({
  //必须,devices() 接口获取到的已接入设备 deviceId
  deviceId: {{serial.deviceId}},
  //必须,serialPorts() 接口获取的端口序号 serialPortIndex
  serialPortIndex: {{serial.serialPortIndex}}
}, function(success, res) {
  if (success) {
    alert("关闭成功");
  } else {
    alert("失败\n" + JSON.stringify(res));
  }
});
            deviceId:
serialPortIndex:
hexData:
jsBridge.usb.writeSerialPort({
  //必须,devices() 接口获取到的已接入设备 deviceId
  deviceId: {{serial.deviceId}},
  //必须,serialPorts() 接口获取的端口序号 serialPortIndex
  serialPortIndex: {{serial.serialPortIndex}},
  //必须,bytes 流的十六进制字符格式
  hexData: "{{serial.hexData}}"
}, function(success, res) {
  if (success) {
    alert("关闭成功");
  } else {
    alert("失败\n" + JSON.stringify(res));
  }
});
            deviceId:
serialPortIndex:
jsBridge.usb.readSerialPort({
  //必须,devices() 接口获取到的已接入设备 deviceId
  deviceId: {{serial.deviceId}},
  //必须,serialPorts() 接口获取的端口序号 serialPortIndex
  serialPortIndex: {{serial.serialPortIndex}}
}, function(success, res) {
  if (success) {
    alert("成功读取到:\n" + res.hexData);
  } else {
    alert("失败\n" + JSON.stringify(res));
  }
});
/**
成功时 res
{
  hexData: "" // bytes 流的十六进制字符格式
}
**/
            deviceId:
serialPortIndex:
jsBridge.usb.breakSerialPort({
  //必须,devices() 接口获取到的已接入设备 deviceId
  deviceId: {{serial.deviceId}},
  //必须,serialPorts() 接口获取的端口序号 serialPortIndex
  serialPortIndex: {{serial.serialPortIndex}}
}, function(success, res) {
  if (success) {
    alert("成功");
  } else {
    alert("失败\n" + JSON.stringify(res));
  }
});
            监听回调数据: