小程序
小游戏
企业微信
微信支付
扫描小程序码分享
wx.writeBLECharacteristicValue()分二个包发送数据,都提示success,蓝牙设备却只收到第二次发送的数据,是什么原因呢??
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
wx.writeBLECharacteristicValue() 返回ok,打印机没有反应,是怎么回事?
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
代码看看
write({
deviceId,
serviceId,
characteristicId,
value
}) {
console.log("value"+value)
var that = this
let e = value
return new Promise((resolve, reject) => {
for (var i = 0;i<e.length;i+=20) {
var endLength = 0
if (i+20<e.length) {
var senddata = e
let buffer = new ArrayBuffer(20)
let dataView = new DataView(buffer)
let dataSend = []
for (var j = i; j < i + 20; j++) {
dataView.setUint8(j - i, senddata[j])
dataSend.push(dataView.getUint8(j-i))
}
console.log('多包发送的包数据1**:'+dataSend)
wx.writeBLECharacteristicValue({
deviceId: deviceId+"",
serviceId: serviceId+'',
characteristicId: characteristicId+'',
value: buffer,
success: function (res) {
console.log('多包writeBLECharacteristicValue success',res.errMsg)
},
fail: function (res) {
console.log('发送失败')
})
that.sleep(0.05)
}else{
if (20 < e.length) {
endLength = senddata.length-i
endLength = senddata.length
let buffer = new ArrayBuffer(endLength)
for (var j = i; j < senddata.length; j++) {
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
wx.writeBLECharacteristicValue() 返回ok,打印机没有反应,是怎么回事?
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
代码看看
write({
deviceId,
serviceId,
characteristicId,
value
}) {
console.log("value"+value)
var that = this
let e = value
return new Promise((resolve, reject) => {
for (var i = 0;i<e.length;i+=20) {
var endLength = 0
if (i+20<e.length) {
var senddata = e
let buffer = new ArrayBuffer(20)
let dataView = new DataView(buffer)
let dataSend = []
for (var j = i; j < i + 20; j++) {
dataView.setUint8(j - i, senddata[j])
dataSend.push(dataView.getUint8(j-i))
}
console.log('多包发送的包数据1**:'+dataSend)
wx.writeBLECharacteristicValue({
deviceId: deviceId+"",
serviceId: serviceId+'',
characteristicId: characteristicId+'',
value: buffer,
success: function (res) {
console.log('多包writeBLECharacteristicValue success',res.errMsg)
},
fail: function (res) {
console.log('发送失败')
}
})
that.sleep(0.05)
}else{
var senddata = e
if (20 < e.length) {
endLength = senddata.length-i
}else{
endLength = senddata.length
}
let buffer = new ArrayBuffer(endLength)
let dataView = new DataView(buffer)
let dataSend = []
for (var j = i; j < senddata.length; j++) {
var length = bufferstr.byteLength;
var count = Math.ceil(length/20);
for(var i=0;i<count;i++){
//对buffer进行分包,最大不超过20字节
let TempBuffer;
if(((i+1)*20)<length){
TempBuffer = bufferstr.slice(i*20,(i+1)*20);
}else{
TempBuffer = bufferstr.slice(i*20,length);
}
this.Delayed(200).then( ()=>{ //延迟200ms
//写入设备
wx.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
value: TempBuffer, //写入数据
success: function (res) {
},
fail: function (res) {
},
})
} )
Delayed(ms,callback){
return new Promise(function(Resolve,Peject){
setTimeout(function(){
Resolve(callback);
},ms);
})
},