uni-app 蓝牙传输

https://www.cnblogs.com/ckfuture/p/16450418.html

https://www.cnblogs.com/yangxiaobai123/p/16021058.html

字符串转base64:https://www.cnblogs.com/sunny3158/p/17312158.html 

将 ArrayBuffer 对象转成 Base64 字符串:基础 - uni.arrayBufferToBase64 - 《uni-app API 文档》 - 书栈网 · BookStack 

ArrayBuffer:https://zhuanlan.zhihu.com/p/655456833 

最近在做一个项目,要求app通过蓝牙连接设备并将报文传输至设备中,在这个过程踩过了几个坑,总结如下:

根据uni-app官网API主要涉及到“蓝牙”和“低功耗蓝牙”两个部分。

主要步骤:

步骤1:初始化蓝牙模块 openBluetoothAdapter

            $openBluetoothAdapter(){
                uni.openBluetoothAdapter({
                    success: (res) => {
                        //初始化成功,搜索设备
                        console.log('openBluetoothAdapter success', res);
                        uni.showLoading({
                           title: '搜索设备中'
                        });
                        setTimeout(()=>{
                            this.baseList=[];//每次扫码清空设备列表,不然会导致重复
                            this.$startBluetoothDevicesDiscovery();//扫码蓝牙设备
                        },100);
                        
                        //定时关闭搜索设备
                        setTimeout(()=>{
                            this.$stopBluetoothDevicesDiscovery();
                            uni.hideLoading();
                        },10*1000);
                        
                    },
                    fail: (res) => {
                        uni.showToast({
                            title: '请打开蓝牙',
                            duration: 1000
                        });
                        if (res.errCode === 10001) {
                            //监听蓝牙适配器状态变化事件
                            uni.onBluetoothAdapterStateChange(function(res){
                                console.log('onBluetoothAdapterStateChange', res);
                                if (res.available) {
                                    //开始扫描
                                    this.$startBluetoothDevicesDiscovery()
                                }
                            })
                        }
                    }
                })
            },

步骤2:搜索附近的蓝牙外围设备 startBluetoothDevicesDiscove

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值