connecting.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <script>
  2. import ble_store from "@/store/bluetooth";
  3. import Dialog from "../../../../static/vant/dialog/dialog";
  4. import Toast from "../../../../static/vant/toast/toast";
  5. import {LOG_DEBUG, LOG_WECHAT} from "../../../utils/log";
  6. let $this;
  7. export default {
  8. name: "Connecting",
  9. props: ['status'],
  10. data() {
  11. return {
  12. // 设备状态 0为未连接,1:连接中,2:已连接 3:连接失败
  13. device_status: 0,
  14. // 脑机sn码
  15. code: "AI00000000",
  16. bleFoundTimeOut: undefined
  17. }
  18. },
  19. created() {
  20. $this = this;
  21. // 监听到父级扫码状态
  22. let connect_interval = setInterval(() => {
  23. LOG_DEBUG("created监听到父级扫码状态", $this.status);
  24. if ($this.status*1 === 1) {
  25. $this.device_status = 1;
  26. $this.getBluetoothState();
  27. clearInterval(connect_interval);
  28. }
  29. }, 500);
  30. },
  31. methods:{
  32. /**
  33. * 修改设备连接状态
  34. * @param $status 设备状态 0:为未连接,1:连接中,2:已连接 3:连接失败 5:取消连接
  35. */
  36. setDeviceStatus($status = 0) {
  37. $this.device_status = $status;
  38. $this.$emit("deviceStatus", $status);
  39. },
  40. /**
  41. * 获取蓝牙状态
  42. */
  43. getBluetoothState() {
  44. LOG_WECHAT("开始搜索手机蓝牙");
  45. // 获取本机蓝牙适配器状态
  46. wx.getBluetoothAdapterState({
  47. success: function(res) {
  48. if (res.available) {
  49. // 开始搜寻附近的蓝牙外围设备。此操作比较耗费系统资源,请在搜索到需要的设备后及时调用 wx.stopBluetoothDevicesDiscovery 停止搜索。
  50. wx.startBluetoothDevicesDiscovery({
  51. allowDuplicatesKey: true,
  52. success: function(res) {
  53. $this.onBluetoothFound();
  54. },
  55. fail(err) {
  56. // 失败取消连接
  57. $this.setDeviceStatus(3);
  58. },
  59. });
  60. } else {
  61. // 打印相关信息
  62. LOG_DEBUG(res["errMsg"])
  63. setTimeout(() => {
  64. Toast.fail({
  65. message: res["errMsg"],
  66. });
  67. }, 3000);
  68. }
  69. },
  70. fail: function (err) {
  71. // 打印相关信息
  72. LOG_DEBUG(err["errMsg"])
  73. setTimeout(() => {
  74. Toast.fail({
  75. message: err["errMsg"],
  76. });
  77. }, 3000);
  78. }
  79. })
  80. },
  81. /**
  82. * 监听搜索到新设备的事件
  83. */
  84. onBluetoothFound() {
  85. $this.bleFoundTimeOut = setTimeout(() => {
  86. if ($this.device_status === 0 || $this.device_status === 3) {
  87. // 移除搜索到新设备的事件的全部监听函数
  88. wx.offBluetoothDeviceFound();
  89. // 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
  90. wx.stopBluetoothDevicesDiscovery();
  91. Dialog.confirm({
  92. message: '脑机连接失败',
  93. showCancelButton: true,
  94. cancelButtonText: "查看指引",
  95. }).catch(() => {
  96. mpvue.navigateTo({
  97. url: "/pages/banner/guide/main"
  98. })
  99. });
  100. }
  101. }, 7000)
  102. try {
  103. // 监听搜索到新设备的事件
  104. wx.onBluetoothDeviceFound((res) => {
  105. $this.code = ble_store.getters.getDeviceSn();
  106. res.devices.forEach((device) => {
  107. if (!device.name && !device.localName) { return; }
  108. if (device.localName && device.localName !== "") {
  109. device.name = device.localName;
  110. }
  111. if (device["name"].toUpperCase() === $this.code) {
  112. // 停止搜寻附近的蓝牙外围设备
  113. wx.stopBluetoothDevicesDiscovery();
  114. // 连接低功耗蓝牙设备
  115. $this.bLEConnection(device.deviceId);
  116. //$this.device_data.deviceId = device.deviceId;
  117. clearTimeout($this.bleFoundTimeOut);
  118. LOG_DEBUG("连接低功耗蓝牙设备:", JSON.stringify(device));
  119. }
  120. });
  121. });
  122. } catch (e) {
  123. LOG_DEBUG("打开蓝牙error", e);
  124. $this.setDeviceStatus(3);
  125. }
  126. },
  127. /**
  128. * 连接低功耗蓝牙设备
  129. */
  130. bLEConnection(deviceId) {
  131. $this.code = ble_store.getters.getDeviceSn();
  132. // 记录日志
  133. LOG_WECHAT("开始连接脑机蓝牙:", $this.code);
  134. // 移除搜索到新设备的事件的全部监听函数
  135. wx.offBluetoothDeviceFound();
  136. // 停止搜寻附近的蓝牙外围设备
  137. wx.stopBluetoothDevicesDiscovery();
  138. // 连接蓝牙低功耗设备。(若小程序在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需再次进行搜索操作)
  139. wx.createBLEConnection({
  140. deviceId: deviceId,
  141. success: function() {
  142. // 协商设置蓝牙低功耗的最大传输单元 (Maximum Transmission Unit, MTU)。需在 wx.createBLEConnection 调用成功后调用。仅安卓系统 5.1 以上版本有效,iOS 因系统限制不支持。
  143. wx.setBLEMTU({
  144. deviceId,
  145. mtu: 250,
  146. success(res) {
  147. LOG_DEBUG("设置mtu成功", JSON.stringify(res));
  148. }
  149. })
  150. ble_store.setters.setDeviceId(deviceId);
  151. // 获取蓝牙设备服务
  152. $this.$connection.getBLEDeviceServices(deviceId);
  153. //成功连接脑机蓝牙
  154. $this.setDeviceStatus(2);
  155. // 记录日志
  156. LOG_WECHAT("脑机蓝牙连接成功:", $this.code);
  157. },
  158. fail(err) {
  159. //连接脑机蓝牙失败
  160. $this.setDeviceStatus(3);
  161. LOG_DEBUG(err);
  162. },
  163. });
  164. },
  165. /**
  166. * 取消连接
  167. */
  168. cancelConnect() {
  169. clearTimeout($this.bleFoundTimeOut);
  170. $this.setDeviceStatus(5);
  171. },
  172. },
  173. }
  174. </script>
  175. <template>
  176. <div>
  177. <van-row class="padding">
  178. <van-col span="5" offset="4">
  179. <div class="device_bg flex flex-direction align-center justify-center">
  180. <img src="https://img.shuimuai.com/web/phone.png" alt="" class="device_phone">
  181. <text class="text-gray device_text"> 我的手机</text>
  182. </div>
  183. </van-col>
  184. <van-col span="3" offset="1">
  185. <div class="dot_container flex align-center">
  186. <div class="dot_wait">
  187. <img v-if="status*1 === 1" src="https://img.shuimuai.com/web/dot.png" alt="" class="moving_dot"
  188. :class="{moving: status*1 === 1}"
  189. />
  190. <img v-if="status*1 === 2" src="https://img.shuimuai.com/m_sign_gou%402x.png" alt="" class="moving_dot">
  191. <img v-if="status*1 === 3" src="https://img.shuimuai.com/fail.png" alt="" class="moving_dot">
  192. </div>
  193. </div>
  194. </van-col>
  195. <van-col span="5" offset="0">
  196. <div class="device_bg flex flex-direction align-center justify-center">
  197. <img src="https://img.shuimuai.com/web/brain.png" alt="" class="device_brain">
  198. <text class="text-gray device_text"> 水母智能脑机</text>
  199. </div>
  200. </van-col>
  201. </van-row>
  202. <van-row>
  203. <van-col span="9" offset="1">
  204. <button class="cu-btn bg-red lg text-white" @click="cancelConnect">
  205. <img src="https://img.shuimuai.com/m_duankainaohuan.png" class="cut_brain_icon" alt="">
  206. <text class="padding-lr cut_text">取消连接</text>
  207. </button>
  208. </van-col>
  209. <van-col span="12" offset="1">
  210. <text class="text-gray text-lg">
  211. <template v-if="status*1 === 1">
  212. 连接中...
  213. </template>
  214. <template v-if="status*1 === 2">
  215. 连接成功
  216. </template>
  217. <template v-if="status*1 === 3">
  218. 连接失败
  219. </template>
  220. </text>
  221. </van-col>
  222. </van-row>
  223. <van-toast id="van-toast"/>
  224. <van-dialog id="van-dialog"/>
  225. </div>
  226. </template>
  227. <style scoped></style>