Offset is outside the bounds of the DataView;at api notifyBLECharacteristicValueChanged success call

人盡茶涼 提交于 2019-12-11 22:45:19

在做微信小程序开发,向蓝牙发送信息时,遇到这样一个错误

错误解释:
数组下标越界

报错原因:
我在设置ArrayBuffer时,直接设置过少,导致在循环通过蓝牙传递信息时,造成数组下标越界。

解决办法:
方法一 ,给ArrayBuffer长度设置更长;
方法二,在循环时,获取数组的长度,确保数组长度不超过循环次数。

修改后代码:

var  retstring = [1,2,3];
var buffer = new ArrayBuffer(14); //设置最多保存14个字节
var dataView = new DataView(buffer);
let values = 0;
   for (let i = 0; i < retstring .length; i++) {
     let str = retstring[i] + "";
      if (str.length>=2){
        values = str.charCodeAt();//字符转为ascii
      }
      dataView.setUint8(i, values);//向蓝牙发送命令参数
    }

详细报错信息:
Offset is outside the bounds of the DataView;at api notifyBLECharacteristicValueChanged success callback function
RangeError: Offset is outside the bounds of the DataView
at DataView.setUint8 ()
at be.modify_bleName (weapp:///pages/badminton/3D-pattern/pattern.js:611:16)
at success (weapp:///pages/badminton/3D-pattern/pattern.js:551:14)
at Function.o.(anonymous function) (debug:///[publib]:1:1293669)
at Object.eval [as success] (debug:///[publib]:1:125667)
at b (debug:///[publib]:1:523025)
at g (debug:///[publib]:1:523268)
at eval (debug:///[publib]:1:524658)
at Object.v (debug:///[publib]:1:1469)
at u (http://127.0.0.1:33423/remote-debug/runtime.js?devtools_ignore=true:1:8489)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!