deviceConnect2.js 6.9 KB

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