美文网首页
如何优雅的实现微信小程序蓝牙通讯功能

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

作者: Sultan | 来源:发表于2018-01-11 15:50 被阅读149次

看了网上的几个例子,发现写的都比较乱,正好有业务需要,所以就封装了一个蓝牙库,采用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

相关文章

网友评论

      本文标题:如何优雅的实现微信小程序蓝牙通讯功能

      本文链接:https://www.haomeiwen.com/subject/yxqtoxtx.html