deviceConnect2.js 5.8 KB

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