小程序 蓝牙API使用全流程

其中需要注意的是消息格式

发送的信息应该转成相应的格式

javascript 复制代码
const combineData = (arr: any[]) => {
    const buffer = new ArrayBuffer(5)
    const dataView = new DataView(buffer)
    // 声明16进制数值 数组
    // 循环数组 并求和
    arr.forEach((item, index) => {
        dataView.setUint8(index, item)
    })
    const sum = arr.reduce((prev, curr) => {
        return prev + curr
    })
    dataView.setUint8(4, sum)
    return buffer as unknown as any[]
}

返回的信息也需要转换

javascript 复制代码
            const hex = Array.prototype.map
                .call(new Uint8Array(res.value), (bit) => {
                    return `00${bit.toString(16)}`.slice(-2)
                })
                .join('')
相关推荐
吴声子夜歌6 天前
小程序——布局示例
小程序
luffy54596 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序
Slow菜鸟6 天前
微信小程序开发(二)目录结构完全指南
微信小程序·小程序
小小王app小程序开发6 天前
海外盲盒小程序抽赏玩法分析(附跨境技术落地要点)
小程序
一叶星殇6 天前
微信小程序请求拦截器踩坑:避免重复刷新 token
微信小程序·小程序
hello kitty w6 天前
4. 基本语法
小程序
CHU7290356 天前
探索淘宝扭蛋机小程序:创意互动与趣味体验的融合
小程序
头发还在的女程序员6 天前
【免费下载】企业能源管理系统
小程序·策略模式·能源管理
吴声子夜歌7 天前
小程序——样式与布局
小程序
hello kitty w7 天前
3.小程序方法的封装
javascript·小程序