connecting.vue 7.8 KB

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