uni-app安卓APP在用BLE蓝牙时获取不到服务信息,一直报错10004

uni-app安卓APP在用BLE蓝牙时获取不到服务信息,一直报错10004

开发环境 版本号 项目创建方式
Windows win11 HBuilderX
产品分类:uniapp/App

PC开发环境操作系统:Windows

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

HBuilderX类型:正式

HBuilderX版本号:4.75

手机系统:Android

手机系统版本号:Android 13

手机厂商:vivo

手机机型:vivox80

页面类型:vue

vue版本:vue3

打包方式:云端

操作步骤:

uni.getBLEDeviceServices({ deviceId, success(res) { // 检查页面状态 if (!that.isPageActive) { console.log(‘页面已离开,忽略服务获取结果’); return; }

    console.log("设备的服务:", res.services);
    console.log('服务数量:', res.services ? res.services.length : 0);

    if (res.services && res.services.length > 0) {
        that.servicesList = res.services;
        that.addLog(`成功获取到 ${res.services.length} 个服务`, 'success');
        that.isConnected = true; // 成功获取服务后设置连接状态

        // 获取每个服务的特征值
        res.services.forEach((service) => {
            that.getDeviceCharacteristics(deviceId, service.uuid);
        });

        // 启动连接保持机制
        that.startConnectionKeepAlive();
    } else {
        console.warn('未获取到任何服务,继续尝试...');
        that.addLog('未获取到任何服务,继续尝试...', 'info');
        // 延时后继续尝试
        that.scheduleRetry(() => that.getDeviceServices(deviceId));
    }
},
fail(err) {
    // 检查页面状态
    if (!that.isPageActive) {
        console.log('页面已离开,忽略服务获取错误');
        return;
    }

    console.log("获取服务失败:", err);
    that.addLog(`获取服务失败: ${err.errMsg} (错误码: ${err.errCode})`, 'error');

    // 如果是连接错误,标记为未连接
    if (err.errCode === 10004) {
        that.isConnected = false;
        that.addLog('检测到连接已断开', 'error');
        return; // 连接断开时不再重试
    }

    // 延时后继续尝试
    that.addLog('延时后继续尝试获取服务...', 'info');
    that.scheduleRetry(() => that.getDeviceServices(deviceId));
}

}, 1000); // 修改为1秒延时



预期结果: 获取实际的值

实际结果: 获取服务失败: { “errMsg”: “getBLEDeviceServices:fail no connection”, “code”: 10004 }

bug描述: 获取服务失败: { “errMsg”: “getBLEDeviceServices:fail no connection”, “code”: 10004 }


更多关于uni-app安卓APP在用BLE蓝牙时获取不到服务信息,一直报错10004的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app安卓APP在用BLE蓝牙时获取不到服务信息,一直报错10004的实战教程也可以访问 https://www.itying.com/category-93-b0.html


连接成功后延迟1秒再去获取服务

回到顶部