n***@163.com
n***@163.com
  • 发布:2024-11-27 16:44
  • 更新:2025-08-28 16:46
  • 阅读:136

【报Bug】uni.notifyBLECharacteristicValueChange报错notifyBLECharacteristicValueChange:fail no descriptor

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win11

HBuilderX类型: 正式

HBuilderX版本号: 4.29

手机系统: Android

手机系统版本号: Android 10

手机厂商: OnePlus 6

手机机型: ONEPLUSA6000

页面类型: vue

vue版本: vue3

打包方式: 离线

项目创建方式: HBuilderX

示例代码:
<script setup>  
import { onLoad } from '@dcloudio/uni-app';  
import { ref } from 'vue';  

let deviceInfo = ref({});  
let result = ref(null);  
let device_service = ref([])  
let device_characteristics = ref([])  
let scanstorage = ref()  
let tmp_predict = ref()  
let NewMultiscan = ref()  
let mresult = ref()  
onLoad((options) => {  
    try {  
        deviceInfo.value = { ...JSON.parse(decodeURIComponent(options.device_info)) };  
        console.log("设备信息:", deviceInfo.value);  
    } catch (error) {  
        console.error("解析设备信息失败:", error);  
    }  
});  
function toScanAndPredict(){  
    console.log('开始扫描并且预测');  
    console.log(deviceInfo.value);  
    const deviceId = deviceInfo.value.deviceId;  
    let connect = false  
    uni.getBLEDeviceServices({  
        deviceId,  
        success(res){  
            console.log(res.services)  
            device_service.value = [...res.services]  
            const serviceId = device_service.value[9].uuid  
            uni.getBLEDeviceCharacteristics({  
                deviceId,  
                serviceId,  
                success(res){  
                    const characteristicIdn = res.characteristics[17].uuid  
                    const characteristicIdw = res.characteristics[16].uuid  
                    console.log(res.characteristics)  
                    device_characteristics.value = res.characteristics  
                    console.log(device_characteristics.value[5])  
                    addEventNotify(deviceId, device_service.value[9].uuid, device_characteristics.value[5])  
                },  
                fail(err){  
                    console.log("获取特征值失败:", err);  
                }  
            })  
        },  
        fail(err){  
            console.log("获取服务失败:", err);  
        }  
    })  
}  
function addEventNotify(deviceId, serviceId, characteristicId){  
    uni.onBLECharacteristicValueChange(res => {  
        if(ab2ten(res.value)[0] == 255){  
            uni.offBLECharacteristicValueChange()  
            let scanIndex = ab2ten(res.value)  
            let buffer = new ArrayBuffer(4)  
            let dataView = new DataView(buffer)  
            dataView.setUint8(0, scanIndex[1])  
            dataView.setUint8(1, scanIndex[2])  
            dataView.setUint8(2, scanIndex[3])  
            dataView.setUint8(3, scanIndex[4])  
            uni.notifyBLECharacteristicValueChange({  
                deviceId: deviceId,  
                serviceId: serviceId,  
                characteristicId: characteristicIdn,  
                state: true,  
                success(res) {  
                    console.log('return data n', res)  
                },  
            })  
            setTimeout(()=>{  
                uni.writeBLECharacteristicValue({  
                    deviceId: deviceId,  
                    serviceId: serviceId,  
                    characteristicId: characteristicIdw,  
                    value: buffer,  
                    success(res) {  
                        console.log('request data w', res)  
                    },  
                })  
            },2000)  
            let d = ref([])  
            uni.onBLECharacteristicValueChange(res => {  
                let a = ab2ten(res.value)  
                console.log(a)  
                if (ab2ten(res.value)[0] != 0) {  
                    a.shift()  
                    Array.prototype.push.apply(d, a);  
                }  
                if (ab2ten(res.value)[0] == 202) {  
                    uni.offBLECharacteristicValueChange()  
                    if (that.data.multiscan) {  
                        if (!scanstorage.value) {  
                            var scanstorage = []  
                        } else {  
                            var scanstorage = scanstorage.value  
                        }  
                        Array.prototype.push.apply(scanstorage, d);  
                        scanstorage.value = scanstorage  
                        tmp_predict.value = scanstorage  
                        NewMultiscan.value = false  
                        mresult.value = '已完成' + String(scanstorage.length / 3822) + '次扫描'  
                    }else{  
                        console.log('d')  
                        //发送数据  
                        tmp_predict.value = d  
                    }  
                }  
            })  
        }  
    });  

    uni.notifyBLECharacteristicValueChange({  
        deviceId: deviceId.toUpperCase(),  
        serviceId: serviceId.toUpperCase(),  
        characteristicId: device_characteristics.value[7].uuid.toUpperCase(),  
        state: true, // 启用 notify 功能  
        success: (res) => {  
            console.log('start scan n', res);  
        },  
        fail: (err)=>{  
            console.log({deviceId, serviceId})  
            console.log(device_characteristics.value[5])  
            console.log('开启扫描失败', err)  
        }  
    })  
}  
// ArrayBuffer转16进度字符串示例  
function ab2hex(buffer) {  
    var hexArr = Array.prototype.map.call(  
        new Uint8Array(buffer),  
        function (bit) {  
            return ('00' + bit.toString(16)).slice(-2)  
        }  
    )  
    return hexArr.join('');  
}  

// ArrayBuffer转10进度字符串示例  
function ab2ten(buffer) {  
    var hexArr = Array.prototype.map.call(  
        new Uint8Array(buffer),  
        function (bit) {  
            return (bit.toString(10))  
        }  
    )  
    return hexArr;  
}  

function ab2int(buffer) {  
    var hexArr = Array.prototype.map.call(  
        new Int16Array(buffer),  
        function (bit) {  
            return (bit.toString(10))  
        }  
    )  
    return hexArr;  
}  
</script>

操作步骤:
uni.notifyBLECharacteristicValueChange({  
        deviceId: deviceId.toUpperCase(),  
        serviceId: serviceId.toUpperCase(),  
        characteristicId: device_characteristics.value[7].uuid.toUpperCase(),  
        state: true, // 启用 notify 功能  
        success: (res) => {  
            console.log('start scan n', res);  
        },  
        fail: (err)=>{  
            console.log({deviceId, serviceId})  
            console.log(device_characteristics.value[5])  
            console.log('开启扫描失败', err)  
        }  
    })

预期结果:
console.log('start scan n', res);  

实际结果:
console.log({deviceId, serviceId})  
            console.log(device_characteristics.value[5])  
            console.log('开启扫描失败', err)  

bug描述:

报错信息
调用uni.notifyBLECharacteristicValueChange接口报错
"errMsg": "notifyBLECharacteristicValueChange:fail no descriptor","code": 10008
目前怀疑是uniapp识别不了相同特征值不同权限的蓝牙接口

2024-11-27 16:44 负责人:无 分享
已邀请:
用户2878492

用户2878492

请问最后解决了吗?我也遇到完全一样的问题

要回复问题请先登录注册