一个控制蓝牙小灯的程序,在ios上和开发者工具里面都能正常的延时,但是就是安卓手机不可以。这是什么情况。
出问题的地方就是那个timer函数,安卓手机并没有执行延时而是直接跳过去了,进行下一步的指令了。
var app = getApp();
var display = new Array();
var inst = new Array();
var value = new Array();
var index = 0;
var inputValue = ‘’;
var inputInst = ‘’;
var current = 0;
var x = 0;
let buffer = new ArrayBuffer(20)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)
dataView.setUint8(1, 0)
dataView.setUint8(2, 0)
dataView.setUint8(3, 0)
dataView.setUint8(4, 0)
dataView.setUint8(5, 0)
dataView.setUint8(6, 0)
dataView.setUint8(7, 0)
dataView.setUint8(8, 0)
dataView.setUint8(9, 0)
dataView.setUint8(10, 0)
dataView.setUint8(11, 0)
dataView.setUint8(12, 0)
dataView.setUint8(13, 0)
dataView.setUint8(14, 0)
dataView.setUint8(15, 0)
dataView.setUint8(16, 0)
dataView.setUint8(17, 0)
dataView.setUint8(18, 0)
dataView.setUint8(19, 0)
Page({
data: {
instructions: [‘关闭’, ‘开启’, ‘延时’],
},
onUnload: function () {
wx.closeBLEConnection({
deviceId: app.globalData.targetAdd,
success: function (res) {
app.globalData.connectFlag = false,
app.globalData.searchFlag = false,
app.globalData.targetAdd = ‘NoAdd’,
console.log(res)
console.log(‘app.globalData.connectFlag’, app.globalData.connectFlag)
console.log(‘app.globalData.searchFlag’, app.globalData.searchFlag)
console.log(‘app.globalData.targetAdd’, app.globalData.targetAdd)
}
})
},
bindPickerChange: function (e) {
console.log(‘picker发送选择改变,携带值为’, e.detail.value)
if (e.detail.value == 0) {
inputInst = ‘关闭’;
}
if (e.detail.value == 1) {
inputInst = ‘开启’;
}
if (e.detail.value == 2) {
inputInst = ‘延时’;
}
this.setData({
index: e.detail.value
})
console.log('指令值: ', inputInst)
},
inputDone: function (e) {
inputValue = e.detail.value
},
inputInstructions: function () {
inst.push(inputInst)
console.log(‘指令数组’, inst)
value.push(inputValue)
console.log(‘值数组’, value)
display.push(inputInst + ": " + inputValue)
console.log(‘显示数组’, display)
this.setData({
display: display
})
},
deleteInstructions: function () {
inst.splice(inst.length - 1, 1)
value.splice(value.length - 1, 1)
display.splice(display.length - 1, 1)
this.setData({
display: display
})
},
run: function () {
if (current < inst.length) {
switch (inst[current]) {
case ‘开启’:
this.runStart();
break;
case ‘关闭’:
this.runStop();
break;
case ‘延时’:
this.runDelay();
break;
}
} else {
current = 0
}
},
runStart: function () {
dataView.setUint8(value[current] - 1, 1)
wx.writeBLECharacteristicValue({
deviceId: app.globalData.targetAdd,
serviceId: ‘0000FFF0-0000-1000-8000-00805F9B34FB’,
characteristicId: ‘0000FFF6-0000-1000-8000-00805F9B34FB’,
value: buffer,
success: function (res) {
console.log(‘writeBLECharacteristicValue success’, res)
},
fail: function (res) {
console.log(“wrong”, res)
}
})
console.log(inst[current], value[current], current)
current++;
this.run();
},
runStop: function () {
dataView.setUint8(value[current] - 1, 0)
wx.writeBLECharacteristicValue({
deviceId: app.globalData.targetAdd,
serviceId: ‘0000FFF0-0000-1000-8000-00805F9B34FB’,
characteristicId: ‘0000FFF6-0000-1000-8000-00805F9B34FB’,
value: buffer,
success: function (res) {
console.log(‘writeBLECharacteristicValue success’, res)
},
fail: function (res) {
console.log(“wrong”, res)
}
})
console.log(inst[current], value[current], current)
current++;
this.run();
},
runDelay: function () {
console.log(inst[current], value[current], current)
this.timer()
// if (x < value[current]) {
// this.timer();
// } else {
// x = 0
// current++
// this.run()
// }
},
timer: function () {
const that = this
setTimeout(function () {
// x++;
// console.log(‘x’, x)
current++
that.run()
}, value[current])
}
})
你好,请提供出现问题的机型和微信版本,以及能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。