device.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <div id="device_container" class="padding-lr">
  3. <div class="device">
  4. <div class="head flex justify-between">
  5. <div>
  6. <div class="line"></div>
  7. <div class="title">我的设备</div>
  8. </div>
  9. <!-- <div class="my_msg" @click="get_toy_list">-->
  10. <!-- <text class="cuIcon-mark"></text>-->
  11. <!-- <text class="under_line">获取设备列表</text>-->
  12. <!-- </div>-->
  13. </div>
  14. <div v-if="connect_toy==0">
  15. <!-- 未连接部分-->
  16. <div class="connect_box" v-if="status == 0">
  17. <device_unconnect @open_scan="open_scan"></device_unconnect>
  18. </div>
  19. <!-- 连接中-->
  20. <div class="connecting_box" v-if="status != 0 && connect_show == false">
  21. <device_connecting :status="status"></device_connecting>
  22. </div>
  23. <!-- 已链接 -->
  24. <div class="connected_box" v-if="connect_show">
  25. <device_connected @open_choose_toy="open_choose_toy"
  26. @change_brain_status="change_device_status"></device_connected>
  27. </div>
  28. </div>
  29. <div v-else>
  30. <!-- 玩具模块-->
  31. <!-- 玩具连接中-->
  32. <div class="connecting_toy">
  33. <toy_connecting :connect_toy="connect_toy"
  34. :deviceId="_deviceId"
  35. @open_choose_toy="open_choose_toy"
  36. @change_toy_connect_status="change_toy_connect_status"
  37. @change_status="change_device_status"
  38. @game_start="game_start"
  39. ></toy_connecting>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- 选择玩具-->
  44. <van-popup :show="choose_toy_window.show" @close="on_close"
  45. position="bottom"
  46. round
  47. closeable
  48. safe-area-inset-bottom
  49. >
  50. <div class="head padding">
  51. <div>
  52. <div class="line"></div>
  53. <div class="title">选择玩具</div>
  54. </div>
  55. </div>
  56. <div class="padding toy_list">
  57. <van-row gutter="8">
  58. <van-col span="8" v-for="(toy,index) in toy_list" :key="index">
  59. <div class="toy_item flex flex-direction justify-center align-center"
  60. @click="choose_toy(index)"
  61. :class="toy_action == toy.id?'toy_item_action_bg':'toy_item_normal_bg'">
  62. <img :src="toy.img" alt="" class="toy_img">
  63. <text class="toy_text padding-top">{{ toy.name }}</text>
  64. </div>
  65. </van-col>
  66. </van-row>
  67. </div>
  68. <div class="toy_actions padding text-center">
  69. <view class="text-gray toy_action_text padding">选择你最感兴趣的项目,点击“选好了”以后将会自动连接</view>
  70. <button class="cu-btn lg cu-btn-primary text-white text-center padding" @click="choose_ok">选好了</button>
  71. </div>
  72. </van-popup>
  73. <van-toast id="van-toast"/>
  74. </div>
  75. </template>
  76. <script>
  77. //蓝牙未连接
  78. import device_unconnect from "@/components/device/unconnect";
  79. //蓝牙连接中
  80. import device_connecting from "@/components/device/connecting";
  81. //蓝牙完成链接
  82. import device_connected from '@/components/device/connected';
  83. //连接玩具
  84. import toy_connecting from '@/components/device/toy/connecting'
  85. //获取个人信息
  86. import Toast from '../../../static/vant/toast/toast';
  87. import {game_devices} from "../../requests/game";
  88. import game_store from '@/store/game'
  89. import bluetooth from "@/utils/bluetooth";
  90. let $this;
  91. export default {
  92. name: "device",
  93. components: {
  94. device_unconnect, device_connecting, device_connected, toy_connecting
  95. },
  96. data() {
  97. return {
  98. //设备状态 0为未连接,1:连接中,2:已连接 3:连接失败
  99. status: 0,
  100. choose_toy_window: {
  101. show: false
  102. },
  103. //'水柱音箱', '喷雾恐龙(大)', '喷雾恐龙(小)', '轨道车', '碰碰车', '小车(大)', '小车(中)', '小车(小)', '飞行器(大)', '飞行器(小)', '水母灯'
  104. toy_list: [
  105. {
  106. id: 1,
  107. name: "水柱音箱",
  108. img: "https://img.shuimuai.com/web/toy_car.png"
  109. },
  110. {
  111. id: 2,
  112. name: "喷雾恐龙(大)",
  113. img: "https://img.shuimuai.com/web/toy_dinasour.png"
  114. },
  115. {
  116. id: 3,
  117. name: "喷雾恐龙(小)",
  118. img: "https://img.shuimuai.com/web/toy_dinasour.png"
  119. },
  120. {
  121. id: 4,
  122. name: "轨道车",
  123. img: "https://img.shuimuai.com/web/toy_car_roat.png"
  124. },
  125. {
  126. id: 5,
  127. name: "碰碰车",
  128. img: "https://img.shuimuai.com/web/toy_car.png"
  129. },
  130. {
  131. id: 6,
  132. name: "小车(大)",
  133. img: "https://img.shuimuai.com/web/toy_car.png"
  134. },
  135. {
  136. id: 7,
  137. name: "小车(中)",
  138. img: "https://img.shuimuai.com/web/toy_car.png"
  139. },
  140. {
  141. id: 8,
  142. name: "小车(小)",
  143. img: "https://img.shuimuai.com/web/toy_car.png"
  144. },
  145. {
  146. id: 9,
  147. name: "飞行器(大)",
  148. img: "https://img.shuimuai.com/web/toy_car.png"
  149. },
  150. {
  151. id: 10,
  152. name: "飞行器(小)",
  153. img: "https://img.shuimuai.com/web/toy_car.png"
  154. },
  155. {
  156. id: 11,
  157. name: "水母灯",
  158. img: "https://img.shuimuai.com/web/toy_car.png"
  159. },
  160. ],
  161. toy_action: 1,
  162. connect_show: false,
  163. //连接玩具 0:未连接 1:连接中 2:已连接 3:连接失败 4:游戏中
  164. connect_toy: 0,
  165. code: "jellyfish1234",
  166. device: {},
  167. canWrite: false,
  168. _deviceId: '',
  169. _serviceId: "",
  170. _characteristicId: "",
  171. chs: [],
  172. _device_index: false
  173. }
  174. },
  175. watch: {
  176. status($new, $old) {
  177. //成功连接的方法
  178. if ($new == 2) {
  179. $this.connect_show = true
  180. } else if ($new == 0) {
  181. $this.connect_show = false
  182. }
  183. }
  184. },
  185. methods: {
  186. //打开 扫描二维码
  187. open_scan() {
  188. wx.openBluetoothAdapter({
  189. success(res) {
  190. //判断已经打开连接了
  191. if (res['errMsg'] == "openBluetoothAdapter:ok") {
  192. console.log('openBluetoothAdapter success', res)
  193. $this.startBluetoothDevicesDiscovery()
  194. }
  195. },
  196. fail(err) {
  197. if (err['errCode'] == 10001) {
  198. Toast.fail({
  199. message: "请连接蓝牙"
  200. })
  201. } else {
  202. Toast.fail({
  203. message: "蓝牙连接失败"
  204. })
  205. }
  206. }
  207. })
  208. //
  209. // wx.scanCode({
  210. // onlyFromCamera: true,
  211. // success: (res) => {
  212. // let $data = res
  213. // if ($data.result) {
  214. // let url = decodeURIComponent($data.result)
  215. // let $code = url.match(/\?code=(.*)/)[1];
  216. // console.log($code)
  217. // }
  218. // }
  219. // })
  220. },
  221. //关闭窗口的方法
  222. on_close() {
  223. $this.choose_toy_window.show = false
  224. },
  225. //选择玩具
  226. choose_toy($index) {
  227. $this.toy_action = $this.toy_list[$index].id
  228. },
  229. // 打开选择玩具窗口
  230. open_choose_toy() {
  231. $this.choose_toy_window.show = true
  232. },
  233. // 选好玩具
  234. choose_ok() {
  235. $this.on_close()
  236. $this.change_toy_connect_status(1)
  237. $this._device_index = $this.toy_action - 1
  238. $this.connect_toy = 2
  239. },
  240. //修改玩具连接状态
  241. change_toy_connect_status($status = 0) {
  242. $this.connect_toy = $status
  243. },
  244. // 修改设备连接状态
  245. change_device_status($status = 0) {
  246. $this.status = $status
  247. //当蓝牙连接已断开
  248. if ($status == 0) {
  249. $this.connect_toy = $status
  250. $this.connect_show = false
  251. wx.closeBLEConnection({
  252. deviceId: $this._deviceId,
  253. success(res) {
  254. Toast.success('已成功断开')
  255. }
  256. })
  257. }
  258. },
  259. //开始蓝牙被发现
  260. startBluetoothDevicesDiscovery() {
  261. wx.startBluetoothDevicesDiscovery({
  262. allowDuplicatesKey: true,
  263. success: (res) => {
  264. console.log('startBluetoothDevicesDiscovery success', res)
  265. $this.onBluetoothDeviceFound()
  266. $this.change_device_status(1)
  267. },
  268. fail(err) {
  269. $this.change_device_status(3)
  270. }
  271. })
  272. },
  273. //打开蓝牙搜索
  274. onBluetoothDeviceFound() {
  275. wx.onBluetoothDeviceFound((res) => {
  276. res.devices.forEach(device => {
  277. if (!device.name && !device.localName) {
  278. return
  279. }
  280. console.log(device['name'])
  281. if (device['name'] == $this.code) {
  282. $this.stopBluetoothDevicesDiscovery()
  283. $this.device = device
  284. game_store.commit('setDeviceId', device.deviceId)
  285. $this._deviceId = device.deviceId
  286. $this.createBLEConnection()
  287. }
  288. })
  289. })
  290. setTimeout(() => {
  291. if (!$this.device.deviceId) {
  292. $this.stopBluetoothDevicesDiscovery()
  293. $this.status = 0
  294. $this.connect_show = false
  295. Toast.fail('未连接到设备')
  296. }
  297. }, 7000)
  298. },
  299. // 停止蓝牙搜索
  300. stopBluetoothDevicesDiscovery() {
  301. wx.stopBluetoothDevicesDiscovery()
  302. },
  303. //连接低功耗蓝牙设备。
  304. createBLEConnection() {
  305. wx.createBLEConnection({
  306. deviceId: $this.device.deviceId,
  307. success: (res) => {
  308. console.log('成功连接')
  309. //成功连接脑环蓝牙
  310. $this.change_device_status(2)
  311. // $this.getBLEDeviceServices($this.device.deviceId)
  312. },
  313. fail(err) {
  314. console.log('err', err)
  315. }
  316. })
  317. },
  318. // 获取游戏设备玩具
  319. get_toy_list() {
  320. game_devices().then((res) => {
  321. let $data = res.data
  322. })
  323. },
  324. // 开始游戏的方法
  325. game_start() {
  326. game_store.commit('setToyIndex', $this.toy_action - 1)
  327. // bluetooth.sendConnect($this.toy_action - 1, $this._deviceId, $this._serviceId, $this._characteristicId)
  328. console.log('device.vue', $this._deviceId)
  329. }
  330. },
  331. mounted() {
  332. $this.get_toy_list()
  333. },
  334. created() {
  335. $this = this
  336. },
  337. onLoad(options) {
  338. // 原有的code
  339. let $_code = wx.getStorageSync('code')
  340. if (options.q) {
  341. let url = decodeURIComponent(options.q)
  342. let $code = url.match(/\?code=(.*)/)[1];
  343. //判断新的code 和 旧的code 是否一致 不一致则重新登录
  344. console.log('1---' + $_code, '2---' + $code)
  345. if ($_code && $_code != $code) {
  346. Toast.fail('该用户已绑定邀请码')
  347. }
  348. }
  349. }
  350. }
  351. </script>
  352. <style scoped>
  353. @import "../static/device.css";
  354. </style>