当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
leetcode--380. O (1) time insertion, deletion and acquisition of random elements
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
快捷键(多行)
Decimal 格式化小数位/DateTime 转换处理
Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
使用Postman进行Mock测试
1256:献给阿尔吉侬的花束
request模块
poi操作word模板替换数据并且导出word
Go语言 RPC通讯
Record a strange bug: component copy after cache component jump
Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)
Restful WebService和gSoap WebService的本质区别
Prediction of tomorrow's trading limit of Low Frequency Quantization
JS force deduction brush question 102 Sequence traversal of binary tree
L2-024 tribe (25 points)
Switch usage (wechat applet)
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
Jacob print word
Nacos+AspnetCore+Ocelot实战编码







