首页 > 解决方案 > 重新连接到已知和配对的网络蓝牙设备

问题描述

我有一个打印到蓝牙热敏打印机的网络应用程序。我设法连接到打印机并打印出我需要的东西。我想知道是否有办法将设备信息保存到会话或本地存储中,因此用户不需要每次都与设备配对。

我目前的代码如下:

    const device = await navigator.bluetooth.requestDevice({
      acceptAllDevices: true,
      optionalServices: [
        '000018f0-0000-1000-8000-00805f9b34fb'
      ]
    });

    const server = await device.gatt.connect();
    const service = await server.getPrimaryService("000018f0-0000-1000-8000-00805f9b34fb");
    const characteristic = await service.getCharacteristic("00002af1-0000-1000-8000-00805f9b34fb");

这个想法是有类似的东西:

const device = await navigator.bluetooth.connect(deviceId);
// using the device's ID or address, name or whatever information is available from the first pairing.

标签: javascriptnode.jsvue.jsbluetooththermal-printer

解决方案


推荐阅读