带有承诺的Web蓝牙 [英] Web bluetooth with promises

查看:199
本文介绍了带有承诺的Web蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,该项目想要使用Web蓝牙连接到BLE设备.我浏览了多个教程,并设法使它们起作用. 在下面的代码中,我获得了设备的特性. 该代码有效,但我不知道为什么.

I'm working on a project where a want to use web bluetooth to connect to a BLE device. I looked trough multiple tutorials and tried to make those work. In the code below i get the characteristic of the device. The code works but I don't know why.

我希望我的诺言能在找到并连接设备后激活.但是,当我将连接.then放入promise中时,另一个.then在启动应用程序时便已激活.

I want my promise to activate when a device is found and connected to. But when I put the connect .then in the promise the other .then already activates when I launch the application.

function read() {
    let prom = new Promise(function (resolve, reject) {
        navigator.bluetooth.requestDevice({
            acceptAllDevices: true,
            optionalServices: []
        })
            .then((device) => {
                console.log('Discovered', device);
                PCB= device;
                resolve();
                return PCB.gatt.connect();
            })
            .then(server => {
                gattServer = server;
                console.log('getting server');
                return gattServer.getPrimaryService(0x1815);
            })
            .then(service => {
                console.log('getting characteristic');
                if(0x2a56){
                    return service.getCharacteristic(0x2a56);
                }
                return service.getCharacteristic();
            })
            .then(characteristics => {
                console.log('> Characteristics: ' + characteristics + characteristics.map(c => c.uuid).join('\n' + ' '.repeat(19)));
            })
            .catch(error => {
                console.log(error);
                reject();
            });
    })
}

推荐答案

您需要

  1. 从功能中兑现您的诺言.
  2. 完成操作序列后兑现您的诺言.

您的问题示例,用//1和//2修改.

Your question example, modified with //1, and //2.

function read() {
  return new Promise(function(resolve, reject) { // 1
    navigator.bluetooth.requestDevice({
        acceptAllDevices: true,
        optionalServices: []
      })
      .then((device) => {
        console.log('Discovered', device);
        PCB = device;
        resolve();
        return PCB.gatt.connect();
      })
      .then(server => {
        gattServer = server;
        console.log('getting server');
        return gattServer.getPrimaryService(0x1815);
      })
      .then(service => {
        console.log('getting characteristic');
        if (0x2a56) {
          return service.getCharacteristic(0x2a56);
        }
        return service.getCharacteristic();
      })
      .then(characteristics => {
        console.log('> Characteristics: ' + characteristics +
          characteristics.map(c => c.uuid).join('\n' + ' '.repeat(19)));
        resolve(); // 2
      })
      .catch(error => {
        console.log(error);
        reject();
      });
  })
}

这篇关于带有承诺的Web蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆