deviceConnect2.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. const app = getApp();
  2. let xBlufi = require("../../utils/blufi/xBlufi.js");
  3. import strings from '../../utils/strings.js';
  4. import lexin_devices from '../../utils/lexin/devices';
  5. import junzheng_util from '../../utils/junzheng/util';
  6. import route_util from '../../utils/route_util.js';
  7. Page({
  8. data: {
  9. nvabarData: {
  10. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  11. title: '连接配网', //导航栏 中间的标题
  12. },
  13. devicesList: [],
  14. searching: false,
  15. version: '2.0',
  16. name: '',
  17. connectedDeviceId: '',
  18. connected: true,
  19. deviceInfo: null,
  20. isInitOK: false,
  21. customData: '',
  22. isOne: true,
  23. isSubmit: false,
  24. islanya: false,
  25. isOnBluetooth: false,
  26. imageUrl: "./../../img/f.png",
  27. connectDevice: {},
  28. },
  29. onLoad(options) {
  30. const that = this;
  31. var param = options.param;
  32. if (!strings.isEmpty(param)) {
  33. param = JSON.parse(options.param);
  34. that.data.connectDevice = param;
  35. if (!strings.isEmpty(param.typeList)) {
  36. var typeList = param.typeList;
  37. typeList = typeList.filter((v) => v.connectType == 3);
  38. if (!strings.isEmpty(typeList)) {
  39. that.setData({
  40. imageUrl: typeList[0].deviceLinkResp.icon2,
  41. });
  42. }
  43. }
  44. }
  45. const isOne = setTimeout(() => {
  46. clearTimeout(isOne);
  47. if (that.data.isOne) {
  48. that.setData({
  49. isOne: false,
  50. });
  51. }
  52. }, 2000);
  53. if (app.globalData.oneInitBluetooth) {
  54. app.globalData.oneInitBluetooth = false;
  55. xBlufi.initXBlufi(1);
  56. };
  57. xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
  58. that.Search();
  59. },
  60. onShow() {
  61. var that = this;
  62. if (!that.data.isOne) {
  63. that.Search();
  64. }
  65. },
  66. ///开始或结束,也可以过滤名称
  67. Search: function () {
  68. xBlufi.notifyStartDiscoverBle({
  69. 'isStart': true,
  70. });
  71. },
  72. // {"type":" 41","result":true,"data":{"errno":0,"errCode":0,"isDiscovering":true,"errMsg":"startBluetoothDevicesDiscovery:ok"}}
  73. // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
  74. // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}},{"deviceId":"42:E0:22:C2:6E:25","name":"","RSSI":-92,"connectable":true,"advertisData":"4c001007221fa8d7a84ba8","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
  75. funListenDeviceMsgEvent: function (options) {
  76. var that = this;
  77. switch (options.type) {
  78. //发现设备列表回调开始
  79. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
  80. if (!options.result) {
  81. console.log("蓝牙未开启 fail =》", options)
  82. wx.showToast({
  83. title: '蓝牙未开启',
  84. icon: 'none'
  85. })
  86. } else {
  87. //蓝牙搜索开始
  88. that.setData({
  89. searching: true
  90. });
  91. }
  92. break;
  93. // [{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0","RSSI":-62,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fd3af0","serviceData":{}}]
  94. ///发现设备列表回调
  95. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
  96. // 搜索到蓝牙
  97. if (options.result) {
  98. var devicesList = options.data.filter((v) => {
  99. var name = v.name;
  100. var isLexin = name.indexOf(lexin_devices.leXin) !== -1;
  101. var isJungzheng1 = junzheng_util.isJungzheng1(name);
  102. var isJungzheng2 = junzheng_util.isJungzheng2(name);
  103. if (isJungzheng1 || isJungzheng2) {
  104. console.log("gasdfqwerqwerqrwqr===00==" + v.name);
  105. } else if (isLexin) {
  106. console.log("gasdfqwerqwerqrwqr===11==" + v.name);
  107. }
  108. // return isLexin;
  109. return isLexin || isJungzheng1 || isJungzheng2;
  110. ///2AXD
  111. // return v.name == "AIrSMArT_d0311004155e";
  112. ///X4
  113. // return v.name == "AIrSMArT_861210052356337";
  114. });
  115. if (!strings.isEmpty(devicesList)) {
  116. ///过滤乐鑫设备
  117. that.setData({
  118. islanya: true,
  119. devicesList: devicesList,
  120. });
  121. }
  122. }
  123. break;
  124. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0"}}
  125. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24"}}
  126. ///主动去连接连接
  127. case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
  128. wx.hideLoading();
  129. if (options.result) {
  130. if (that.data.isOnBluetooth) {
  131. return;
  132. };
  133. that.setData({
  134. isOnBluetooth: true,
  135. });
  136. setTimeout(() => {
  137. that.data.isSubmit = false;
  138. that.setData({
  139. islanya: false,
  140. devicesList: [],
  141. isOnBluetooth: false,
  142. });
  143. var param = JSON.stringify(that.data.connectDevice);
  144. wx.navigateTo({
  145. url: '../deviceConnect3/deviceConnect3?deviceId=' + options.data.deviceId + '&name=' + options.data.name + "&param=" + param,
  146. });
  147. }, 200);
  148. } else {
  149. that.data.isSubmit = false;
  150. wx.showModal({
  151. title: '提示',
  152. content: '连接失败',
  153. showCancel: false
  154. });
  155. }
  156. break;
  157. ///停止发现设备列表回调
  158. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
  159. if (options.result) {
  160. //蓝牙停止搜索ok
  161. console.log('蓝牙停止搜索ok')
  162. } else {
  163. //蓝牙停止搜索失败
  164. console.log('蓝牙停止搜索失败')
  165. }
  166. that.setData({
  167. searching: false
  168. });
  169. break;
  170. }
  171. },
  172. // [{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24","RSSI":-51,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fcbb24","serviceData":{}}]
  173. async Connect(e) {
  174. var that = this;
  175. const devicesList = that.data.devicesList;
  176. if (devicesList.length < 1) {
  177. // var url = "../deviceConnect4/deviceConnect4";
  178. // var p = JSON.stringify(that.data.connectDevice);
  179. // var param = "?deviceId=" + deviceId + "&param=" + p;
  180. // route_util.jumpParams(url, param);
  181. wx.showToast({
  182. title: '请短按设备配对键',
  183. icon: 'none',
  184. duration: 2000
  185. });
  186. return;
  187. };
  188. if (that.data.isSubmit) {
  189. return;
  190. };
  191. that.data.isSubmit = true;
  192. // 停止搜索
  193. xBlufi.notifyStartDiscoverBle({
  194. 'isStart': false,
  195. });
  196. var name = devicesList[0].name;
  197. var deviceId = devicesList[0].deviceId;
  198. var isJungzheng1 = junzheng_util.isJungzheng1(name);
  199. var isJungzheng2 = junzheng_util.isJungzheng2(name);
  200. if (isJungzheng1 || isJungzheng2) {
  201. var url = "../deviceConnect4/deviceConnect4";
  202. var param = "?deviceId=" + deviceId + '&name=' + name;
  203. route_util.jumpParams(url, param);
  204. return;
  205. }
  206. //开始连接
  207. var name = devicesList[0].name;
  208. // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId)
  209. xBlufi.notifyConnectBle({
  210. isStart: true,
  211. name: name,
  212. deviceId: deviceId,
  213. });
  214. wx.showLoading({
  215. title: '请稍后',
  216. });
  217. },
  218. getBluetoothStatus() {
  219. app.getBluetoothStatus();
  220. },
  221. notRoter() {
  222. wx.navigateBack({
  223. delta: 1
  224. });
  225. },
  226. onUnload: function () {
  227. var that = this;
  228. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  229. },
  230. })