如何优雅的实现微信小程序蓝牙通讯功能

看了网上的几个例子,发现写的都比较乱,正好有业务需要,所以就封装了一个蓝牙库,采用Promise实现,将过程变得极其简单,主要代码如下:

connectDevice: function () {

    return bleComm.open().then(res => {
      return bleComm.getStatus();
    }).then(res => {
      return bleComm.startScan();
    }).then(res => {
      return bleComm.getDevices();
    }).then(res => {
      return bleComm.foundDevice();
    }).then(res => {
      return bleComm.stopScan();
    }).then(res => {
      return bleComm.connect();
    }).then(res => {
      return bleComm.getServices();
    }).then(res => {
      return bleComm.getCharacteristics();
    }).then(res => {
      return bleComm.registerNotify();
    })
  }

github地址:https://github.com/zidong0822/WXBleDemo.git

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容