当前位置:网站首页>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
边栏推荐
猜你喜欢

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

基于ibeacons三点定位(微信小程序)

Intégration de Clusters CDH Phoenix basée sur la gestion cm

1256:献给阿尔吉侬的花束

RobotFramework 之 用例执行

基于Ocelot的gRpc网关

烟雾传感器(mq-2)使用详细教程(基于树莓派3b+实现)

Chrome插件 之 Selenium IDE、XPath 安装

趣谈网络协议

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
随机推荐
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
1256:献给阿尔吉侬的花束
关于pthread多线程一些好文章
基于Ocelot的gRpc网关
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
MySQL 修改主数据库
log4j 输出日志信息到文件中
百度图片识别自定义实现(替代AipOcr)
服务器日志分析工具(识别,提取,合并,统计异常信息)
DDT+Excel进行接口测试
使用itextpdf实现截取pdf文档第几页到第几页,进行分片
Wechat applet obtains login user information, openid and access_ token
1256: bouquet for algenon
Redis docker 安装
RobotFramework 之 用例标签机制
Strange bug of cnpm
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
pthread_self()为何重复了
Kettle -- control parsing
Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)