deviceConnect2.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. thithats.Search();
  44. }
  45. },
  46. // {"type":" 41","result":true,"data":{"errno":0,"errCode":0,"isDiscovering":true,"errMsg":"startBluetoothDevicesDiscovery:ok"}}
  47. // deviceConnect2.js:50 gadsfasdfqwerqwerqwerqer==={"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
  48. // {"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":{}}]}
  49. funListenDeviceMsgEvent: function (options) {
  50. console.log("gadsfasdfqwerqwerqwerqer===" + JSON.stringify(options));
  51. const that = this;
  52. switch (options.type) {
  53. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
  54. // 搜索到蓝牙
  55. if (options.result) {
  56. if (options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1).length > 0) {
  57. that.setData({
  58. islanya: true,
  59. devicesList: options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1)
  60. });
  61. }
  62. }
  63. break;
  64. case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
  65. // console.log("连接回调:" + JSON.stringify(options))
  66. if (options.result) {
  67. wx.hideLoading();
  68. if (that.data.isOnBluetooth) {
  69. return;
  70. };
  71. that.setData({
  72. isOnBluetooth: true,
  73. });
  74. setTimeout(() => {
  75. that.setData({
  76. isSubmit: false,
  77. islanya: false,
  78. devicesList: [],
  79. isOnBluetooth: false,
  80. });
  81. wx.navigateTo({
  82. url: '../deviceConnect3/deviceConnect3?deviceId=' + options.data.deviceId + '&name=' + options.data.name,
  83. });
  84. }, 500);
  85. } else {
  86. wx.hideLoading()
  87. wx.showModal({
  88. title: '提示',
  89. content: '连接失败',
  90. showCancel: false
  91. });
  92. }
  93. break;
  94. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
  95. if (!options.result) {
  96. console.log("蓝牙未开启 fail =》", options)
  97. wx.showToast({
  98. title: '蓝牙未开启',
  99. icon: 'none'
  100. })
  101. } else {
  102. //蓝牙搜索开始
  103. that.setData({
  104. searching: true
  105. });
  106. }
  107. break;
  108. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
  109. if (options.result) {
  110. //蓝牙停止搜索ok
  111. console.log('蓝牙停止搜索ok')
  112. } else {
  113. //蓝牙停止搜索失败
  114. console.log('蓝牙停止搜索失败')
  115. }
  116. that.setData({
  117. searching: false
  118. });
  119. break;
  120. }
  121. },
  122. Search: function () {
  123. xBlufi.notifyStartDiscoverBle({
  124. 'isStart': true,
  125. });
  126. },
  127. async Connect(e) {
  128. // if(!this.data.islanya) {
  129. // return;
  130. // };
  131. // [{"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":{}}]
  132. var that = this;
  133. const deviceIdData = that.data.devicesList;
  134. if (deviceIdData.length < 1) {
  135. wx.showToast({
  136. title: '请短按设备配对键',
  137. icon: 'none',
  138. duration: 2000
  139. });
  140. return;
  141. };
  142. if (that.data.isSubmit) {
  143. return;
  144. };
  145. that.setData({
  146. isSubmit: true
  147. });
  148. // 停止搜索
  149. xBlufi.notifyStartDiscoverBle({
  150. 'isStart': false,
  151. });
  152. //开始连接
  153. let name = deviceIdData[0].name;
  154. // console.log('点击了,蓝牙准备连接的deviceId:' + deviceIdData[0].deviceId)
  155. xBlufi.notifyConnectBle({
  156. isStart: true,
  157. deviceId: deviceIdData[0].deviceId,
  158. name
  159. });
  160. wx.showLoading({
  161. title: '请稍后',
  162. });
  163. },
  164. onUnload: function () {
  165. var that = that
  166. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  167. },
  168. getBluetoothStatus() {
  169. app.getBluetoothStatus();
  170. },
  171. notRoter() {
  172. wx.navigateBack({
  173. delta: 1
  174. });
  175. },
  176. })