lanya
- 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
- })
lanya的更多相关文章
- PC上面的蓝牙的通信(C#)
添加引用InTheHand.Net.Personal.dll 首先创建一个蓝牙类 class LanYa { public string blueName { get; set; } //l蓝牙名字 ...
- pom.xml文件模板、application文件模板、configuration逆向生成文件、
pom: <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:// ...
- python爬虫入门(2)re模块-正则表达式
正则表达式 search //匹配第一次遇到符合规则的 匹配IP地址 import re re.search(r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3 ...
- PC蓝牙通信C#代码实现
PC蓝牙通信C#代码实现 这篇文章主要为大家详细介绍了PC蓝牙通信C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了C#实现PC蓝牙通信代码,供大家参考,具体内容如下 ...
随机推荐
- 学习Python3 天眼查 爬虫
刚开始学习Python,不愿意看基础,记忆不好,那些语法记不住,直接上个项目,这样比较深刻 刚好公司有个情况要查企业的信息,就想做个爬虫吧,有验证码的不愿意搞,那是个老大难问题,就选择了天眼查 过程都 ...
- sql,求和小于一定值的数据行
select count(id),sum(Price) from [T_AddPrice] as a --order by id
- c# 执行javascript 脚本
/// <summary> /// 执行JS /// this.ExecuteScript("get('{0}')".FormatWith(token0), File. ...
- docker 快速部署ES集群 spark集群
1) 拉下来 ES集群 spark集群 两套快速部署环境, 并只用docker跑起来,并保存到私库. 2)弄清楚怎么样打包 linux镜像(或者说制作). 3)试着改一下,让它们跑在集群里面. 4) ...
- net use远程重启服务器
在命令行工具中分别输入如下3条命令 net use \\10.10.1.100\ipc$ Password /user:Username shutdown -f -r -m \\10.10.1.1 ...
- Restful OData Protocol
Web服务有两种实现方式: 一是SOAP协议方式 二是REST方式. SOAP是一套完整的实现Web服务的解决方案.这里有必要先简单了解SOAP方式的Web服务,然后对比SOAP方式,我们会发现RES ...
- UEFI引导过程及windows引导修复
UEFI启动是一种新的主板引导项.传统引导方式就是Legacy模式. CSM的选项是UEFI模拟Legacy模式启动,选中后则可使用Legacy模式启动机器. Legacy模式仅支持传统的MBR分区, ...
- ORC 文件存储格式
1.orc列式存储概念 a)列式存储:orc并不是纯粹的列式存储,也是先基于行对数据表进行分组(行组),然后对行组进行列式存储. b)查询数据的时候不需要扫描全部数据(磁盘IO),只需查询指定列即可. ...
- 招聘IT图书兼职作者(长期兼职)
招聘图书兼职作者(长期兼职),本公司有十多年的计算机图书出版经验,每年出版上百本编程类图书, 和清华大学出版社 电子工业出版社 机械工业出版社都有很好的合作.你可以不用按时上线,不用天天被boss盯着 ...
- 发布一个PHP包到Packagist, 然后使用Composer安装
Composer 能够方便的进行项目的依赖管理, 当我们发布一个包并且希望别人通过Composer安装的时候, 就需要将包发布到Composer的包仓库Packagist上面. 下面进行详细的说明一 ...