如题,我们使用微信小程序BLE蓝牙对设备进行升级,每包数据<1K,拆分为20B一小段发送。测试时,使用苹果手机很慢,安卓手机则能很快发完。请问大家是否遇到类似的问题,有没有办法解决?
我们尝试了不同的写法,不同的发送延时,都能成功发送数据,但无法提升IOS写数据的速度。部分代码如下
//常量
/**单帧发送长度,测试时最大500 */
const _frameSize = 20;
/**接收成帧超时ms */
const _recTimeout = 500;
/**等待接收超时 */
const _waitRecTimeout = 2000;
/**蓝牙连接超时 */
const _connectTimeout = 11000;
/**发送延时ms,现在不需要了 */
const _writeDelay = 0;
/**
* 发送数据
* @param {ArrayBuffer} buffer 发送的数据
* @param {function} receiveCallback 接收结果回调
*/
function WriteBLEBuffer(buffer, receiveCallback) {
if (_isWritingFlag && receiveCallback) {
receiveCallback("正在发送,请稍后....", null);
return;
}
_isWritingFlag = true;
_receiveDataCallback = receiveCallback;
_writeBuffer = buffer;
_writeStart = 0;
_writeEnd = _frameSize;
//分包发送数据-延时发送
_writeBufferTimeoutFunc()
}
/**
* 延时发送数据处理
*/
function _writeBufferTimeoutFunc() {
//判断是否发送完成
if (_writeStart >= _writeBuffer.byteLength) {
log.AddCommLog("发送:", _writeBuffer);
_isWritingFlag = false;
//等待回应超时
_waitRecTimer = setTimeout(() => {
_endSending("等待回应超时", null);
return;
}, _waitRecTimeout);
return;
}
//发送下一包
if (_writeEnd > _writeBuffer.byteLength) {
_writeEnd = _writeBuffer.byteLength;
}
let newArr = _writeBuffer.slice(_writeStart, _writeEnd);
//等回应的处理方式--不好用,会更慢一点但也差不多
// wx.writeBLECharacteristicValue({
// deviceId: _deviceId,
// serviceId: _serviceId,
// characteristicId: _charWriteId,
// value: newArr,
// success: function (res) {
// console.log("发送成功:", util.formatTime(new Date(), true), newArr);
// setTimeout(() => {
// _writeStart += _frameSize;
// _writeEnd += _frameSize;
// _writeBufferTimeoutFunc();
// }, _writeDelay);
// },
// fail: function (res) {
// console.log("发送失败:", res.errMsg);
// },
// })
// 直接发的处理方式
_writeBLECharacteristicValue(newArr, _deviceId, _serviceId, _charWriteId);
_writeStart += _frameSize;
_writeEnd += _frameSize;
//延时
if (_writeDelay > 0) {
setTimeout(() => {
_writeBufferTimeoutFunc();
}, _writeDelay);
} else {
_writeBufferTimeoutFunc();
}
}
/**
* 底层发送数据
* @param {ArrayBuffer} buffer 发送数据
* @param {String} devId 设备ID
* @param {String} serId 服务ID
* @param {String} charId 通道ID
*/
function _writeBLECharacteristicValue(buffer, devId, serId, charId) {
// console.log("发送", util.formatTime(new Date()));
//向低功耗蓝牙设备特征值中写入二进制数据
wx.writeBLECharacteristicValue({
deviceId: devId,
serviceId: serId,
characteristicId: charId,
value: buffer,
success: function (res) {
// console.log("发送成功:", util.formatTime(new Date(), true), buffer);
},
fail: function (res) {
console.log("发送失败:", res.errMsg);
},
})
}
附上我们的通讯日志,可以看到发的真的很慢啊
IOS发送日志
update2.js? [sm]:227 进度:4.62962962962963 信息:正在发送第6包,共108包...
util.js? [sm]:19 发送成功: 2025-10-22 14:22:40.739 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:40.799 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:40.860 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:40.919 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:40.980 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.38 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.130 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.190 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.249 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.308 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.369 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.429 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.520 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.580 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.638 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.700 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.760 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.849 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.910 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:41.969 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:42.30 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:42.88 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:42.149 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:42.209 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:42.269 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:42.330 ArrayBuffer(20)
util.js? [sm]:19 发送成功: 2025-10-22 14:22:42.418 ArrayBuffer(10)
log.js? [sm]:19 [日志][2025-10-22 14:22:42.420] 发送:ab 0c 02 00 00 01 10 6c 00 05 00 00 02 00 00 01 f0 d0 fa 0d 99 48 60 0e 98 40 68 0d 99 88 60 0e 98 81 78 40 68 03 90 08 46 03 99 00 f0 2c fb 17 90 17 98 0e 99 8a 68 0c 31 0d 9b 03 f1 0c 0c 01 33 ee 46 ce f8 00 30 02 91 11 46 02 9a 63 46 07 f0 a8 fc 8d f8 63 00 b4 e0 19 98 00 21 41 70 b0 e0 1a 98 0c 90 19 98 0b 90 0c 98 80 88 0b 99 88 80 0c 98 c0 88 00 28 0a d1 ff e7 42 f2 18 40 c2 f2 00 00 41 f2 ba 51 4f f6 ff 72 42 52 ff e7 0c 98 c0 88 0b 99 c8 80 42 f2 18 40 c2 f2 00 00 41 f2 ba 51 40 5a 4f f6 ff 71 88 42 30 d0 ff e7 0b 98 c0 88 42 f2 18 41 c2 f2 00 01 41 f2 ba 52 89 5a 88 42 0b dc ff e7 0b 98 00 21 41 70 0c 98 80 68 0b 99 88 60 01 20 8d f8 6f 00 77 e0 0b 98 c0 88 42 f2 18 41 c2 f2 00 01 41 f2 ba 52 89 5a 01 31 88 42 0a dd ff e7 b6 fa 88 00 0a 02 00 0a 00 08 0b 98 01 21 41 70 0c 98 80 68 0b 9a 90 60 8d f8 6f 10 60 e0 ff e7 ff e7 0b 98 c0 88 41 02 01 20 00 f0 b6 fa 17 90 17 98 0c 99 8a 68 0c 31 0b 9b 03 f1 08 0c 01 33 ee 46 ce f8 00 30 01 91 11 46 01 9a 63 46 07 f0 32 fc 8d f8 63 00 9d f8 63 00 01 28 36 d1 ff e7 0b 98 40 78 00 28 31 d1 ff e7 0b 98 c0 88 42 f2 18 41 c2 f2 00 01 41 f2 ba 52 88 52 88 5a 0b 99 89 88 01 39 88 42 20 d1 ff e7 07 f0 1c fd 8d f8 63 00 9d f8 63 00 00 28 04 d1 ff e7 0b 98 01 21 41 70 08 e0 42 f2 18 40 c2 f2 00 00 41 f6 79 21 01 22 42 54 ff e7 42 f2 18 40 c2 f2 00 00 41 f2 ba 51 4f f6 ff 72 42 52 ff e7 ff e7 03 e0 19 98 01 21 41 70 ff e7 9d f8 63 00 8d f8 6f 00 ff e7 9d f8 6f 00 1c b0 80 bd 00 00 80 b5 ad f5 99 6d 00 20 ad f8 c6 04 42 f2 18 40 c2 f2 00 00 00 f5 76 61 04 91 0d f1 16 01 03 91 4f f4 96 62 02 90 08 46 11 46 ff f7 fd fb 3c c1 ab----这是重复打码的完整包数据内容,上面是20B拆分的包
util.js? [sm]:19 2025-10-22 14:22:42.449 收到Notify数据: ArrayBuffer(19)
以上日志为等待回调的情况下打的,不等回调速度快一点点也差不多;
Android发送日志,安卓同样大小一包发出去,400ms内已经收到回应了,测试环境为同样微信小程序代码和同一台接收端,同样内容,同样大小的包(截取日志的包号不同)
update2.js:227 进度:3.3936651583710407 信息:正在发送第31包,共884包...
appservice.app.js:118 [日志][2025-10-21 14:20:43.84]发送:ab 0c 02 00 00 01 10 74 03 1e 00 00 02 00 00 dc 31 c2 f2 00 01 07 20 81 f8 4b 00 9e e0 9d f8 81 10 40 f6 dc 30 ae 57 88 00 0a 02 00 3c 01 08 c2 f2 00 00 00 eb 41 00 90 f8 42 00 8d f8 82 00 9d f8 82 00 01 28 15 db ff e7 40 f6 dc 30 c2 f2 00 00 41 6b 9d f8 82 00 40 5c 01 28 0a d1 ff e7 40 f6 dc 31 c2 f2 00 01 0a 6d 00 20 10 70 81 f8 4a 00 e8 e2 40 f6 64 50 c2 f2 00 00 41 f2 a8 21 40 5c ff 28 5d d0 ff e7 40 f6 64 50 c2 f2 00 00 41 f2 a8 21 40 5c 1f 99 9d f8 81 20 02 eb 42 02 01 eb c2 01 89 78 88 42 4b d0 ff e7 40 f6 dc 31 c2 f2 00 01 0a 6d 05 20 10 70 00 20 81 f8 4a 00 40 f6 64 51 c2 f2 00 01 41 f2 a9 22 01 20 88 54 42 f2 24 10 c2 f2 00 00 d0 f8 f8 00 00 78 78 b3 ff e7 9d f8 81 00 40 f6 dc 33 c2 f2 00 03 07 93 1a 46 02 f8 59 0f 9d f8 81 00 03 eb 40 00 90 f8 43 00 83 f8 5a 00 07 20 83 f8 5b 00 01 20 83 f8 5c 00 93 f8 4b 10 83 f8 5d 10 93 f8 4e 10 83 f8 5e 10 59 6b 09 7a 83 f8 5f 10 93 f8 41 10 83 f8 60 10 47 f2 95 11 c0 f6 07 01 03 f0 cd fb ff e7 81 e2 40 f6 dc 31 c2 f2 00 01 07 20 81 f8 4b 00 ff e7 ff e7 07 e0 40 f6 dc 31 c2 f2 00 01 00 20 81 f8 4a 00 ff e7 40 f6 dc 30 c2 f2 00 00 40 6b 00 7a 90 b1 ff e7 40 f6 dc 30 c2 f2 00 00 40 6b 00 7a 01 28 09 d0 ff e7 40 f6 dc 30 c2 f2 00 00 40 6b 00 7a 02 28 19 d1 ff e7 00 20 12 90 11 90 10 90 04 20 8d f8 40 00 01 20 05 90 8d f8 41 00 10 a8 06 90 06 f0 2a fe 05 99 06 98 05 22 8d f8 40 20 8d f8 41 10 06 f0 21 fe ff e7 3b e2 40 f6 dc 30 c2 f2 00 00 04 90 c0 6b 4e f0 17 fc 04 98 01 21 8d f8 74 10 00 f1 62 02 1e 92 c0 6b 1d aa 4e f0 3e fb 8d f8 83 00 9d f9 83 00 05 28 01 d1 ff e7 20 e2 9d f9 83 00 50 b9 ff e7 40 f6 dc 31 5b 10 ab
appservice.app.js:107 2025-10-21 14:20:43.337 收到Notify数据: ArrayBuffer(19) {byteLength: 19, maxByteLength: 19, resizable: false, detached: false}
appservice.app.js:118 [日志][2025-10-21 14:20:43.439]接收:ab 0d 00 01 00 00 10 00 74 03 1e 00 00 02 00 00 56 97 ab
