uni-app ios端使用uni.writeBLECharacteristicValue向设备发送关机指令后无success或fail回调,安卓正常

发布于 1周前 作者 gougou168 来自 Uni-App

uni-app ios端使用uni.writeBLECharacteristicValue向设备发送关机指令后无success或fail回调,安卓正常

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

PC开发环境操作系统:Windows

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

HBuilderX类型:正式

HBuilderX版本号:3.99

手机系统:iOS

手机系统版本号:iOS 12.4

手机厂商:苹果

手机机型:iphone6 plus

页面类型:vue

vue版本:vue2

打包方式:云端

项目创建方式:HBuilderX

### 示例代码:

```javascript
uni.writeBLECharacteristicValue({
deviceId: params.device_id,
serviceId: params.service_id,
characteristicId: params.characteristic_id,
value: hexStringToBuffer(params.command), // 这里的value是ArrayBuffer类型
success: (res) => {
console.log('writeBLECharacteristicValue success', res.errMsg) 

typeof params.exec == "function" && params.exec(res);  

},  
fail: (res) => {  
console.log('writeBLECharacteristicValue fail:', res)  

}     
})

操作步骤:

发送设备关机得指令时才没回调

预期结果:

只要写成功都应该有成功或失败得回调

实际结果:

只要写成功但没有成功或失败得回调

bug描述:

【报Bug】ios端使用uni.writeBLECharacteristicValue 向设备发送关机指令,writeBLECharacteristicValue 没有success或fail回调,安卓正常


3 回复

Android write成功后有回调,ios没有。是uni.writeBLECharacteristicValue的回调没有不是notify


在使用 uni.writeBLECharacteristicValue 向设备发送关机指令时,iOS端没有收到 successfail 回调,而安卓端正常,可能的原因和解决方法如下:

1. iOS BLE 限制

  • iOS 对 BLE 的操作有一些限制,比如在某些情况下,iOS 可能不会立即返回回调,或者在后台模式下 BLE 操作可能会被挂起。
  • 确保你的应用在前台运行,并且没有进入后台模式。

2. 设备状态

  • 在发送关机指令后,设备可能会立即断开连接,导致 iOS 无法收到回调。
  • 可以在发送关机指令后,延迟一段时间再断开连接,确保 iOS 能够收到回调。

3. BLE 服务/特征值

  • 确保你正在写入的 BLE 服务/特征值是正确的,并且设备支持该操作。
  • 检查设备的 BLE 服务/特征值是否与代码中配置的一致。

4. 重试机制

  • 在某些情况下,BLE 操作可能会失败,建议实现重试机制,在失败后重试几次。

5. 调试日志

  • uni.writeBLECharacteristicValue 前后添加日志,检查是否成功进入写操作。
  • 使用 uni.onBLECharacteristicValueChange 监听设备返回的值,确认设备是否收到了指令。

6. 代码示例

以下是一个简单的代码示例,展示了如何发送关机指令并处理回调:

uni.writeBLECharacteristicValue({
    deviceId: deviceId,
    serviceId: serviceId,
    characteristicId: characteristicId,
    value: arrayBuffer, // 关机指令的ArrayBuffer
    success: (res) => {
        console.log('writeBLECharacteristicValue success', res);
        // 延迟断开连接
        setTimeout(() => {
            uni.closeBLEConnection({
                deviceId: deviceId,
                success: (res) => {
                    console.log('closeBLEConnection success', res);
                },
                fail: (err) => {
                    console.error('closeBLEConnection fail', err);
                }
            });
        }, 1000); // 延迟1秒
    },
    fail: (err) => {
        console.error('writeBLECharacteristicValue fail', err);
        // 重试机制
        retryWriteBLECharacteristicValue(deviceId, serviceId, characteristicId, arrayBuffer);
    }
});

function retryWriteBLECharacteristicValue(deviceId, serviceId, characteristicId, arrayBuffer, retryCount = 3) {
    if (retryCount > 0) {
        uni.writeBLECharacteristicValue({
            deviceId: deviceId,
            serviceId: serviceId,
            characteristicId: characteristicId,
            value: arrayBuffer,
            success: (res) => {
                console.log('writeBLECharacteristicValue success', res);
            },
            fail: (err) => {
                console.error('writeBLECharacteristicValue fail', err);
                retryWriteBLECharacteristicValue(deviceId, serviceId, characteristicId, arrayBuffer, retryCount - 1);
            }
        });
    } else {
        console.error('writeBLECharacteristicValue failed after retries');
    }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!