微信小程序ibeacon三点定位详解
程序员文章站
2023-11-28 08:55:10
空余时间简单写了一个微信小程序ibeacon三点定位。
事先淘宝买了七八个ibeacon小设备,放置在办公司角落。分别设置三个ibeacon的位置坐标点,根据每一个ibe...
空余时间简单写了一个微信小程序ibeacon三点定位。
事先淘宝买了七八个ibeacon小设备,放置在办公司角落。分别设置三个ibeacon的位置坐标点,根据每一个ibeacon到已经开启蓝牙的目标物距离,计算出目标物在当前区域内坐标位置。适用于区域内购物指示。当然,进入该区域事先要打开手机蓝牙。
下面代码:
var app = getapp() page({ data: { motto: 'hello world', openble:'打开蓝牙设备', startblediscovery:'初始化蓝牙设备', startbledevices:'目标定位', restartsearchble:'重置蓝牙', startsearchble:'默认空', userinfo: {}, deviceid: '', searchflag:true, devicerssi:'', devicename:'', deviceid:[], advertisserviceuuids: [], advertisdata:[], canvaspointx:'', canvaspointy:'', avatarurl:'../index/bg-image.jpg', showposition:'' }, //事件处理函数 bindviewtap: function() { wx.navigateto({ url: '../logs/logs' }) }, onload: function () { var that = this //调用应用实例的方法获取全局数据 //that.setdata({ deviceid: opt.deviceid }) app.getuserinfo(function(userinfo){ //更新数据 that.setdata({ userinfo:userinfo }) //判断兼容性 if (wx.openbluetoothadapter) { //打开蓝牙适配器,如果没有打开 showtoast wx.openbluetoothadapter({ success: function(res){ // success //获取本机的蓝牙适配器状态 wx.getbluetoothadapterstate({ success: function(res){ // success that.setdata({ searchflag:true }) }, fail: function(res) { // fail that.setdata({ searchflag:false }) }, complete: function(res) { // complete } }) }, fail: function(res) { // fail 本机是否已经打开蓝牙设备 wx.showtoast({title:'请打开本机蓝牙设备,重新扫码', duration:10000 }) }, complete: function(res) { // complete } }) } else { // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示 wx.showmodal({ title: '提示', content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。' }) } }) }, startbledevices: function(){ var that = this wx.startbluetoothdevicesdiscovery({ services: [], success: function(res){ //获取本机蓝牙设备状态 // success that.setdata({ showposition:setinterval(that.devicesfunc,1000) }) }, fail: function(res) { // fail }, complete: function(res) { // complete } }) }, // startbledevices: function(){ // var that = this // that.setdata({ // showposition:setinterval(that.devicesfunc,1000) // }) // }, devicesfunc: function(){ var that = this wx.getbluetoothdevices({ success: function(res){ // console.log(res); var arrayrssi = new array(); var arraydevicename = new array(); var arraydeviceid = new array(); // var arrayuuids = new array(); var arrayadvertisdata = new array(); var pointadistance = ''; var pointbdistance = ''; var pointcdistance = ''; for(var i = 0; i<res.devices.length;i++){ //console.log(res.devices[i].name); if(res.devices[i].name.indexof('craft')==0){ //console.log(res.devices[i]); arrayrssi.push(res.devices[i].rssi); arraydevicename.push(res.devices[i].name); arraydeviceid[i]= res.devices[i].deviceid; arrayuuids[i]= res.devices[i].advertisserviceuuids[i]; arrayadvertisdata[i]= res.devices[i].advertisdata ; 调用计算rssi对应距离的函数 var irssi = math.abs(arrayrssi[i]); var power = (irssi-59)/(10*2.0); var mm = math.pow(10, power); console.log(arraydevicename[i]+"距离的位置是"+mm+"米"); 取01,02,03分别为,(2,0),(2,2),(0,2)固定坐标点,做定位 if(res.devices[i].name.indexof('craft01')==0){ var pointarssi = res.devices[i].rssi ; var irssi = math.abs(pointarssi); var power = (irssi-55)/(10*2.0); var pointadistance = math.pow(10, power); console.log("a"+pointadistance); console.log(pointarssi); } if(res.devices[i].name.indexof('craft02')==0){ var pointbrssi = res.devices[i].rssi; var irssi = math.abs(pointbrssi); var power = (irssi-55)/(10*2.0); var pointbdistance = math.pow(10, power); console.log("b"+pointbdistance); console.log(pointbrssi); } if(res.devices[i].name.indexof('craft03')==0){ var pointcrssi = res.devices[i].rssi; var irssi = math.abs(pointcrssi); var power = (irssi-57)/(10*2.0); var pointcdistance = math.pow(10, power); console.log("c"+pointcdistance); console.log(pointcrssi); } } } // 从 arrayrssi 取三个距离定位点最近的ibeacon参与定位 if(arrayrssi.length > 3){ //根据arrayrssi进行信号强弱排序.距离越远rssi值越小 for(var i = 0 ; i < arrayrssi.length ; i ++){ for(var j = i+1 ; j< arrayrssi.length ; j++){ if(arrayrssi[i]<arrayrssi[j]){ var select = arrayrssi[i]; arrayrssi[i] = arrayrssi[j]; arrayrssi[j] = select; } } } //获取最近的三个距离 for(var i = 0 ; i < 3; i++){ if(i==0){ var pointarssi = res.devices[i].rssi ; var irssi = math.abs(pointarssi); var power = (irssi-55)/(10*2.0); var pointadistance = math.pow(10, power); console.log("a"+pointadistance); console.log(pointarssi); } if(i==1){ var pointbrssi = res.devices[i].rssi; var irssi = math.abs(pointbrssi); var power = (irssi-55)/(10*2.0); var pointbdistance = math.pow(10, power); console.log("b"+pointbdistance); console.log(pointbrssi); } if(i==2){ var pointcrssi = res.devices[i].rssi; var irssi = math.abs(pointcrssi); var power = (irssi-57)/(10*2.0); var pointcdistance = math.pow(10, power); console.log("c"+pointcdistance); console.log(pointcrssi); } } } //获取定位点的x和y if(!pointadistance==''&&!pointbdistance==''&&!pointcdistance==''){ var pointdx='';var pointdy = ''; var p = math.pow(pointadistance,2)/10-math.pow(pointbdistance,2)/10; pointdx = 2.5 - p; var m = math.pow(pointadistance,2)/10-math.pow(pointcdistance,2)/10; pointdy = 2.5 - m; console.log('目标所在位置x是'+pointdx); console.log('目标所在位置y是'+pointdy); if(pointdx > 0 && pointdy > 0){ wx.showtoast({title:'欢迎进入25楼craft', duration:4000 }); } that.setdata({ canvaspointx:pointdx, canvaspointy:pointdy }) //创建画布 //计算坐标点在规定canvas上的位置显示 var context = wx.createcanvascontext(); context.setstrokestyle("#00ff00"); //a,b,c,d,e,f a,b起始坐标,c半径,d,e起始和终止角度 context.arc(that.data.canvaspointx*30,that.data.canvaspointy*30,5,0,2*math.pi); context.fill(); wx.drawcanvas({ canvasid: 'firstcanvas', actions: context.getactions() // 获取绘图动作数组 }) wx.showmodal({title:'x轴:'+pointdx+'y轴:'+pointdy, duration:5000 }); //开始网络请求 wx.request({ url: 'http://craftww.cn/weixinibeacon/index.php', data: {}, method: 'get', // options, get, head, post, put, delete, trace, connect // header: {}, // 设置请求的 header success: function(res){ // success }, fail: function(res) { // fail }, complete: function(res) { // complete } }) }else{ wx.showtoast({title:'正在搜索...', duration:1000 }) 开始网络请求 wx.request({ url: 'http://craftww.cn/weixinibeacon/index.php', data: {}, method: 'get', // options, get, head, post, put, delete, trace, connect // header: {}, // 设置请求的 header success: function(res){ // success }, fail: function(res) { // fail }, complete: function(res) { // complete } }) } }, fail: function(res) { // fail }, complete: function(res) { // complete } }) } , //动态显示 createlist: function(thisname){ var that = this that.setdata({ array:[{devicedistance:"1"},{devicedistance:"1"},{devicedistance:"1"},{devicedistance:"1"},{devicedistance:"1"}] }) }, restartsearchble: function(){ var that = this //清除本地数据缓存 wx.stopbluetoothdevicesdiscovery({ success: function(res){ // success }, fail: function(res) { // fail }, complete: function(res) { // complete } }) wx.clearstoragesync(); clearinterval(that.data.showposition); console.log(that.data.showposition); // that.context.clearrect(0,0,canvas.width,canvas.height); //断开蓝牙 var that = this wx.closebluetoothadapter({ success: function(res){ // success console.log('重置成功'); //打开蓝牙适配器,如果没有打开 showtoast wx.openbluetoothadapter({ success: function(res){ // success //获取本机的蓝牙适配器状态 wx.getbluetoothadapterstate({ success: function(res){ // success }, fail: function(res) { // fail }, complete: function(res) { // complete } }) }, fail: function(res) { }, complete: function(res) { // complete } }) }, fail: function(res) { // fail }, complete: function(res) { // complete } }) } //zheli })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。