当前位置:网站首页>微信小程序通过低功耗蓝牙设备进行定位及测距(二)
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
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
边栏推荐
猜你喜欢
scikit-learn構建模型的萬能模板
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
The art of automation
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
低频量化之明日涨停预测
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
cnpm的诡异bug
随机推荐
cnpm的诡异bug
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
基础知识学习记录
AtomicIntegerArray源码分析与感悟
Special test 05 · double integral [Li Yanfang's whole class]
Ptorch classical convolutional neural network lenet
变长参数__VA_ARGS__ 和 写日志的宏定义
[code analysis (2)] communication efficient learning of deep networks from decentralized data
[code analysis (3)] communication efficient learning of deep networks from decentralized data
接口文档yaml
Neuron and neural network
mysql通过binlog文件恢复数据
STM32 learning record 0007 - new project (based on register version)
_模_板_
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
读了一篇博客,重新理解闭包整理一下
【项目】小帽外卖(八)
Jiannanchun understood the word game
JMeter pressure test tool
Decimal 格式化小数位/DateTime 转换处理