lanya

时间:2023-10-06 10:57:13
  1. var app = getApp()
  2. Page({
  3. data: {
  4. motto: 'Hello World',
  5. openBLE:'打开蓝牙设备',
  6. startBLEDiscovery:'初始化蓝牙设备',
  7. startBLEDevices:'目标定位',
  8. reStartSearchBLE:'重置蓝牙',
  9. startSearchBLE:'默认空',
  10. userInfo: {},
  11. deviceId: '',
  12. searchFlag:true,
  13. deviceRSSI:'',
  14. deviceName:'',
  15. deviceId:[],
  16. advertisServiceUUIDs: [],
  17. advertisData:[],
  18. canvasPointX:'',
  19. canvasPointY:'',
  20. avatarUrl:'../index/bg-image.jpg',
  21. showPosition:''
  22. },
  23. //事件处理函数
  24. bindViewTap: function() {
  25. wx.navigateTo({
  26. url: '../logs/logs'
  27. })
  28. },
  29. onLoad: function () {
  30. var that = this
  31. //调用应用实例的方法获取全局数据
  32. //that.setData({ deviceId: opt.deviceId })
  33. app.getUserInfo(function(userInfo){
  34. //更新数据
  35. that.setData({
  36. userInfo:userInfo
  37. })
  38. //判断兼容性
  39. if (wx.openBluetoothAdapter) {
  40. //打开蓝牙适配器,如果没有打开 showtoast
  41. wx.openBluetoothAdapter({
  42. success: function(res){
  43. // success
  44. //获取本机的蓝牙适配器状态
  45. wx.getBluetoothAdapterState({
  46. success: function(res){
  47. // success
  48. that.setData({
  49. searchFlag:true
  50. })
  51. },
  52. fail: function(res) {
  53. // fail
  54. that.setData({
  55. searchFlag:false
  56. })
  57. },
  58. complete: function(res) {
  59. // complete
  60. }
  61. })
  62. },
  63. fail: function(res) {
  64. // fail 本机是否已经打开蓝牙设备
  65. wx.showToast({title:'请打开本机蓝牙设备,重新扫码', duration:10000 })
  66. },
  67. complete: function(res) {
  68. // complete
  69. }
  70. })
  71. } else {
  72. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  73. wx.showModal({
  74. title: '提示',
  75. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  76. })
  77. }
  78. })
  79. },
  80. startBLEDevices: function(){
  81. var that = this
  82. wx.startBluetoothDevicesDiscovery({
  83. services: [],
  84. success: function(res){
  85. //获取本机蓝牙设备状态
  86. // success
  87. that.setData({
  88. showPosition:setInterval(that.devicesFunc,1000)
  89. })
  90. },
  91. fail: function(res) {
  92. // fail
  93. },
  94. complete: function(res) {
  95. // complete
  96. }
  97. })
  98. },
  99. // startBLEDevices: function(){
  100. // var that = this
  101. // that.setData({
  102. // showPosition:setInterval(that.devicesFunc,1000)
  103. // })
  104. // },
  105. devicesFunc: function(){
  106. var that = this
  107. wx.getBluetoothDevices({
  108. success: function(res){
  109. // console.log(res);
  110. var arrayRSSI = new Array();
  111. var arraydeviceName = new Array();
  112. var arraydeviceId = new Array();
  113. // var arrayUUIDs = new Array();
  114. var arrayadvertisData = new Array();
  115. var pointADistance = '';
  116. var pointBDistance = '';
  117. var pointCDistance = '';
  118. for(var i = 0; i<res.devices.length;i++){
  119. //console.log(res.devices[i].name);
  120. if(res.devices[i].name.indexOf('craft')==0){
  121. //console.log(res.devices[i]);
  122. arrayRSSI.push(res.devices[i].RSSI);
  123. arraydeviceName.push(res.devices[i].name);
  124. arraydeviceId[i]= res.devices[i].deviceId;
  125. arrayUUIDs[i]= res.devices[i].advertisServiceUUIDs[i];
  126. arrayadvertisData[i]= res.devices[i].advertisData ;
  127. 调用计算rssi对应距离的函数
  128. var iRssi = Math.abs(arrayRSSI[i]);
  129. var power = (iRssi-59)/(10*2.0);
  130. var mm = Math.pow(10, power);
  131. console.log(arraydeviceName[i]+"距离的位置是"+mm+"米");
  132. 取01,02,03分别为,(2,0),(2,2),(0,2)固定坐标点,做定位
  133. if(res.devices[i].name.indexOf('craft01')==0){
  134. var pointARSSi = res.devices[i].RSSI ;
  135. var iRssi = Math.abs(pointARSSi);
  136. var power = (iRssi-55)/(10*2.0);
  137. var pointADistance = Math.pow(10, power);
  138. console.log("a"+pointADistance);
  139. console.log(pointARSSi);
  140. }
  141. if(res.devices[i].name.indexOf('craft02')==0){
  142. var pointBRSSi = res.devices[i].RSSI;
  143. var iRssi = Math.abs(pointBRSSi);
  144. var power = (iRssi-55)/(10*2.0);
  145. var pointBDistance = Math.pow(10, power);
  146. console.log("b"+pointBDistance);
  147. console.log(pointBRSSi);
  148. }
  149. if(res.devices[i].name.indexOf('craft03')==0){
  150. var pointCRSSi = res.devices[i].RSSI;
  151. var iRssi = Math.abs(pointCRSSi);
  152. var power = (iRssi-57)/(10*2.0);
  153. var pointCDistance = Math.pow(10, power);
  154. console.log("c"+pointCDistance);
  155. console.log(pointCRSSi);
  156. }
  157. }
  158. }
  159. // 从 arrayRSSI 取三个距离定位点最近的ibeacon参与定位
  160. if(arrayRSSI.length > 3){
  161. //根据arrayRSSI进行信号强弱排序.距离越远rssi值越小
  162. for(var i = 0 ; i < arrayRSSI.length ; i ++){
  163. for(var j = i+1 ; j< arrayRSSI.length ; j++){
  164. if(arrayRSSI[i]<arrayRSSI[j]){
  165. var select = arrayRSSI[i];
  166. arrayRSSI[i] = arrayRSSI[j];
  167. arrayRSSI[j] = select;
  168. }
  169. }
  170. }
  171. //获取最近的三个距离
  172. for(var i = 0 ; i < 3; i++){
  173. if(i==0){
  174. var pointARSSi = res.devices[i].RSSI ;
  175. var iRssi = Math.abs(pointARSSi);
  176. var power = (iRssi-55)/(10*2.0);
  177. var pointADistance = Math.pow(10, power);
  178. console.log("a"+pointADistance);
  179. console.log(pointARSSi);
  180. }
  181. if(i==1){
  182. var pointBRSSi = res.devices[i].RSSI;
  183. var iRssi = Math.abs(pointBRSSi);
  184. var power = (iRssi-55)/(10*2.0);
  185. var pointBDistance = Math.pow(10, power);
  186. console.log("b"+pointBDistance);
  187. console.log(pointBRSSi);
  188. }
  189. if(i==2){
  190. var pointCRSSi = res.devices[i].RSSI;
  191. var iRssi = Math.abs(pointCRSSi);
  192. var power = (iRssi-57)/(10*2.0);
  193. var pointCDistance = Math.pow(10, power);
  194. console.log("c"+pointCDistance);
  195. console.log(pointCRSSi);
  196. }
  197. }
  198. }
  199. //获取定位点的x和y
  200. if(!pointADistance==''&&!pointBDistance==''&&!pointCDistance==''){
  201. var pointDX='';var pointDY = '';
  202. var p = Math.pow(pointADistance,2)/10-Math.pow(pointBDistance,2)/10;
  203. pointDX = 2.5 - p;
  204. var m = Math.pow(pointADistance,2)/10-Math.pow(pointCDistance,2)/10;
  205. pointDY = 2.5 - m;
  206. console.log('目标所在位置X是'+pointDX);
  207. console.log('目标所在位置Y是'+pointDY);
  208. if(pointDX > 0 && pointDY > 0){
  209. wx.showToast({title:'欢迎进入25楼craft', duration:4000 });
  210. }
  211. that.setData({
  212. canvasPointX:pointDX,
  213. canvasPointY:pointDY
  214. })
  215. //创建画布
  216. //计算坐标点在规定canvas上的位置显示
  217. var context = wx.createCanvasContext();
  218. context.setStrokeStyle("#00ff00");
  219. //a,b,c,d,e,f a,b起始坐标,c半径,d,e起始和终止角度
  220. context.arc(that.data.canvasPointX*30,that.data.canvasPointY*30,5,0,2*Math.PI);
  221. context.fill();
  222. wx.drawCanvas({
  223. canvasId: 'firstCanvas',
  224. actions: context.getActions() // 获取绘图动作数组
  225. })
  226. wx.showModal({title:'X轴:'+pointDX+'Y轴:'+pointDY, duration:5000 });
  227. //开始网络请求
  228. wx.request({
  229. url: 'http://craftww.cn/weixinIbeacon/index.php',
  230. data: {},
  231. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  232. // header: {}, // 设置请求的 header
  233. success: function(res){
  234. // success
  235. },
  236. fail: function(res) {
  237. // fail
  238. },
  239. complete: function(res) {
  240. // complete
  241. }
  242. })
  243. }else{
  244. wx.showToast({title:'正在搜索...', duration:1000 })
  245. 开始网络请求
  246. wx.request({
  247. url: 'http://craftww.cn/weixinIbeacon/index.php',
  248. data: {},
  249. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  250. // header: {}, // 设置请求的 header
  251. success: function(res){
  252. // success
  253. },
  254. fail: function(res) {
  255. // fail
  256. },
  257. complete: function(res) {
  258. // complete
  259. }
  260. })
  261. }
  262. },
  263. fail: function(res) {
  264. // fail
  265. },
  266. complete: function(res) {
  267. // complete
  268. }
  269. })
  270. } ,
  271. //动态显示
  272. createList: function(thisName){
  273. var that = this
  274. that.setData({
  275. array:[{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"}]
  276. })
  277. },
  278. reStartSearchBLE: function(){
  279. var that = this
  280. //清除本地数据缓存
  281. wx.stopBluetoothDevicesDiscovery({
  282. success: function(res){
  283. // success
  284. },
  285. fail: function(res) {
  286. // fail
  287. },
  288. complete: function(res) {
  289. // complete
  290. }
  291. })
  292. wx.clearStorageSync();
  293. clearInterval(that.data.showPosition);
  294. console.log(that.data.showPosition);
  295. // that.context.clearRect(0,0,canvas.width,canvas.height);
  296. //断开蓝牙
  297. var that = this
  298. wx.closeBluetoothAdapter({
  299. success: function(res){
  300. // success
  301. console.log('重置成功');
  302. //打开蓝牙适配器,如果没有打开 showtoast
  303. wx.openBluetoothAdapter({
  304. success: function(res){
  305. // success
  306. //获取本机的蓝牙适配器状态
  307. wx.getBluetoothAdapterState({
  308. success: function(res){
  309. // success
  310. },
  311. fail: function(res) {
  312. // fail
  313. },
  314. complete: function(res) {
  315. // complete
  316. }
  317. })
  318. },
  319. fail: function(res) {
  320. },
  321. complete: function(res) {
  322. // complete
  323. }
  324. })
  325. },
  326. fail: function(res) {
  327. // fail
  328. },
  329. complete: function(res) {
  330. // complete
  331. }
  332. })
  333. }
  334. //zheli
  335. })

相关文章