deviceConnect2.js 7.2 KB

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