1***@163.com
1***@163.com
  • 发布:2024-08-19 14:25
  • 更新:2024-08-19 14:25
  • 阅读:380

【报Bug】readBLECharacteristicValue:fail property not support?报1007错误

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: T330-HRAS-C1-20240617-110121

HBuilderX类型: 正式

HBuilderX版本号: 4.24

手机系统: Android

手机系统版本号: Android 12

手机机型: T330-HRAS-C1

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

示例代码:
<template>  
    <view>  
        <block v-if="m_show_auth == 0">  
            <u-loading-page :loading="true" :loading-text="m_show_tips"></u-loading-page>  
            <!--        <view class="feed-back" :style="'background: '+m_bgColor">  
          <view style="margin-top:150px;">  
            <  
          </view>  
        </view> -->  
        </block>  

        <block v-else>  
            <view class="feed-contain">  
                <u-cell-group>  
                    <u-cell title="已找到匹配的蓝牙物联网模块:" :border="true" :value="m_operation_tips">  
                        <u-icon slot="icon" color="#2979ff" size="20" name="setting"></u-icon>  
                    </u-cell>  
                    <u-cell title="蓝牙名称" title-width="80px" :required="true" :center="true"  
                        :value="m_blueDevice.name"></u-cell>  
                    <u-cell title="蓝牙标识" title-width="80px" :required="true" :center="true"  
                        :value="m_blueDevice.deviceId"></u-cell>  
                    <u-cell title="蓝牙UUID" title-width="80px" :required="true" :center="true"  
                        :value="m_blueDevice.advertisServiceUUIDs[0]"></u-cell>  
                </u-cell-group>  
            </view>  

            <view class="maring-center">  
                <view class="feed-name">  
                    <view class="feed-tag" />  
                    <label class="feed-span">请选择联网模式:</label>  
                </view>  
            </view>  

            <u-radio-group :value="m_nSelMode" @change="onModeClick">  
                <u-cell-group>  
                    <u-cell title="WIFI联网模式" :clickable="true" :required="true">  
                        <u-radio slot="right-icon" name="1" />  
                    </u-cell>  
                    <u-cell title="4G联网模式" :clickable="true" :required="true">  
                        <u-radio slot="right-icon" name="2" />  
                    </u-cell>  
                </u-cell-group>  
            </u-radio-group>  
            <block v-if="m_nSelMode == '1'">  
                <view class="maring-center">  
                    <view class="feed-name">  
                        <view class="feed-tag" />  
                        <label class="feed-span">配置WIFI联网参数:</label>  
                    </view>  
                </view>  
                <u-cell-group>  
                    <u-cell title="WIFI名称" :border="true">  
                        <u--input slot="right-icon" customStyle="margin-left: -30%" placeholder="请输入WIFI名称"  
                            border="bottom" v-model="m_strWifiName" clearable>  
                        </u--input>  
                    </u-cell>  
                    <u-cell title="WIFI密码" :border="true">  
                        <u--input slot="right-icon" customStyle="margin-left: -30%" placeholder="请输入WIFI密码"  
                            border="bottom" v-model="m_strWifiPass" clearable>  
                        </u--input>  
                    </u-cell>  
                    <u-cell title="信号强度" :border="true">  
                        <u--input slot="right-icon" customStyle="margin-left: -30%" placeholder="WIFI信号强度"  
                            border="bottom" :readonly="true" v-model="m_strWifiLevel" clearable>  
                        </u--input>  
                    </u-cell>  
                </u-cell-group>  
            </block>  
            <block v-else>  
                <view class="maring-center">  
                    <view class="feed-name">  
                        <view class="feed-tag" />  
                        <label class="feed-span">配置4G联网参数:</label>  
                    </view>  
                </view>  
                <u-cell title="4G模块版本" title-width="90px" required :value="m_str4GVer" value-class="feed-value" />  
                <u-cell title="4GSIM卡号" title-width="90px" required :value="m_str4GSim" value-class="feed-value" />  
                <u-cell title="4G信号强度" title-width="90px" required :value="m_str4GLevel" value-class="feed-value" />  
            </block>  

            <view class="feed-bottom">  
                <view class="addr-footer">  
                    <view class="button-wrapper">  
                        <u-button type="info" custom-class="feed-btn" plain class="addr-right"  
                            bind:click="doTapBtnCancel"> 取 消 </u-button>  
                    </view>  
                    <view class="button-wrapper" style="width: 56px;">  

                    </view>  
                    <view class="button-wrapper">  
                        <u-button type="info" custom-class="feed-btn" class="addr-right" bind:click="doTapBtnSave"> 保 存  
                        </u-button>  
                    </view>  
                </view>  
            </view>  
        </block>  
    </view>  
</template>  

<script>  
    const STATE_LAB_CHECK = 0;  
    const STATE_LAB_MODE = 1;  
    const STATE_LAB_PARAM = 2;  
    const STATE_SET_MODE = 3;  
    const STATE_SET_APN = 4;  
    const STATE_SET_APS = 5;  

    const IOT_NOT_INIT = 0;  
    const IOT_WIFI_MODE = 1;  
    const IOT_4G_MODE = 2;  
    export default {  
        data() {  
            return {  
                m_str4GLevel: '未配置',  
                m_str4GVer: '未配置',  
                m_str4GSim: '未配置',  
                m_strWifiName: '',  
                m_strWifiPass: '',  
                m_strWifiLevel: '',  
                m_curDevice: null,  
                m_bgColor: '#fff',  
                m_show_auth: false,  
                m_has_unload: false,  
                m_show_tips: '正在读取蓝牙配置参数...',  
                m_operation_tips: '',  
                // 以下是查询蓝牙服务用到的变量...  
                m_bIsListenBlueSearchDevice: false,  
                m_bIsListenBlueStateChange: false,  
                m_nSelMode: IOT_WIFI_MODE.toString(),  
                m_netMode: [],  
                m_nCmdState: STATE_LAB_CHECK,  
                m_nIotMode: IOT_NOT_INIT,  
                m_blueDevice: null,  
            }  
        },  
        onUnload(options) {  
            console.log("关闭页面");  
            let theBlueDevice = this.m_blueDevice;  

            // 再次停止蓝牙搜索  
            uni.stopBluetoothDevicesDiscovery({  
                success(res) {  
                    console.log('再次停止蓝牙搜索成功 => ', res);  
                    uni.closeBluetoothAdapter  

                },  
                fail(res) {  
                    console.log('再次停止蓝牙搜索失败 => ', res);  
                }  
            })  

            // 断开蓝牙链接  
            uni.closeBLEConnection({  
                deviceId: theBlueDevice.serviceId,  
                success(res) {  
                    console.log('断开蓝牙成功 => ', res);  
                    uni.closeBluetoothAdapter  

                },  
                fail(res) {  
                    console.log('断开蓝牙失败 => ', res);  
                }  
            })  

            // 关闭蓝牙模块  
            uni.closeBluetoothAdapter({  
                success(res) {  
                    console.log('关闭蓝牙成功 => ', res);  
                    uni.closeBluetoothAdapter  

                },  
                fail(res) {  
                    console.log('关闭蓝牙失败 => ', res);  
                }  
            })  

        },  
        onLoad(options) {  
            let theAppData = getApp().globalData;  
            console.log("theAppData: ", theAppData);  
            let that = this;  

            this.m_device = theAppData.m_device;  
            this.m_netMode = theAppData.m_netMode;  

            // 获取wifi信号强度  
            // this.doGetDeviceSignal();  

            // 初始化蓝牙模块  
            uni.openBluetoothAdapter({  
                success(res) {  
                    console.log("初始化蓝牙蓝牙  => ", res)  
                    this.m_show_tips = "初始化蓝牙蓝牙";  

                    // 开始搜寻附近的蓝牙外围设备  
                    uni.startBluetoothDevicesDiscovery({  
                        success(res) {  
                            this.m_show_tips = "搜索蓝牙";  
                            console.log("搜索蓝牙  => ", res)  
                            // 搜索完成后停止搜索蓝牙  

                            // 监听寻找到新设备的事件  
                            uni.onBluetoothDeviceFound(function(devices) {  
                                this.m_show_tips = "搜索蓝牙列表完成";  
                                console.log('搜索到的蓝牙列表 => ', devices)  
                                that.doProcFoundDevices(devices.devices);  
                            })  

                        },  
                        fail(res) {  
                            this.m_show_tips = "搜索蓝牙失败";  
                            console.log('搜索蓝牙失败 => ', res);  
                        }  
                    })  
                },  
                fail(res) {  
                    this.m_show_tips = "初始化蓝牙蓝牙失败";  
                    console.log('初始化蓝牙蓝牙失败 => ', res);  
                }  
            })  
        },  
        methods: {  
            // ArrayBuffer转16进度字符串示例  
            ab2hex(buffer) {  
                const hexArr = Array.prototype.map.call(  
                    new Uint8Array(buffer),  
                    function (bit) {  
                        return ('00' + bit.toString(16)).slice(-2)  
                    }  
                )  
                return hexArr.join('')  
            },  
            // 统一的发现蓝牙设备的处理接口...  
            doProcFoundDevices: function(inArrDevice) {  
                this.m_show_tips = "正在寻找设备蓝牙";  
                for (var i = 0; i < inArrDevice.length; ++i) {  
                    if (typeof inArrDevice[i].name != 'string') {  
                        continue;  
                    }  
                    // 分割名称字符串,通过设备标记进行识别...  
                    let curTYDevice = this.m_device;  
                    let theBlueKey = curTYDevice.device_sn;  
                    let theDeviceName = inArrDevice[i].name;  
                    let theBlueDevice = this.m_blueDevice;  
                    // 将找到的蓝牙记录分别放入显示列表当中...  
                    this.m_show_tips = "正在寻找设备蓝牙: " + curTYDevice.device_sn;  
                    // 判断蓝牙的名称是不是当前设备的标识 device_sn  
                    if (theDeviceName == theBlueKey) {  
                        this.m_show_tips = "已找到设备蓝牙";  
                        if (theBlueDevice == null) {  
                            theBlueDevice = inArrDevice[i];  
                            this.onTapBlueDevice(theBlueDevice);  
                        } else {  
                            if (theBlueDevice.deviceId != inArrDevice[i].deviceId) {  
                                theBlueDevice = inArrDevice[i];  
                                this.onTapBlueDevice(theBlueDevice);  
                            }  
                        }  
                        setTimeout(() => {  
                            // 立即停止搜索蓝牙设备的指令...  
                            uni.stopBluetoothDevicesDiscovery({  
                                success(res) {  
                                    console.log('停止搜索蓝牙 => ', res)  
                                }  
                            })  
                        }, 1000);  

                    }  
                }  
            },  

            // 调用统一的接口进行蓝牙设备的信息读取...  
            onTapBlueDevice: function(inBlueDevice) {  
                let that = this;  
                inBlueDevice.isFoundOK = false;  
                inBlueDevice.isConnectOK = false;  

                that.m_blueDevice = inBlueDevice;  
                that.m_show_tips = '已经找到匹配的蓝牙设备:' + inBlueDevice.name;  

                // 重置蓝牙设备的服务数据区...  
                inBlueDevice.serviceData = {  
                    m_listServices: [],  
                    m_nCurServiceNum: 0,  
                    m_bWriteCharacter: false,  
                    m_bReadCharacter: false,  
                    m_bNotifyCharacter: false,  
                    m_bIndicateCharacter: false,  
                    m_curBlueDeviceName: inBlueDevice.name,  
                };  
                // 开始尝试连接指定的蓝牙设备...  
                that.m_show_tips = '正在连接蓝牙:' + inBlueDevice.name;  
                uni.createBLEConnection({  
                    deviceId: inBlueDevice.deviceId,  
                    success(res) {  
                        that.m_show_tips = '连接蓝牙设备成功';  
                        console.log('连接蓝牙设备成功 => ', res);  

                        // 获取已连接蓝牙设备服务 => 输入设备对象...  
                        // 要延迟2秒后再执行方法。不然取不到值  
                        setTimeout(() => {  
                            that.doGetBlueSeviceId(inBlueDevice);  
                        }, 3000);  
                    },  
                    fail: function(res) {  
                        that.m_show_tips = '连接蓝牙设备失败';  
                        console.log('连接蓝牙设备失败 => ', res);  
                    }  
                });  
            },  
            // 获取已连接蓝牙设备的服务信息...  
            doGetBlueSeviceId: function(inBlueDevice) {  
                let that = this;  
                this.m_show_tips = "获取设备蓝牙信息中...";  
                uni.getBLEDeviceServices({  
                    deviceId: inBlueDevice.deviceId,  
                    success: function(res) {  
                        that.m_show_tips = "获取蓝牙设备的服务信息成功";  
                        console.log('获取蓝牙设备的服务信息成功 => ', res);  
                        // 将当前设备所有的服务列表保存起来备用...  
                        inBlueDevice.serviceData.m_listServices = res.services;  
                        // 遍历所有服务的特征值 => 输入蓝牙设备对象...  

                        that.doGetBlueCharacteristics(inBlueDevice);  
                    },  
                    fail: function(res) {  
                        that.m_show_tips = "获取蓝牙设备的服务信息失败";  
                        console.log('获取蓝牙设备的服务信息失败 => ', res);  
                    }  
                });  
            },  
            // 获取蓝牙设备的特征值...  
            doGetBlueCharacteristics(inBlueDevice) {  
                var that = this;  
                var theServiceData = inBlueDevice.serviceData;  
                var list = theServiceData.m_listServices;  
                var num = theServiceData.m_nCurServiceNum;  

                var write = theServiceData.m_bWriteCharacter;  
                var read = theServiceData.m_bReadCharacter;  
                var notify = theServiceData.m_bNotifyCharacter;  
                var indicate = theServiceData.m_bIndicateCharacter;  

                var inDeviceId = inBlueDevice.deviceId;  
                var inServiceId = list[num].uuid;  

                this.m_show_tips = "获取蓝牙设备的特征值";  
                uni.getBLEDeviceCharacteristics({  
                    deviceId: inDeviceId,  
                    serviceId: inServiceId,  
                    success: (res) => {  
                        that.m_show_tips = "获取蓝牙服务的特征值成功";  
                        console.log('获取蓝牙服务的特征值成功 => ', res);  
                        for (let i = 0; i < res.characteristics.length; i++) {  

                            let properties = res.characteristics[i].properties;  
                            let item = res.characteristics[i].uuid;  
                            // 保存通知接口标识符...  
                            if (!notify && properties.notify) {  
                                let notifyId = {  
                                    charaterId: item,  
                                    serviceId: inServiceId,  
                                    properties: properties,  
                                }  
                                inBlueDevice.notifyId = notifyId;  
                                notify = true;  
                            }  
                            // 保存写入接口标识符...  
                            if (!write && properties.write) {  

                                let writeId = {  
                                    charaterId: item,  
                                    serviceId: inServiceId,  
                                    properties: properties,  
                                }  

                                inBlueDevice.writeId = writeId;  
                                write = true;  
                            }  
                            // 保存读取接口标识符...  
                            if (!read && properties.read) {  
                                let readId = {  
                                    charaterId: item,  
                                    serviceId: inServiceId,  
                                    properties: properties,  
                                }  
                                inBlueDevice.readId = readId;  
                                read = true;  
                            }  
                            if(!indicate && properties.indicate){  
                                let indicateId = {  
                                    charaterId: item,  
                                    serviceId: inServiceId,  
                                    properties: properties,  
                                }  
                                inBlueDevice.indicateId = indicateId;  
                                indicate = true;  

                            }  
                        }                         
                        // 三个接口全部找全了,跳出返回...  
                        if (write && notify && read && indicate) {  
                            // 设置已经找到蓝牙设备特征值标志状态...  
                            inBlueDevice.isFoundOK = true;  
                            inBlueDevice.isConnectOK = true;  
                            // 在界面显示查找结果...  
                            that.m_blueDevice = inBlueDevice,  
                                that.m_show_tips = '已成功连接蓝牙设备:' + inBlueDevice.name  

                            // 立即让当前页面开启蓝牙状态监听...  
                            uni.onBLEConnectionStateChange(that.listenConnectionChange);  

                            // 设置最大传输单元...  
                            that.m_show_tips = '设置最大传输单元'  
                            uni.setBLEMTU({  
                                deviceId: inDeviceId,  
                                mtu: 512,  
                                success(res) {  
                                    that.m_show_tips = 'setBLEMTU 成功'  
                                    console.log('setBLEMTU 成功 => ', res);  
                                },  
                                fail(res) {  
                                    that.m_show_tips = 'setBLEMTU 失败'  
                                    console.log('setBLEMTU 失败 => ', res);  
                                }  
                            });  

                            // 设置蓝牙为通知读取状态...  
                            that.doMakeBlueNotifyMode();  

                            // 1.8秒之后发送蓝牙指令...  
                            setTimeout(function() {  
                                that.m_show_auth = true;  
                                // 立即向蓝牙设备发送状态验证指令,等待回复...  
                                that.m_nCmdState = STATE_LAB_CHECK;  
                                that.doWriteBlueMachineCmd("SetLabel:CHK_LABEL", true,inBlueDevice.notifyId,inBlueDevice.notifyId);  
                            }, 2000);  
                            // 完成返回...  
                            return;  
                        }  

                        // 只要有个接口没有,继续查询服务...  
                        theServiceData.m_bWriteCharacter = write;  
                        theServiceData.m_bReadCharacter = read;  
                        theServiceData.m_bNotifyCharacter = notify;  
                        theServiceData.m_bIndicateCharacter = indicate;  
                        theServiceData.m_nCurServiceNum = ++num;  
                        // 如果已经到达最大的服务数...  
                        if (num === list.length) {  
                            // 弹框提示,中断查询...  
                            uni.showModal({  
                                showCancel: false,  
                                title: '蓝牙设备:' + inDeviceId,  
                                content: '找不到该设备的读写特征值',  
                                success(res) {  
                                    // 连接失败,需要重置设备标志...  
                                    inBlueDevice.isFoundOK = false;  
                                    inBlueDevice.isConnectOK = false;  
                                    that.setData({  
                                        m_blueDevice: inBlueDevice  
                                    });  
                                }  
                            })  
                        } else {  
                            console.log('继续进行服务功能查询工作 => ', inBlueDevice);  
                            // 继续进行服务功能查询工作...  
                            that.doGetBlueCharacteristics(inBlueDevice);  
                        }  
                    },  
                    fail: function(res) {  
                        console.log('获取蓝牙服务的特征值失败 => ', res);  
                    },  
                    complete: function(res) {  
                        console.log("write:" , inBlueDevice.writeId);  
                        console.log("read:" , inBlueDevice.readId);  
                        console.log("notify:" , inBlueDevice.notifyId);  
                        console.log("indicate:" , inBlueDevice.indicateId);  
                    }  
                });  
            },  
            // 设置数据变化通知...  
            doMakeBlueNotifyMode() {  
                var that = this;  
                // 监听蓝牙设备的数据变化...  
                var theBlueDevice = that.m_blueDevice;  
                uni.onBLECharacteristicValueChange(function(res) {  
                    console.log('characteristic value => ', res);  
                    console.log("inBuffer.byteLength:",res.value.byteLength)  

                    var value = that.ab2hex(res.value);  
                    console.log("ArrayBuffer转16进度字符串完成:", value)  

                    if(res.deviceId == theBlueDevice.deviceId){  
                        // 调用本地接口对蓝牙模块数据进行解析...  
                        that.doParseBlueChange(res.value);  
                    }  
                });  
            },  
            // 对蓝牙数据变化进行解析...  
            doParseBlueChange(inBuffer) {  
                // 立即关闭加载框...  
                // 如果输入的数据长度不是有效的,直接返回...  
                if (inBuffer == null || inBuffer.byteLength <= 0) {  
                    return;  
                }  

                var strValue = '';  
                // 将ArrayBuffer转换成DataView对象...  
                var theDataView = new DataView(inBuffer);  
                for (var i = 0; i < inBuffer.byteLength; i++) {  
                    strValue += String.fromCharCode(theDataView.getUint8(i));  
                }  
                // 将获取到的蓝牙数据打印处理,并根据数据进行分发...  
                console.log('16进度转字符串:=> ', strValue);  

                var nCmdState = this.m_nCmdState;  
                if (nCmdState == STATE_LAB_CHECK) {  
                    // 处理验证指令的反馈结果...  
                    this.doCmdCheckBlue(strValue);  
                } else if (nCmdState == STATE_LAB_MODE) {  
                    // 获取当前联网模式...  
                    this.doCmdModeBlue(strValue);  
                } else if (nCmdState == STATE_LAB_PARAM) {  
                    //处理联网模式的数据  
                    this.doCmdParamBlue(strValue);  
                } else if (nCmdState == STATE_SET_MODE) {  
                    // this.doCmdSetModeBlue(strValue);  
                } else if (nCmdState == STATE_SET_APN) {  
                    // this.doCmdSetAPNBlue(strValue);  
                } else if (nCmdState == STATE_SET_APS) {  
                    // this.doCmdSetAPSBlue(strValue);  
                }  
            },  
            // 处理验证指令的反馈结果...  
            doCmdCheckBlue(inValue) {  
                // 如果不是天愈设备,弹出错误信息...  
                if (inValue != "THIS_TY_DEVICE") {  
                    var strMsg = "蓝牙模块配置错误:" + inValue;  
                    this.setData({  
                        m_show_tips: strMsg  
                    });  
                    // Notify(strMsg);  
                    return;  
                }  
                // 立即设置当前处于下一个IOT模式验证状态...  
                this.m_nCmdState = STATE_LAB_MODE;  

                var theBlueDevice = this.m_blueDevice;  
                // 如果是有效的天愈设备,设置状态,发送模式指令到蓝牙设备...  
                this.doWriteBlueMachineCmd("SetLabel:MODE_LABEL", true,theBlueDevice.notifyId,theBlueDevice.indicateId);  
            },  
            // 获取当前联网模式...  
            doCmdModeBlue(inValue) {  
                var arrMode = inValue.split(':');  
                var strMsg = "获取IOT工作模式失败!";  
                if ((arrMode.length != 2) || arrMode[0] != "MODE") {  
                    this.m_show_tips = strMsg;  
                    return;  
                }  
                // 解析得到的IOT设备工作模式...  
                var nMode = parseInt(arrMode[1]);  
                nMode = isNaN(nMode) ? 0 : nMode;  
                // 设置状态...  
                this.m_bgColor = '#eee';  
                this.m_nIotMode = nMode;  
                this.m_nSelMode = nMode.toString();  
                this.m_nCmdState = STATE_LAB_PARAM;  
                // 根据不同的模式进行不同的指令发送...  
                if (nMode == IOT_NOT_INIT) {  
                    this.doWriteBlueMachineCmd("SetLabel:MACSTA", true);  
                } else if (nMode == IOT_WIFI_MODE) {  
                    this.doWriteBlueMachineCmd("SetLabel:WIFI_PARAMS", true);  
                } else if (nMode == IOT_4G_MODE) {  
                    this.doWriteBlueMachineCmd("SetLabel:4G_PARAMS", true);  
                }  
            },  
            // 处理联网模式的数据  
            doCmdParamBlue(inValue) {  
                var arrParam = inValue.split(',');  

                var nMode = this.m_nIotMode;  
                if (nMode == IOT_NOT_INIT) {  
                    this.doParseInitParam(inValue);  
                } else if (nMode == IOT_WIFI_MODE) {  
                    // 如果解析得到的数据不是数组,或者,数组长度低于3,返回失败...  
                    if (!(arrParam instanceof Array) || (arrParam.length < 3)) {  
                        // Notify("获取WIFI配置参数失败!");  
                        return;  
                    }  

                    // 显示参数到界面...  
                    this.m_strWifiName = arrParam[0],  
                        this.m_strWifiPass = arrParam[1],  
                        this.m_strWifiLevel = arrParam[2]  
                } else if (nMode == IOT_4G_MODE) {  
                    // 如果解析得到的数据不是数组,或者,数组长度低于3,返回失败...  
                    if (!(arrParam instanceof Array) || (arrParam.length < 3)) {  
                        // Notify("获取4G配置参数失败!");  
                        return;  
                    }  
                    this.m_str4GVer = arrParam[0],  
                        this.m_str4GSim = arrParam[1],  
                        this.m_str4GLevel = arrParam[2]  
                }  
            },  

            // 相应蓝牙连接变化的事件响应接口...  
            listenConnectionChange: function(res) {  
                let that = this;  
                let theBlueDevice = this.m_blueDevice;  
                if (theBlueDevice == null) {  
                    return;  
                }  

                // 不是当前正在处理的蓝牙设备,不进行处理,直接返回...  
                if (theBlueDevice.deviceId != res.deviceId) {  
                    return;  
                }  

                // 打印当前发生连接状态变化的设备信息...  
                console.log("蓝牙设备连接发生变化 => ", res);  
                let strErrMsg = '蓝牙设备已断开,需要重连...';  
                // 如果是蓝牙打印机发生的连接中断事件 => 将状态变化更新到全局变量当中...  
                if (theBlueDevice.deviceId == res.deviceId && !res.connected) {  
                    theBlueDevice.isConnectOK = false;  
                    strErrMsg = '蓝牙设备已断开,需要重连...';  
                }  
                // 打印关闭日志...  
                console.log(strErrMsg);  
                // 没有发生界面卸载事件 => 关闭蓝牙适配器,等待重连...  
                if (!that.data.m_has_unload) {  

                    // 关闭蓝牙适配,关闭状态监听...  
                    uni.closeBluetoothAdapter({  
                        success(res) {  
                            console.log("关闭蓝牙适配器成功 => ", res);  
                        },  
                        fail(res) {  
                            console.log("关闭蓝牙适配器失败 => ", res);  
                        }  
                    })  
                    // 这个通知可以穿越界面显示在顶层...  
                    // Notify(strErrMsg);  
                }  
            },  

            // 立即发送蓝牙数据状态...  
            doWriteBlueMachineCmd(inCmd, showTips,sendId,receiveId) {  
                // 在界面和调试区显示指令通知信息...  
                var strMessage = "正在发送蓝牙指令: " + inCmd;  
                // 需要显示提示信息...  
                if (showTips) {  
                    this.m_show_tips = strMessage;  
                }  
                var that = this;  
                var theNewBuf, dataView;  
                var nCmdByteSize = inCmd.length;  
                var theBlueDevice = that.m_blueDevice;  

                // 在这里创建了一个新的 ArrayBuffer 对象,其大小为输入命令字符串的长度。  
                var theNewBuf = new ArrayBuffer(nCmdByteSize);  

                // 这里创建了一个新的 DataView 对象,用于操作新创建的 ArrayBuffer。  
                var dataView = new DataView(theNewBuf);  

                // 在循环中,使用 setUint8 方法将输入命令字符串中第 i 个字符的 Unicode 编码值存储到新创建的 ArrayBuffer 中的对应位置。  
                for (var i = 0; i < nCmdByteSize; ++i) {  
                    dataView.setUint8(i, inCmd.charCodeAt(i));  
                }  

                // 向蓝牙设备发送指定长度的字节数据内容...  
                uni.writeBLECharacteristicValue({  
                    value: theNewBuf,  
                    deviceId: theBlueDevice.deviceId,  
                    serviceId: sendId.serviceId,  
                    characteristicId: sendId.charaterId,  
                    success: function(res) {  
                        console.log('发送蓝牙数据的特征值: ', sendId);  
                        console.log('发送蓝牙数据 \'' + inCmd + '\' 成功 => ', res);  
                        //读取返回信息  
                        that.doMakeReadBlueEvent(receiveId);  
                    },  
                    fail: function(res) {  
                        console.log('发送蓝牙数据的特征值: ', sendId);  
                        console.log('发送蓝牙数据 \'' + inCmd + '\' 失败 => ', res);  
                        // Notify('发送蓝牙数据失败!');  
                        uni.hideLoading();  
                    }  
                });  
            },  
            // 立即发起读取事件...  
            doMakeReadBlueEvent(receiveId) {  
                var theBlueDevice = this.m_blueDevice;  
                let that = this;  
                uni.readBLECharacteristicValue({  
                    deviceId: theBlueDevice.deviceId,  
                    serviceId: receiveId.serviceId,  
                    characteristicId: receiveId.charaterId,  
                    success: function(res) {  
                        console.log('接收数据的特征值: ', receiveId);  
                        console.log('获取蓝牙数据成功 => ', res);  
                    },  
                    fail: function(res) {  
                        console.log('接收数据的特征值: ', receiveId);  
                        console.log('获取蓝牙数据失败 => ', res);  
                    }  
                });  
            },  

            // =============================================  
            // 点击某个联网模式...  
            onModeClick(inEvent) {  
                let strCurMode = inEvent;  
                let nCurMode = parseInt(strCurMode);  
                // 设置IOT模式...  
                this.m_nIotMode = nCurMode;  
                this.m_nSelMode = strCurMode;  
                // this.m_nCmdState = STATE_SET_MODE;  
                this.m_nCmdState = STATE_LAB_PARAM;  

                this.m_str4GVer = null;  
                this.m_str4GSim = null;  
                this.m_str4GLevel = null;  
                this.m_strWifiName = null;  
                this.m_strWifiPass = null;  
                this.m_strWifiLevel = null;  

                // 当进行模式切换之后,需要立即进行配置...  
                let strCmd = "SetMode:" + strCurMode;  

                var theBlueDevice = this.m_blueDevice;  
                // 如果是有效的天愈设备,设置状态,发送模式指令到蓝牙设备...  
                this.doWriteBlueMachineCmd(strCmd, true,theBlueDevice.notifyId,theBlueDevice.readId);  
                this.m_nSelMode = inEvent;  
            },  
        }  
    }  
</script>  

<style>  
    .feed-back {  
        min-width: 100%;  
        min-height: 100%;  
        position: absolute;  
    }  

    .feed-contain {  
        padding: 15rpx 0 0;  
    }  

    .feed-line {  
        line-height: 30px !important;  
        align-items: center;  
    }  

    .feed-card {  
        font-size: 14px !important;  
        padding: 10px 15px !important;  
    }  

    .icon-blue {  
        font-size: 20px !important;  
        color: deepskyblue;  
    }  

    .feed-name {  
        display: flex;  
        flex-wrap: wrap;  
        margin: 20rpx 0rpx 10rpx;  
        justify-content: space-between;  
    }  

    .feed-tag {  
        background-color: #0f8eff;  
        margin: 8rpx 8rpx 0 0;  
        padding-right: 10rpx;  
        height: 30rpx;  
    }  

    .feed-span {  
        /*width: 120rpx;*/  
        font-size: 32rpx;  
        font-weight: 550;  
        line-height: 42rpx;  
        color: #555;  
        flex: 1;  
    }  

    .feed-value {  
        text-align: center !important;  
    }  

    .feed-bottom {  
        padding: 18rpx 26rpx;  
        background-color: white;  
        bottom: 0;  
        width: 100%;  
        display: flex;  
        flex-wrap: wrap;  
        justify-content: space-around;  
        border-top: 1px solid #eee;  
        z-index: 1000;  
    }  

    .addr-footer {  
        text-align: center;  
        padding: 8px 8px;  
    }  

    .button-wrapper {  
        display: inline-block;  
        padding: 0;  
    }  

    .addr-right {  
        margin-right: 30rpx;  
    }  

    .feed-btn {  
        min-width: 90px !important;  
        height: 40px !important;  
    }  
</style>

操作步骤:

使用Android平板发送数据就会如此
SetLabel:CHK_LABEL 页面启动时会发送,但是基本都是没见过失败
SetLabel:MODE_LABEL 页面启动时会发送,没成功过
SetMode:1 点击时发送的命令,差不多10多次成功一次
SetMode:2 点击时发送的命令,差不多10次成功一次

预期结果:

{
"errMsg": "readBLECharacteristicValue:ok"
}

{
"deviceId": "A0:76:4E:53:8D:66",
"serviceId": "000000FF-0000-1000-8000-00805F9B34FB",
"characteristicId": "0000FF01-0000-1000-8000-00805F9B34FB",
"value": {}
}

实际结果:

{
"errMsg": "readBLECharacteristicValue:fail property not support",
"errCode": 10007,
"code": 10007
}

bug描述:

在接收接收数据时经常会出现 1007错误,但是一直点击发送数据其中的某次数据就可以接收到。不管发送频率的快慢,我隔个3-5秒再次发送,接收也是1007错误。
发送和接收的特征值;
{
"charaterId": "00002B29-0000-1000-8000-00805F9B34FB",
"serviceId": "00001801-0000-1000-8000-00805F9B34FB",
"properties": {
"read": true,
"write": true,
"notify": false,
"indicate": false
}
}

2024-08-19 14:25 负责人:无 分享
已邀请:

要回复问题请先登录注册