低功耗蓝牙使用API设计封装函数模式示例
发布于 4 年前 作者 zhuxiuying 4870 次浏览 来自 分享
var bluetooth = {

    RESETfunction(){
        // 基础数据配置

        this.btName = "";
        this.deviceId = "";
        this.serviceId = "";
        this.write_characteristicId = "";
        this.read_characteristicId = "";

    },
    STARTasync function () {
        // 启动蓝牙,并用户判断用户是否已开启蓝牙

    },
    SEARCHasync function () {
        // 搜索设备是否在附近,wx.onBluetoothDeviceFound

        await this.START(); 
        return new Promise((reslove, reject)=>{
            reslove();
        })
    },
    CONNECTasync function () {
        // 连接设备

        // if(self.btName == device.btName)
        let sameName = self.btName == device.btName ? true : false// 记录蓝牙连接是否多次同时快速触发

        if(this.waitConnect && sameName){
            console.log("正在连接,请稍等...")
            return;
        }

        this.waitConnect = true;

        await this.SEARCH(); 
        return new Promise((reslove, reject)=>{
            
            this.waitConnect = false;
            reslove();

        })
    },
    SENDasync function (device, data, callback{
        // 发送数据
        
        await this.CONNECT();  

        return new Promise((reslove, reject)=>{

            device.sendEnd();

            callback();

        })

    },
    receiveDataCallbackfunction(){
        // 接收设备返回的数据到前台
        callback(value)

    },
    CLOSEasync function () {
        // 关闭蓝牙线程
    },
    DISCONNECTfunction(){
        // 断开指定设备连接
    },
    STOPfunction(){

        // 停止搜索监听
    },
    ERRORfunction(){
        /***
       * ERROR 错误
       * err [@params](/user/params) {} 
       * err.ercode [@params](/user/params) Number
       *  0 ok  正常
       * 10000  not init    未初始化蓝牙适配器
       * 10001  not available   当前蓝牙适配器不可用
       * 10002  no device   没有找到指定设备
       * 10003  connection fail 连接失败
       * 10004  no service  没有找到指定服务
       * 10005  no characteristic   没有找到指定特征值
       * 10006  no connection   当前连接已断开
       * 10007  property not support    当前特征值不支持此操作
       * 10008  system error    其余所有系统上报的异常
       * 10009  system not support  Android 系统特有,系统版本低于 4.3 不支持 BLE
       * 10012  operate time out    连接超时
       * 10013  invalid_data    连接 deviceId 为空或者是格式不正确
       */

  },
}

var device = {
    btName"btName",
    deviceId"00:00:00:AB:CD:EF",  
    passChecknew Date().getTime(),  // 记录触发跟设备返回是否为同一事件
    sendEndfunction(res){
        // 用于记录发送数据给设备成功后,设备一直未返回执行相关命令
        clearTimeout(bluetooth.sendTimer);  
        bluetooth.sendTimer = setTimeout(() => {
            
            clearTimeout(bluetooth.sendTimer);

            bluetooth.CLOSE();

        }, timeout);
    },
    onBLEConnectionStateChangefunction(res){
        // 用于监听蓝牙设备操作过程中,突然把设备断电或手动断开蓝牙捕获异常处理

        // if(this.passCheck == )

    },
    capturefunction(err){
        // 用于获取操作过程中,未启动,蓝牙连接失败等问题处理

        // if(this.passCheck == )

    }
}

// 使用示例
let data = "00 11 22";
bluetooth.START(device);
bluetooth.SEARCH(device);
bluetooth.CONNECT(device);
bluetooth.SEND(device, data, function (value{
    console.log("value=>",value)  // 用于收到设备返回数据回调处理
    if(value){
        bluetooth.passCheck = "";
        bluetooth.CLOSE();
    }
})
1 回复

写的真棒,收藏了

回到顶部