BluetoothScan.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <van-popup
  3. :show="show"
  4. position="bottom"
  5. :style="{ height: '70%' }"
  6. round
  7. closeable
  8. close-icon-position="top-right"
  9. @update:show="show = $event"
  10. @close="handleClose"
  11. >
  12. <div class="bluetooth-scan">
  13. <div class="scan-header">
  14. <h3>蓝牙设备扫描</h3>
  15. <div class="scan-status">
  16. <van-loading v-if="isScanning" type="spinner" size="16px" />
  17. <span v-if="isScanning" class="scan-text">正在扫描FAYA设备... (已发现 {{ devices.length }} 个)</span>
  18. <span v-else-if="connectedDevice" class="scan-text">已连接设备: {{ connectedDevice.name }}</span>
  19. <span v-else-if="devices.length > 0" class="scan-text">已发现 {{ devices.length }} 个FAYA设备,点击设备连接</span>
  20. <span v-else class="scan-text">点击重新扫描开始搜索FAYA设备</span>
  21. </div>
  22. </div>
  23. <div class="scan-actions">
  24. <van-button
  25. v-if="isScanning"
  26. type="warning"
  27. block
  28. @click="handleStopScan"
  29. >
  30. 停止扫描
  31. </van-button>
  32. <van-button
  33. v-else-if="!connectedDevice"
  34. type="primary"
  35. block
  36. @click="handleStartScan"
  37. >
  38. 重新扫描
  39. </van-button>
  40. </div>
  41. <div class="devices-list">
  42. <div v-if="devices.length === 0 && !isScanning" class="empty-state">
  43. <van-empty description="暂无设备,点击重新扫描" />
  44. </div>
  45. <div v-else class="device-items">
  46. <div
  47. v-for="device in devices"
  48. :key="device.address"
  49. class="device-item"
  50. :class="{ 'device-connected': connectedDevice?.address === device.address }"
  51. @click="handleConnect(device)"
  52. >
  53. <div class="device-info">
  54. <div class="device-name">{{ device.name || '未知设备' }}</div>
  55. <div class="device-address">{{ device.address }}</div>
  56. </div>
  57. <div class="device-action">
  58. <van-tag v-if="connectedDevice?.address === device.address" type="success">
  59. 已连接
  60. </van-tag>
  61. <van-icon v-else name="arrow" />
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </van-popup>
  68. </template>
  69. <script setup>
  70. import { ref, watch } from 'vue'
  71. import { showToast, showNotify, closeToast } from 'vant'
  72. const props = defineProps({
  73. modelValue: {
  74. type: Boolean,
  75. default: false
  76. }
  77. })
  78. const emit = defineEmits(['update:modelValue', 'connected', 'disconnected'])
  79. const show = ref(false)
  80. const isScanning = ref(false)
  81. const devices = ref([])
  82. const connectedDevice = ref(null)
  83. watch(() => props.modelValue, (val) => {
  84. show.value = val
  85. })
  86. watch(show, (val) => {
  87. emit('update:modelValue', val)
  88. // 弹窗打开时,如果未连接设备,自动开始扫描
  89. if (val && !connectedDevice.value && !isScanning.value) {
  90. // 延迟一下确保弹窗已完全打开
  91. setTimeout(() => {
  92. handleStartScan()
  93. }, 100)
  94. } else if (!val) {
  95. // 弹窗关闭时
  96. closeToast() // 清除所有 toast
  97. // 如果正在扫描,停止扫描
  98. if (isScanning.value) {
  99. handleStopScan()
  100. }
  101. // 如果正在连接,清除连接状态
  102. if (connectingDevice.value) {
  103. connectingDevice.value = null
  104. }
  105. }
  106. })
  107. // 开始扫描
  108. const handleStartScan = () => {
  109. if (!window.AndroidScale) {
  110. showNotify({ type: 'danger', message: '未找到 AndroidScale 接口' })
  111. return
  112. }
  113. // 检查缓存和设备状态,如果缓存中没有但组件状态有,清除组件状态
  114. const saved = localStorage.getItem('bluetooth-device')
  115. if (!saved && connectedDevice.value) {
  116. console.log('检测到状态不一致,清除连接状态')
  117. connectedDevice.value = null
  118. connectingDevice.value = null
  119. }
  120. // 如果组件状态显示已连接设备,不允许扫描
  121. if (connectedDevice.value) {
  122. showNotify({ type: 'warning', message: '请先断开当前连接' })
  123. return
  124. }
  125. isScanning.value = true
  126. devices.value = []
  127. try {
  128. window.AndroidScale.startScan()
  129. } catch (error) {
  130. console.error('开始扫描失败:', error)
  131. showNotify({ type: 'danger', message: '扫描失败,请重试' })
  132. isScanning.value = false
  133. }
  134. }
  135. // 停止扫描
  136. const handleStopScan = () => {
  137. if (!window.AndroidScale) {
  138. showNotify({ type: 'danger', message: '未找到 AndroidScale 接口' })
  139. return
  140. }
  141. try {
  142. // 如果支持停止扫描方法,调用它
  143. if (window.AndroidScale.stopScan) {
  144. window.AndroidScale.stopScan()
  145. }
  146. isScanning.value = false
  147. closeToast() // 关闭扫描提示
  148. } catch (error) {
  149. console.error('停止扫描失败:', error)
  150. isScanning.value = false
  151. closeToast()
  152. showNotify({ type: 'danger', message: '停止扫描失败' })
  153. }
  154. }
  155. // 处理扫描结果回调
  156. const handleScanResult = (deviceStr) => {
  157. try {
  158. let deviceData
  159. if (typeof deviceStr === 'string') {
  160. deviceData = JSON.parse(deviceStr)
  161. } else {
  162. deviceData = deviceStr
  163. }
  164. // 只处理名称为"FAYA"的设备
  165. const deviceName = deviceData.name || ''
  166. if (deviceName !== 'FAYA') {
  167. return // 忽略非FAYA设备
  168. }
  169. // 检查设备是否已存在
  170. const exists = devices.value.some(d => d.address === deviceData.address)
  171. if (!exists && deviceData.address) {
  172. devices.value.push({
  173. name: deviceData.name || '未知设备',
  174. address: deviceData.address
  175. })
  176. // 扫描到FAYA设备后自动停止扫描
  177. if (isScanning.value) {
  178. handleStopScan()
  179. }
  180. }
  181. } catch (error) {
  182. console.error('解析扫描结果失败:', error)
  183. }
  184. }
  185. // 当前正在连接的设备
  186. const connectingDevice = ref(null)
  187. // 连接设备
  188. const handleConnect = async (device) => {
  189. // 如果设备已连接,提示已连接
  190. if (connectedDevice.value?.address === device.address) {
  191. showToast({ type: 'success', message: '设备已连接' })
  192. return
  193. }
  194. if (!window.AndroidScale) {
  195. showNotify({ type: 'danger', message: '未找到 AndroidScale 接口' })
  196. return
  197. }
  198. // 如果正在扫描,先停止扫描
  199. if (isScanning.value) {
  200. handleStopScan()
  201. }
  202. try {
  203. connectingDevice.value = device
  204. window.AndroidScale.connect(device.address)
  205. } catch (error) {
  206. closeToast()
  207. connectingDevice.value = null
  208. console.error('连接失败:', error)
  209. showNotify({ type: 'danger', message: '连接失败,请重试' })
  210. }
  211. }
  212. // 处理连接状态变化
  213. const handleConnectionState = (isConnected) => {
  214. closeToast()
  215. console.log(isConnected,'isConnected')
  216. if (isConnected) {
  217. // 连接成功
  218. if (connectingDevice.value) {
  219. connectedDevice.value = connectingDevice.value
  220. // 保存到缓存
  221. localStorage.setItem('bluetooth-device', JSON.stringify(connectedDevice.value))
  222. emit('connected', connectedDevice.value)
  223. connectingDevice.value = null
  224. // 连接成功后自动关闭弹窗(不显示额外 toast)
  225. setTimeout(() => {
  226. closeToast() // 确保关闭所有 toast
  227. show.value = false
  228. }, 300)
  229. }
  230. } else {
  231. // 断开连接
  232. // 清除缓存
  233. localStorage.removeItem('bluetooth-device')
  234. connectedDevice.value = null
  235. connectingDevice.value = null
  236. emit('disconnected')
  237. // 断开连接时也确保关闭 toast
  238. closeToast()
  239. }
  240. }
  241. // 关闭弹窗
  242. const handleClose = () => {
  243. // 清除所有 toast
  244. closeToast()
  245. // 如果正在扫描,先停止扫描
  246. if (isScanning.value) {
  247. handleStopScan()
  248. }
  249. // 如果正在连接,清除连接状态
  250. if (connectingDevice.value) {
  251. connectingDevice.value = null
  252. }
  253. show.value = false
  254. }
  255. // 初始化回调
  256. const initCallbacks = () => {
  257. // 扫描结果回调
  258. window.onScaleScanResult = handleScanResult
  259. // 连接状态回调
  260. window.onScaleConnectionState = handleConnectionState
  261. }
  262. // 加载已保存的设备
  263. const loadSavedDevice = () => {
  264. try {
  265. const saved = localStorage.getItem('bluetooth-device')
  266. if (saved) {
  267. const device = JSON.parse(saved)
  268. connectedDevice.value = device
  269. // 检查设备是否在列表中,如果不在则添加到列表
  270. const exists = devices.value.some(d => d.address === device.address)
  271. if (!exists) {
  272. devices.value.push(device)
  273. }
  274. emit('connected', device)
  275. }
  276. } catch (error) {
  277. console.error('加载保存的设备失败:', error)
  278. }
  279. }
  280. // 断开连接
  281. const disconnect = () => {
  282. // 立即清除连接状态(不等待回调)
  283. const wasConnected = !!connectedDevice.value
  284. connectedDevice.value = null
  285. connectingDevice.value = null
  286. localStorage.removeItem('bluetooth-device')
  287. if (wasConnected) {
  288. emit('disconnected')
  289. }
  290. if (window.AndroidScale) {
  291. try {
  292. window.AndroidScale.disconnect()
  293. console.log('已调用 AndroidScale.disconnect()')
  294. } catch (error) {
  295. console.error('断开连接失败:', error)
  296. showNotify({ type: 'danger', message: '断开连接失败' })
  297. }
  298. }
  299. }
  300. // 清除已连接设备状态(供外部调用)
  301. const clearConnectedDevice = () => {
  302. connectedDevice.value = null
  303. connectingDevice.value = null
  304. // 同时清除缓存
  305. localStorage.removeItem('bluetooth-device')
  306. console.log('已清除连接设备状态')
  307. }
  308. // 暴露方法
  309. defineExpose({
  310. handleConnectionState,
  311. loadSavedDevice,
  312. disconnect,
  313. startScan: handleStartScan,
  314. stopScan: handleStopScan,
  315. clearConnectedDevice
  316. })
  317. // 初始化
  318. initCallbacks()
  319. loadSavedDevice()
  320. </script>
  321. <style scoped lang="sass">
  322. .bluetooth-scan
  323. padding: 20px
  324. height: 100%
  325. display: flex
  326. flex-direction: column
  327. background: #fff
  328. .scan-header
  329. margin-bottom: 20px
  330. text-align: center
  331. h3
  332. margin: 0 0 10px 0
  333. font-size: 18px
  334. font-weight: 600
  335. color: #333
  336. .scan-status
  337. display: flex
  338. align-items: center
  339. justify-content: center
  340. gap: 8px
  341. font-size: 14px
  342. color: #666
  343. .scan-text
  344. color: #999
  345. .scan-actions
  346. margin-bottom: 20px
  347. .devices-list
  348. flex: 1
  349. overflow-y: auto
  350. .empty-state
  351. display: flex
  352. align-items: center
  353. justify-content: center
  354. height: 100%
  355. min-height: 200px
  356. .device-items
  357. display: flex
  358. flex-direction: column
  359. gap: 12px
  360. .device-item
  361. display: flex
  362. align-items: center
  363. justify-content: space-between
  364. padding: 16px
  365. background: #f7f8fa
  366. border-radius: 8px
  367. cursor: pointer
  368. transition: all 0.3s ease
  369. &:active
  370. background: #ebedf0
  371. transform: scale(0.98)
  372. &.device-connected
  373. background: #e8f5e9
  374. border: 1px solid #4caf50
  375. .device-info
  376. flex: 1
  377. .device-name
  378. font-size: 16px
  379. font-weight: 500
  380. color: #333
  381. margin-bottom: 4px
  382. .device-address
  383. font-size: 12px
  384. color: #999
  385. font-family: monospace
  386. .device-action
  387. display: flex
  388. align-items: center
  389. </style>