当前位置:网站首页>Wechat applet positioning and ranging through low-power Bluetooth device (2)
Wechat applet positioning and ranging through low-power Bluetooth device (2)
2022-04-23 14:05:00 【one billion twenty-nine million one hundred and seventy-nine th】
Principle of positioning and ranging
Wechat applet searches for nearby Bluetooth devices , Get the signal by specifying the Bluetooth name rssi, Then it is transformed into distance through signal strength , And then realize positioning and ranging .( See code for specific implementation )
The formula of signal loudness to distance
Code implementation
var pointBRSSi = res.devices[i].RSSI;
var iRssi = Math.abs(pointBRSSi);
var power = (iRssi-55)/(10*2.0);
var pointBDistance = Math.pow(10, power);
Wechat applet positioning and ranging
js
// Search for nearby Bluetooth devices At the same time find hc-09rssi Convert to distance
toClock1: function () {
var that = this;
//console.log(that.data.timer)
// 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);
// Adapter available
if(res.available){
//clearTimeout(timer);
// Search for Bluetooth devices
wx.startBluetoothDevicesDiscovery({
services: [],
allowDuplicatesKey: false,
success: function (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")
for (var i = 0; i < res.devices.length; i++) {
//console.log(" The first "+(i+1) + " individual RSSI:" + res.devices[i].RSSI+"\n")
if(res.devices[i].name==="HC-09"){
var rssi=res.devices[i].RSSI
// var pointBRSSi = res.devices[i].RSSI;
var iRssi = Math.abs(rssi);
var power = (iRssi-55)/(10*2.0);
var pointBDistance = Math.pow(10, power);
console.log("hc-o9 What is the distance from the mobile phone :"+pointBDistance)
that.setData({
dis:pointBDistance
})
// Greater than 5 rice shock
if(pointBDistance>5){
wx.vibrateLong();
}
}
}
}
})
},
fail: function (err) {
console.log(err);
wx.showModal({
title: ' reminder ',
content: ' Bluetooth search failed '
})
}
});
}else{
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.showModal({
title: ' reminder ',
content: ' Bluetooth device not available '
})
}
})
},
fail: function (err) {
console.log(err);
wx.showToast({
title: ' Bluetooth initialization failed ',
icon: 'fail',
duration: 2000
})
// The search timeout Stop scanning the device
setTimeout(function () {
var that = this;
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log(" stop searching " + JSON.stringify(res.errMsg));
}
})
}, 4000);
}
});
// },10000)
},
About Bluetooth initialization of wechat applet 、 To search for nearby Bluetooth devices and specify Bluetooth connections, you can refer to the following articles :
https://blog.csdn.net/baidu_38978508/article/details/123439507?spm=1001.2014.3001.5502
More about wechat applet knowledge and javaweb Develop knowledge and pay attention to the following official account for more source code. :
版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401297761.html
边栏推荐
猜你喜欢
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
基于CM管理的CDH集群集成Phoenix
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
微信小程序的订阅号开发(消息推送)
Choreographer full resolution
linux安装mysql后修改密码
Pycharm连接远程服务器并实现远程调试
基于微信小程序的wifi模块使用
Detailed tutorial on the use of setinterval timing function of wechat applet
go 语言 数组,字符串,切片
随机推荐
leetcode--977. Squares of a Sorted Array
Neuron and neural network
PySide2
Mysql个人学习总结
腾讯根据ip解析地址
SQL数据库
使用Postman进行Mock测试
基于微信小程序的wifi模块使用
全局变量能否放在头文件中定义
VsCode-Go
Android篇:2019初中级Android开发社招面试解答(中
Wechat applet obtains login user information, openid and access_ token
Chrome插件 之 Selenium IDE、XPath 安装
服务器日志分析工具(识别,提取,合并,统计异常信息)
使用itextpdf实现截取pdf文档第几页到第几页,进行分片
centOS下mysql主从配置
Record a strange bug: component copy after cache component jump
visio安装报错 1:1935 2:{XXXXXXXX...
Pytorch 经典卷积神经网络 LeNet
分页SQL