ble-demo.nvue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view class="page">
  3. <view class="status">
  4. <text class="label">状态:</text>
  5. <text class="value">{{ state }}</text>
  6. </view>
  7. <view class="status" v-if="device">
  8. <text class="label">设备:</text>
  9. <text class="value">{{ device.name }} ({{ device.deviceId }})</text>
  10. </view>
  11. <view class="btn-row">
  12. <text class="btn" @click="onScanConnect">扫描并连接</text>
  13. <text class="btn" @click="onDisconnect">断开</text>
  14. </view>
  15. <view class="btn-row">
  16. <text class="btn" @click="onPowerOn">开机</text>
  17. <text class="btn" @click="onPowerOff">关机</text>
  18. <text class="btn" @click="onStart">开始艾灸</text>
  19. <text class="btn" @click="onPause">暂停</text>
  20. </view>
  21. <view class="log-box">
  22. <text class="log-line" v-for="(l, i) in logs" :key="i">{{ l }}</text>
  23. </view>
  24. <ay-toast ref="ayToast" />
  25. </view>
  26. </template>
  27. <script>
  28. import bleManager, {
  29. BLE_STATE, MODE, SUB_MODE, TEMPERATURE,
  30. ensureBlePrerequisite, openLocationSettings
  31. } from '@/utils/ble'
  32. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  33. export default {
  34. components: {
  35. ayToast
  36. },
  37. data() {
  38. return {
  39. state: BLE_STATE.IDLE,
  40. device: null,
  41. logs: []
  42. }
  43. },
  44. onLoad() {
  45. bleManager.configure({ deviceNamePrefix: 'AJY', debug: true })
  46. this._unbinders = [
  47. bleManager.on('state', s => { this.state = s }),
  48. bleManager.on('connected', d => { this.device = d; this.log('已连接 ' + d.deviceId) }),
  49. bleManager.on('disconnected', e => { this.log('已断开 ' + (e.reason || '')) }),
  50. bleManager.on('reconnected', d => { this.log('自动重连成功') }),
  51. bleManager.on('report:GROUP_1', d => { this.log('参数组1 ' + JSON.stringify(d)) }),
  52. bleManager.on('report:GROUP_2', d => { this.log('参数组2 ' + JSON.stringify(d)) })
  53. ]
  54. },
  55. onUnload() {
  56. this._unbinders.forEach(fn => fn && fn())
  57. },
  58. methods: {
  59. log(msg) {
  60. const t = new Date().toTimeString().slice(0, 8)
  61. this.logs.unshift(`[${t}] ${msg}`)
  62. if (this.logs.length > 100) this.logs.pop()
  63. },
  64. async onScanConnect() {
  65. try {
  66. await ensureBlePrerequisite()
  67. await bleManager.init()
  68. const dev = await bleManager.scanAndConnect({ namePrefix: 'BT-', timeout: 8000 })
  69. this.device = dev
  70. } catch (e) {
  71. this.log('失败: ' + e.message)
  72. if (e.message === 'BLE_LOCATION_OFF') {
  73. uni.showModal({
  74. title: '提示', content: '请先开启位置服务', success: r => { if (r.confirm) openLocationSettings() }
  75. })
  76. } else if (e.message === 'BLE_ADAPTER_OFF') {
  77. this.$toastRef.error('请开启蓝牙')
  78. }
  79. }
  80. },
  81. async onDisconnect() {
  82. await bleManager.disconnect()
  83. this.device = null
  84. },
  85. async onPowerOn() { await this._safe(() => bleManager.powerOn()) },
  86. async onPowerOff() { await this._safe(() => bleManager.powerOff()) },
  87. async onStart() {
  88. await this._safe(() => bleManager.startMoxi({
  89. mode: MODE.BASIC, subMode: SUB_MODE.SUB_1,
  90. temperature: TEMPERATURE.MID, duration: 30
  91. }))
  92. },
  93. async onPause() { await this._safe(() => bleManager.pauseMoxi()) },
  94. async _safe(fn) {
  95. try { await fn(); this.log('指令已下发') }
  96. catch (e) { this.log('指令失败: ' + e.message) }
  97. }
  98. }
  99. }
  100. </script>
  101. <style>
  102. .page { flex: 1; padding: 24rpx; background-color: #f5f5f7; }
  103. .status { flex-direction: row; padding: 12rpx 0; }
  104. .label { width: 120rpx; color: #666; font-size: 28rpx; }
  105. .value { flex: 1; color: #222; font-size: 28rpx; }
  106. .btn-row { flex-direction: row; flex-wrap: wrap; margin-top: 12rpx; }
  107. .btn { padding: 16rpx 24rpx; margin: 8rpx; background-color: #4f8ef7; color: #fff; border-radius: 8rpx; font-size: 28rpx; }
  108. .log-box { flex: 1; margin-top: 24rpx; padding: 16rpx; background-color: #1e1e1e; border-radius: 8rpx; }
  109. .log-line { color: #b2f5b2; font-size: 24rpx; line-height: 36rpx; }
  110. </style>