更新记录
1.0.1(2023-01-03) 下载此版本
新增项目相关的蓝牙协议prot.js及其延伸js
1.0.0(2022-12-08) 下载此版本
首次提交,实现了蓝牙通信的api及简单页面(还包含项目需要的蓝牙协议等)
平台兼容性
uni-app
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 | 
|---|---|---|---|---|---|---|---|---|
| √ | √ | - | - | √ | - | - | - | - | 
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 | 
|---|---|---|---|---|---|---|---|---|---|---|
| √ | √ | - | - | - | - | - | - | - | - | - | 
yinrh-blue
蓝牙插件
- 蓝牙搜索/连接的UI页面
 - 蓝牙API(详见blue.js)
 - 需要导入
uni_modules/yinrh-core插件 
组件介绍
yinrh-blue.uve 自定义蓝牙组件
- 在
template中,直接使用,无需import 
blue.js 蓝牙操作API
1、导入js
import blueJs from '@/uni_modules/yinrh-blue/components/yinrh-blue/js/blue.js'
2、配置UUID
blueJs.config('meter_cj');
3、设置监听
blueJs.listener({
    stateChanged: (res) => {},
    notifyChanged: (res) => {},
});
4、定位,部分Android手机需要打开定位才能搜索到蓝牙设备
import locationJs from '@/uni_modules/yinrh-core/components/yinrh-core/js/location.js'
locationJs.location({
    that:this,
});
5、初始化蓝牙,调用blueJs.openAsync或者blueJs.open方法
blueJs.openAsync({
    that: this,
});
6、搜索蓝牙设备,调用blueJs.found方法
blueJs.found({
    deviceId: '', // 已连接的蓝牙设备ID
    prepare: (val) => {
        // 开始搜索的回调方法,用于重置数据等
    },
    finish: (val) => {
        // 结束搜索的回调方法,用于重置数据等
    },
    reject: (err) => {
        // 失败的回调方法
    },
    resolve: (dev) => {
        // 发现设备的回调方法,返回发现的设备
    },
});
7、连接设备,调用blueJs.connectAsync方法
let _connect = await blueJs.connectAsync({
    connect: '', // 已经连接的设备ID
    current: '', // 即将连接的设备ID
    that: this, // vue的this对象
    prepare: () => {
        // 准备连接的回调方法
    },
    finish: () => {
        // 完成连接的回调方法
    },
    result: (id) => {
        // 连接成功的回调方法
    },
}).catch((err) => {
    // 连接失败(包括mtu、notify、services等等的失败)的回调方法
});
8、发送指令到蓝牙设备
blueJs.sendCammand({
    deviceId: '', // 已经连接的设备ID
    buffer: [], // ArrayBuffer数组
});
prot.js 协议操作API
1、导入js
import protJs from '@/uni_modules/yinrh-blue/components/yinrh-blue/js/prot.js'
2、配置协议(配置密钥keys和类型type)
protJs.config('meter_cj');
3、封装指令(ArrayBuffer)
ArrayBuffer ab = protJs.buffer({
    meterId: '', // 表ID(8字节)
    index: -1, // =[-1|0|1], 密钥索引值,0-未开户、1-已开户,-1-不加密
    data: '', // 数据对象内容
    dataId: '3004', // 数据对象ID(2个字节)
    control:  '84', // 控制域(1字节)
    version: '17', // 协议框架版本(1字节)
});
4、透传指令(ArrayBuffer)
ArrayBuffer ab = protJs.report('');
5、处理数据(蓝牙返回的数据)
protJs.notify(buffer, {
    report: () => {
        // 透传的回调方法
    },
});
yinrh-blue/node_modules/crypto-js
- 引用第三方库
 

                                                                    
                                                                        收藏人数:
                                    
                                                        下载插件并导入HBuilderX
                                                    
                                            下载示例项目ZIP
                                        
                                        赞赏(0)
                                    
                                            
 下载 2080
                
 赞赏 0
                
            
                    下载 10688502 
                
                        赞赏 1797 
                    
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                                赞赏
                            
            
京公网安备:11010802035340号