当前位置:网站首页>微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
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
边栏推荐
猜你喜欢
Choreographer全解析
STM32学习记录0007——新建工程(基于寄存器版)
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
Interesting talk about network protocol
Express中间件③(自定义中间件)
Autumn recruitment in 2021, salary ranking No
Node接入支付宝开放平台的沙箱实现支付功能
33 million IOPs, 39 microsecond delay, carbon footprint certification, who is serious?
基础知识学习记录
nodejs通过require读取本地json文件出现Unexpected token / in JSON at position
随机推荐
网站_收藏
Express ② (routage)
Express②(路由)
centOS下mysql主从配置
JMeter pressure test tool
分页SQL
关于stream流,浅记一下------
对List集合进行分页
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
cnpm的诡异bug
L2-024 部落 (25 分)
linux安装mysql后修改密码
About note 1
美联储数字货币最新进展
生产环境——
Decentralized Collaborative Learning Framework for Next POI Recommendation
Choreographer full resolution
商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
Decimal 格式化小数位/DateTime 转换处理