当前位置:网站首页>Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
2022-04-23 14:05:00 【one billion twenty-nine million one hundred and seventy-nine th】
Preface :
Wechat applet searches for nearby Bluetooth devices , Bluetooth initialization must be performed first , initialization ok Search for nearby Bluetooth devices . The specified Bluetooth connection requires deviceId( Hardware through at Commands can be viewed deviceId)
preparation :
Software : Wechat applet
Hardware :
Bluetooth devices :hc-09
Single chip microcomputer :stm32
One : hardware component
see hc-09deviceId
Hardware :
Two : Software part
Software
wxml
<button class="to-clock" hover-class="hover-to-clock" bindtap="toClock"> Bluetooth initialization </button>
js
// Bluetooth initialization
wx.openBluetoothAdapter({
success: function (res) {
console.log(" Initialize Bluetooth adapter ");
/*getBluetoothAdapterState() Get the status of the native Bluetooth adapter , Determine if it is available ,available by false Because the user does not turn on the system Bluetooth */
wx.getBluetoothAdapterState({
success:function (res) {
// Print related information
console.log(JSON.stringify(res.errMsg) + "\n Bluetooth is available :" + res.available);
// res.available==true Adapter available res.available==false Adapter not available
if(res.available){
// Search for Bluetooth devices
wx.startBluetoothDevicesDiscovery({
services: [],
allowDuplicatesKey: false,
success: function (res) {
console.log(" Search for Bluetooth devices :"+res)
// Get Bluetooth device output information list
wx.getBluetoothDevices({
success: function (res) {
console.log(' Number of search devices :' + res.devices.length)
console.log(' Equipment information :\n' + JSON.stringify(res.devices)+"\n")
// Wechat applet connects to Bluetooth hc-09
wx.createBLEConnection({
deviceId:"2C:AB:33:33:94:08",
success: function (res) {
console.log(' Connection success output information :' + res)
wx.hideLoading()
wx.showModal({
title: ' reminder ',
content: ' Bluetooth connection successful '
})
},
fail: function () {
wx.hideLoading()
wx.showModal({
title: ' reminder ',
content: ' Bluetooth connection failed '
})
},
})
}
})
},
fail: function (err) {
wx.hideLoading()
console.log(err);
wx.showModal({
title: ' reminder ',
content: ' Bluetooth search failed '
})
}
});
}else{
wx.hideLoading()
wx.showModal({
title: ' reminder ',
content: ' Bluetooth device not available '
})
}
},
fail: function (res) {
// Print related information
console.log(JSON.stringify(res.errMsg) + "\n Bluetooth is available :" + res.available);
wx.hideLoading()
wx.showModal({
title: ' reminder ',
content: ' Bluetooth device not available '
})
}
})
},
fail: function (err) {
wx.hideLoading()
console.log(err);
wx.showToast({
title: ' Bluetooth initialization failed ',
icon: 'success',
duration: 2000
})
setTimeout(function () {
wx.hideToast()
}, 2000)
}
});
More knowledge of WeChat applet and Bluetooth communication knowledge, and the following official account.

版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401297802.html
边栏推荐
- VsCode-Go
- Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]
- STM32 learning record 0007 - new project (based on register version)
- 微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
- 微信小程序 input隐藏和不可操作的设置
- Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
- L2-024 tribe (25 points)
- websocket
- 微信小程序基于udp协议与esp8266进行通信
- There is a mining virus in the server
猜你喜欢
随机推荐
Atcoder beginer contest 248c dice sum (generating function)
对List集合进行分页
快捷键(多行)
pthread_self()为何重复了
Nodejs安装及环境配置
CentOS mysql多实例部署
scikit-learn構建模型的萬能模板
Spark入门基本操作
Force deduction brush question 101 Symmetric binary tree
json反序列化匿名数组/对象
室内外地图切换(室内基于ibeacons三点定位)
容差分析相关的计算公式
scikit-learn构建模型的万能模板
生成随机高质量符合高斯分布的随机数
RobotFramework 之 用例执行
Oracle alarm log alert Chinese trace and trace files
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
基于微信小程序的wifi模块使用
Chapter I review of e-commerce spike products
POI operation word template replaces data and exports word








