当前位置:网站首页>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
边栏推荐
猜你喜欢
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
基于ibeacons签到系统
快速安装mongodb
Detailed tutorial on the use of setinterval timing function of wechat applet
1256:献给阿尔吉侬的花束
mysql新表,自增id长达20位,原因竟是......
Ptorch classical convolutional neural network lenet
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
微信小程序的订阅号开发(消息推送)
PyMySQL
随机推荐
生产环境——
Choreographer full resolution
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
RobotFramework 之 用例标签机制
Restful WebService和gSoap WebService的本质区别
RobotFramework 之 项目框架
变长参数__VA_ARGS__ 和 写日志的宏定义
There is a mining virus in the server
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
Detailed tutorial on the use of setinterval timing function of wechat applet
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
leetcode--977. Squares of a Sorted Array
Program compilation and debugging learning record
Force deduction brush question 101 Symmetric binary tree
POI operation word template replaces data and exports word
leetcode--380. O (1) time insertion, deletion and acquisition of random elements
理解虚基类、虚函数与纯虚函数的概念(转)
PyMySQL
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
9月8日,临去松山湖的前夜