找到的str转buffer的方法如下
export const str2buffer = (str: string): ArrayBuffer => {
return new Uint8Array(
unescape(encodeURIComponent(str)).split("").map((value) => {
return value.charCodeAt(0)
})
)
}
通过这个方法生成数据后,直接调用
wx.writeBLECharacteristicValue({
deviceId: this.data.deviceId,
serviceId: this.data.serviceId,
characteristicId,
value: str2buffer("123"),
success: (res) => {
}
})
提示我
{"errno":1001,"errMsg":"writeBLECharacteristicValue:fail parameter error: parameter.value should be ArrayBuffer instead of Uint8Array;"}
我用的ts,好奇为啥不能Uint8Array