deviceConnect2.js 5.9 KB

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