微信小程序 BLE 蓝牙封装

亦然 -
微信小程序 BLE 蓝牙封装

为了简化微信小程序环境下的蓝牙接入流程,经过线上正式项目一年的运行,发现BLE这块API许多坑,且难以移植复用,所以将它封装出来提高可维护性以及可移植性。

如何使用安装Eventenitter

npm install eventemitter2 --save

引入

在项目根目录utils文件夹下添加如下文件:ble.js、bleHandler.js、tools.js、error.js
完成上面步骤,就可以直接在小程序中使用蓝牙功能了。✨

示例
const emitter = new EventEmitter2();
const ble = new BLE(blename, emitter)

ble.listen(res => {
    if (res.type == 'connect') {
    switch(res.data){
        case "未打开适配器":
        break
      case "蓝牙已连接":
        break
      case ""
        break
    }
  }else if (res.type == "response") {
     console.log('收到设备消息响应:', res)
    //TODO
  }
})

ble.init()
实现细节

使用方法如上,很简单,只需要维护一个全局的ble实例,则可以进行蓝牙的各种功能操作。第二部引入的那几个文件是用来干嘛的呢?
大体上将蓝牙的连接、通讯、维护过程按功能的复杂程度分为三层:BLE、BLEHandler、Tool,ble更偏向用户层,blehandler提供一些流程性控制,tool则完全是封装的微信API,隔离一些繁复的工作,使代码看起来简洁一些。

源码解析
import BLEHandler from "./bleHandler"

class BLE extends BLEHandler {
    constructor(blename, emitter) {
        super(blename, emitter)
    }
    listen(callback) {
        // 蓝牙事件注册,打开channel
        this.emitter.removeAllListeners("channel")
        this.emitter.on("channel", callback)
    }
    removeListen() {
        // 移除所有蓝牙事件
        this.emitter.removeAllListeners("channel")
    }
    async init() {
        let flow = false
        // 打开蓝牙适配器状态监听
        this.onBLEConnectionStateChange()
        // 蓝牙适配器初始化
        await this.openAdapter()
        // 搜索蓝牙设备
        await this.startSearch()
        // 获取设备ID
        flow = await this.onBluetoothFound()
        // 停止搜索设备
        await this.stopSearchBluetooth()
        if (!flow) return
        // 连接蓝牙
        await this.connectBlue();
        // 获取serviceId
        await this.getBLEServices()
        // 设置特征值
        await this.getCharacteristics();
        // 订阅特征值
        await this.notifyBLECharacteristicValueChange()
        // 打开传输监听,等待设备反馈数据
        this.onBLECharacteristicValueChange()
    }
    // 发送指令
    async send(mudata, cmd) {
        let flow = await this.sentOrder(mudata, cmd)
        return flow
    }
    async close() {
        await this.closeBLEConnection()
        await this.closeBLEAdapter()
    }

}

export { BLE };
BLEHandler(promise的封装,及Eventenitter通信控制)
import * as t from "./tools"
import { HTTP } from "../server";

/**
 * 蓝牙工具类
 * 封装小程序蓝牙流程方法
 * 处理事件通信
 */
class BLEHandler {
    constructor(blename, emitter) {
        this.blename = blename
        this.emitter = emitter
        this.readCharacteristicId = "";
        this.writeCharacteristicId = "";
        this.notifyCharacteristicId = "";
        this.deviceId = "";
        this.serviceId = "";
        this.lastDate = new Date().getTime()
    }
    async openAdapter() {
        let [err, res] = await t._openAdapter.call(this);
        if (err != null) {
            this.emitter.emit("channel", {
                type: "connect",
                data: "未打开适配器"
            })
            return;
        }
        return true
    }
    async startSearch() {
        let [err, res] = await t._startSearch.call(this);
        if (err != null) {
            return;
        }
        this.emitter.emit("channel", {
            type: "connect",
            data: "蓝牙搜索中"
        })

    }
    async onBluetoothFound() {
        let [err, res] = await t._onBluetoothFound.call(this);
        if (err != null) {
            this.emitter.emit("channel", {
                type: "connect",
                data: "未找到设备"
            })
            // 取消适配器
            this.closeBLEAdapter()
            wx.setStorageSync("bluestatus", "");
            return;
        }
        this.emitter.emit("channel", {
            type: "connect",
            data: "正在连接中"
        })
        return true
    }
    async stopSearchBluetooth() {
        let [err, res] = await t._stopSearchBluetooth.call(this);
        if (err != null) {
            return;
        }
    }
    async connectBlue() {
        let [err, res] = await t._connectBlue.call(this);
        if (err != null) {
            return;
        }
    }
    async getBLEServices() {
        let [err, res] = await t._getBLEServices.call(this);
        if (err != null) {
            return;
        }
    }
    async getCharacteristics() {
        let [err, res] = await t._getCharacteristics.call(this);
        if (err != null) {
            this.emitter.emit("channel", {
                type: "connect",
                data: "无法订阅特征值"
            })
            // 取消连接
            this.closeBLEConnection()
            this.closeBLEAdapter()
            wx.setStorageSync("bluestatus", "");
            return;
        }
        return true
    }
    async notifyBLECharacteristicValueChange() {
        let [err, res] = await t._notifyBLECharacteristicValueChange.call(this);
        if (err != null) {
            // 取消连接
            this.emitter.emit("channel", {
                type: "connect",
                data: "无法订阅特征值"
            })
            this.closeBLEConnection()
            this.closeBLEAdapter()
            wx.setStorageSync("bluestatus", "");
            return;
        }
        this.emitter.emit("channel", {
            type: "connect",
            data: "蓝牙已连接"
        })
        wx.setStorageSync("bluestatus", "on");
        return true
    }
    async closeBLEConnection() {
        let [err, res] = await t._closeBLEConnection.call(this);
        if (err != null) {
            return;
        }
    }
    async closeBLEAdapter() {
        let [err, res] = await t._closeBLEAdapter.call(this);
        if (err != null) {
            return;
        }
    }
    async sentOrder(mudata, cmd) {
        let data = t._sentOrder(mudata, cmd)
        console.log("-- 发送数据:", data)
        let arrayBuffer = new Uint8Array(data).buffer;
        let [err, res] = await t._writeBLECharacteristicValue.call(this, arrayBuffer)
        if (err != null) {
            return
        }
        return true

    }

    // 打开蓝牙适配器状态监听
    onBLEConnectionStateChange() {
        wx.onBLEConnectionStateChange(res => {
            // 该方法回调中可以用于处理连接意外断开等异常情况
            if (!res.connected) {
                this.closeBLEAdapter()
                wx.setStorageSync("bluestatus", "");
                this.emitter.emit("channel", {
                    type: "connect",
                    data: "蓝牙已断开"
                })
            }
        }, err => {
            console.log('err', err)
        })
    }

    // 收到设备推送的notification
    onBLECharacteristicValueChange() {
        wx.onBLECharacteristicValueChange(res => {
            let arrbf = new Uint8Array(res.value)
            console.log("收到上传数据:",arrbf)
            console.log("时间戳",new Date().getTime())
            arrbf.map(res=>{
                console.log(res)
            })
            if (this._checkData(arrbf)) {
                if (arrbf[3] != 0x00) {
                    let nowDate = new Date().getTime()
                    if ((nowDate - this.lastDate) > 900) {
                        console.log('-- 节流900ms,Lock!')
                        this.lastDate = nowDate
                        this._uploadInfo(arrbf)
                        this.emitter.emit("channel", {
                            type: "response",
                            data: arrbf
                        })
                    }
                }
            }
        })
    }
    _uploadInfo(message) {
        console.log("-- 准备数据同步!", this._mapToArray(message))
        let bleorder = wx.getStorageSync("bleorder");
        let blecabinet = wx.getStorageSync("blecabinet")
        HTTP({
            url: "cabinet/uploadBlueData",
            methods: "post",
            data: {
                cabinetQrCode: blecabinet,
                order: bleorder,
                message: this._mapToArray(message)
            }
        }).then(res => {
            console.log("✔ 数据同步成功!")

        }, err => {
            console.log('✘ 数据同步失败', err)
        })
    }
    _mapToArray(arrbf) {
        let arr = []
        arrbf.map(item => {
            arr.push(item)
        })
        return arr
    }
    // 校验数据正确性
    _checkData(arrbf) {
        // 校验帧头帧尾
        if (arrbf[0] != 0xEE || arrbf[1] != 0xFA || arrbf[arrbf.length - 1] != 0xFF || arrbf[arrbf.length - 2] != 0xFC) {
            console.log('✘ 帧头帧尾不匹配,请重发')
            console.log('帧头:', arrbf[0])
            console.log('帧头:', arrbf[1])
            console.log('帧尾:', arrbf[arrbf.length - 1])
            console.log('帧尾:', arrbf[arrbf.length - 2])
            return false
        }
        // 校验CRC
        let crc = t._modBusCRC16(arrbf, 2, arrbf.length - 5)
        if (arrbf[arrbf.length - 3] != crc & 0xff && arrbf[arrbf.length - 4] != (crc >> 8) & 0xff) {
            console.log('✘ crc校验错误,请重发')
            return false
        }
        let time = new Date().toLocaleTimeString()
        console.log(`✔ CRC数据校验成功!${arrbf[3] == 0 ? '❤' : '命令码:' + arrbf[3]},time:${time}`)
        return true
    }

}
export default BLEHandler
流程图

项目地址:https://github.com/arsize/ble

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

前端微信小程序蓝牙javascript程序员

扩展阅读

加个好友,技术交流

1628738909466805.jpg