当前位置:网站首页>微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
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
边栏推荐
- 美联储数字货币最新进展
- About note 1
- 1256: bouquet for algenon
- Express②(路由)
- Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
- Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
- leetcode--977. Squares of a Sorted Array
- linux安装mysql后修改密码
- Function executes only the once function for the first time
- Android篇:2019初中级Android开发社招面试解答(中
猜你喜欢

groutine

Oracle告警日志alert.log和跟踪trace文件中文乱码显示

Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)

Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计

Choreographer全解析

基于Ocelot的gRpc网关

As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture

初探 Lambda Powertools TypeScript

STM32学习记录0007——新建工程(基于寄存器版)

Universal template for scikit learn model construction
随机推荐
Programming travel function
groutine
mysql通过binlog文件恢复数据
JS 烧脑面试题大赏
关于stream流,浅记一下------
Neuron and neural network
Universal template for scikit learn model construction
3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
金蝶云星空API调用实践
Oracle告警日志alert.log和跟踪trace文件中文乱码显示
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
Decimal 格式化小数位/DateTime 转换处理
接口文档yaml
JMeter pressure test tool
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
服务器中挖矿病毒了,屮
FDFS start
Strange bug of cnpm
Pytorch 经典卷积神经网络 LeNet
多线程 @Async 线程池