当前位置:网站首页>微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
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
边栏推荐
- FBS(fman build system)打包
- elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
- Reading notes: Secure federated matrix factorization
- _模_板_
- Introduction to spark basic operation
- Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
- redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
- Choreographer全解析
- [code analysis (7)] communication efficient learning of deep networks from decentralized data
- 33 million IOPs, 39 microsecond delay, carbon footprint certification, who is serious?
猜你喜欢
随机推荐
The latest development of fed digital currency
容差分析相关的计算公式
Problems encountered in the project (V) understanding of operating excel interface poi
生产环境——
1256:献给阿尔吉侬的花束
Analysis and understanding of atomicintegerarray source code
神经元与神经网络
Spark入门基本操作
力扣刷题 101. 对称二叉树
美联储数字货币最新进展
linux安装mysql后修改密码
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
[code analysis (2)] communication efficient learning of deep networks from decentralized data
Jenkins construction and use
go 语言 数组,字符串,切片
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
接口文档yaml
AtomicIntegerArray源码分析与感悟
The art of automation
读了一篇博客,重新理解闭包整理一下








