deviceConnect2.js 6.0 KB

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