当前位置:网站首页>微信小程序通过低功耗蓝牙设备进行定位及测距(二)
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
2022-04-23 14:02:00 【1029179954】
定位及测距的原理
微信小程序搜索附近蓝牙设备,通过指定蓝牙名称获取信号轻度rssi,再通过信号强度转化为距离,进而实现定位和测距。(具体实现看代码)
信号响度转距离的公式

代码实现
var pointBRSSi = res.devices[i].RSSI;
var iRssi = Math.abs(pointBRSSi);
var power = (iRssi-55)/(10*2.0);
var pointBDistance = Math.pow(10, power);
微信小程序定位及测距
js
//搜索附近蓝牙设备 同时找到hc-09rssi 转换为距离
toClock1: function () {
var that = this;
//console.log(that.data.timer)
//蓝牙初始化
wx.openBluetoothAdapter({
success: function (res) {
console.log("初始化蓝牙适配器");
/*getBluetoothAdapterState() 获取本机蓝牙适配器状态,判断是否可用,available为false则因为用户没有开启系统蓝牙*/
wx.getBluetoothAdapterState({
success:function (res) {
//打印相关信息
console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);
//适配器可用
if(res.available){
//clearTimeout(timer);
//搜索蓝牙设备
wx.startBluetoothDevicesDiscovery({
services: [],
allowDuplicatesKey: false,
success: function (res) {
//获取蓝牙设备输出信息列表
wx.getBluetoothDevices({
success: function (res) {
console.log('搜设备数目:' + res.devices.length)
console.log('设备信息:\n' + JSON.stringify(res.devices)+"\n")
for (var i = 0; i < res.devices.length; i++) {
//console.log("第"+(i+1) + "个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离手机的距离是:"+pointBDistance)
that.setData({
dis:pointBDistance
})
//大于5米 震动
if(pointBDistance>5){
wx.vibrateLong();
}
}
}
}
})
},
fail: function (err) {
console.log(err);
wx.showModal({
title: '温馨提示',
content: '搜索蓝牙失败'
})
}
});
}else{
wx.showModal({
title: '温馨提示',
content: '蓝牙设备不可用'
})
}
},
fail: function (res) {
//打印相关信息
console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);
wx.showModal({
title: '温馨提示',
content: '蓝牙设备不可用'
})
}
})
},
fail: function (err) {
console.log(err);
wx.showToast({
title: '蓝牙初始化失败',
icon: 'fail',
duration: 2000
})
//搜索超时 停止扫描设备
setTimeout(function () {
var that = this;
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log("停止搜索" + JSON.stringify(res.errMsg));
}
})
}, 4000);
}
});
// },10000)
},
关于微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙可以参考学习下面的文章:
https://blog.csdn.net/baidu_38978508/article/details/123439507?spm=1001.2014.3001.5502
更多关于微信小程序知识及javaweb开发知识关注下面公众号可获取更多源码:

版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/123441079
边栏推荐
- 程序编译调试学习记录
- SPC简介
- UML Unified Modeling Language
- 解决方案架构师的小锦囊 - 架构图的 5 种类型
- Express中间件③(自定义中间件)
- Decentralized Collaborative Learning Framework for Next POI Recommendation
- linux MySQL数据定时dump
- AtomicIntegerArray源码分析与感悟
- Ptorch classical convolutional neural network lenet
- Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
猜你喜欢
随机推荐
Android 面试主题集合整理
Basic knowledge learning record
cnpm的诡异bug
UML Unified Modeling Language
Record a strange bug: component copy after cache component jump
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
JS brain burning interview question reward
快捷键(多行)
蓝绿发布、滚动发布、灰度发布,有什么区别?
Reading notes: Secure federated matrix factorization
linux安装mysql后修改密码
项目中遇到的问题(五)操作Excel接口Poi的理解
第十五章 软件工程新技术
Express ② (routage)
mysql通过binlog文件恢复数据
函数只执行第一次的执行一次 once函数
Atcoder beginer contest 248c dice sum (generating function)
Universal template for scikit learn model construction
Using Jupiter notebook in virtual environment
Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]









