当前位置:网站首页>微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
2022-04-23 14:02:00 【1029179954】
前言:
微信小程序搜索附近蓝牙设备,必须先进行蓝牙初始化,初始化ok进行搜索附近蓝牙设备。连接指定蓝牙需要deviceId(硬件通过at指令可以查看deviceId)
准备工作:
软件:微信小程序
硬件:
蓝牙设备:hc-09
单片机:stm32
一:硬件部分
查看hc-09deviceId
硬件:
二:软件部分
软件
wxml
<button class="to-clock" hover-class="hover-to-clock" bindtap="toClock">蓝牙初始化</button>
js
//蓝牙初始化
wx.openBluetoothAdapter({
success: function (res) {
console.log("初始化蓝牙适配器");
/*getBluetoothAdapterState() 获取本机蓝牙适配器状态,判断是否可用,available为false则因为用户没有开启系统蓝牙*/
wx.getBluetoothAdapterState({
success:function (res) {
//打印相关信息
console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);
// res.available==true适配器可用 res.available==false适配器不可用
if(res.available){
//搜索蓝牙设备
wx.startBluetoothDevicesDiscovery({
services: [],
allowDuplicatesKey: false,
success: function (res) {
console.log("搜索蓝牙设备:"+res)
//获取蓝牙设备输出信息列表
wx.getBluetoothDevices({
success: function (res) {
console.log('搜设备数目:' + res.devices.length)
console.log('设备信息:\n' + JSON.stringify(res.devices)+"\n")
//微信小程序连接蓝牙 hc-09
wx.createBLEConnection({
deviceId:"2C:AB:33:33:94:08",
success: function (res) {
console.log('连接成功输出信息:' + res)
wx.hideLoading()
wx.showModal({
title: '温馨提示',
content: '蓝牙连接成功'
})
},
fail: function () {
wx.hideLoading()
wx.showModal({
title: '温馨提示',
content: '蓝牙连接失败'
})
},
})
}
})
},
fail: function (err) {
wx.hideLoading()
console.log(err);
wx.showModal({
title: '温馨提示',
content: '搜索蓝牙失败'
})
}
});
}else{
wx.hideLoading()
wx.showModal({
title: '温馨提示',
content: '蓝牙设备不可用'
})
}
},
fail: function (res) {
//打印相关信息
console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);
wx.hideLoading()
wx.showModal({
title: '温馨提示',
content: '蓝牙设备不可用'
})
}
})
},
fail: function (err) {
wx.hideLoading()
console.log(err);
wx.showToast({
title: '蓝牙初始化失败',
icon: 'success',
duration: 2000
})
setTimeout(function () {
wx.hideToast()
}, 2000)
}
});
更多微信小程序知识及蓝牙通信知识关注下面公众号
版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/123439507
边栏推荐
- 网站_收藏
- New关键字的学习和总结
- redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
- Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
- Qt Designer怎样加入资源文件
- Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
- 美联储数字货币最新进展
- Autumn recruitment in 2021, salary ranking No
- [code analysis (7)] communication efficient learning of deep networks from decentralized data
- Programming travel function
猜你喜欢
随机推荐
json反序列化匿名数组/对象
Problems encountered in the project (V) understanding of operating excel interface poi
Neuron and neural network
服务器中挖矿病毒了,屮
JS force deduction brush question 102 Sequence traversal of binary tree
多线程 @Async 线程池
Pytorch 经典卷积神经网络 LeNet
crontab定时任务输出产生大量邮件耗尽文件系统inode问题处理
Jenkins construction and use
Oracle告警日志alert.log和跟踪trace文件中文乱码显示
Nodejs安装及环境配置
变长参数__VA_ARGS__ 和 写日志的宏定义
Port occupied 1
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
Express②(路由)
Express ② (routing)
Postman reference summary
村上春树 --《当我谈跑步时,我谈些什么》句子摘录
mysql新表,自增id长达20位,原因竟是......
Yarn online dynamic resource tuning