为Quasar-Framework/Capacitor的Bluetooth-le写入操作设置自定义超时时间
在Quasar/Capacitor中使用Bluetooth-LE插件配置自定义超时的可运行示例
我之前在自己的Quasar + Capacitor项目里折腾过这个插件的超时配置,刚好可以给你一个能直接跑起来的示例,一步步来:
首先得确保你安装的是1.7.0及以上版本的插件,先执行安装/升级命令:
npm install @capacitor-community/bluetooth-le@^1.7.0 # 或者用yarn yarn add @capacitor-community/bluetooth-le@^1.7.0
接下来是一个完整的Quasar单文件组件示例,包含了初始化蓝牙、扫描设备、带超时的连接,以及带自定义超时的读写操作:
<template> <q-page class="row items-center justify-center"> <q-btn label="启动蓝牙并执行操作" @click="runBluetoothFlow" /> <q-card class="mt-5 w-full max-w-md" v-if="logContent"> <q-card-section> <pre class="text-sm">{{ logContent }}</pre> </q-card-section> </q-card> </q-page> </template> <script setup> import { ref } from 'vue' import { BluetoothLE } from '@capacitor-community/bluetooth-le' const logContent = ref('') // 辅助函数:添加操作日志 const appendLog = (text) => { logContent.value += `${new Date().toLocaleTimeString()}: ${text}\n` } const runBluetoothFlow = async () => { try { // 1. 初始化蓝牙,可配置全局扫描超时(也可以在startScan里单独设置) await BluetoothLE.initialize({ requestPermissions: true, scanTimeout: 10000 // 全局默认扫描超时:10秒 }) appendLog('蓝牙初始化完成,权限已申请') // 2. 扫描目标BLE设备,单独配置扫描超时 const scanResult = await BluetoothLE.startScan({ services: ['0000FFE0-0000-1000-8000-00805F9B34FB'], // 替换成你的设备服务UUID scanTimeout: 8000 // 本次扫描自定义超时:8秒 }) if (scanResult.devices.length === 0) { appendLog('扫描超时,未发现目标设备') return } const targetDevice = scanResult.devices[0] appendLog(`扫描到目标设备: ${targetDevice.name || targetDevice.deviceId}`) // 3. 带连接超时的设备连接 await BluetoothLE.connect({ deviceId: targetDevice.deviceId, connectionTimeout: 15000 // 自定义连接超时:15秒 }) appendLog(`成功连接设备: ${targetDevice.deviceId}`) // 4. 发现设备服务与特征(读写前必须执行) const services = await BluetoothLE.discoverServices({ deviceId: targetDevice.deviceId }) const targetService = services.services.find(s => s.uuid === '0000FFE0-0000-1000-8000-00805F9B34FB') const targetChar = targetService.characteristics.find(c => c.uuid === '0000FFE1-0000-1000-8000-00805F9B34FB') // 5. 带自定义超时的特征值读取 try { const readResult = await BluetoothLE.read({ deviceId: targetDevice.deviceId, service: targetService.uuid, characteristic: targetChar.uuid, readTimeout: 5000 // 读取超时:5秒 }) // 转换Base64结果为文本 const plainText = atob(readResult.value) appendLog(`读取到数据: ${plainText}`) } catch (readErr) { readErr.message.includes('timeout') ? appendLog('读取操作超时,请检查设备连接') : appendLog(`读取失败: ${readErr.message}`) } // 6. 带自定义超时的特征值写入 try { // 把普通文本转为Base64格式(BLE写入要求) const writeValue = btoa('Hello BLE Device!') await BluetoothLE.write({ deviceId: targetDevice.deviceId, service: targetService.uuid, characteristic: targetChar.uuid, value: writeValue, writeTimeout: 3000 // 写入超时:3秒 }) appendLog('写入操作成功') } catch (writeErr) { writeErr.message.includes('timeout') ? appendLog('写入操作超时,请检查设备连接') : appendLog(`写入失败: ${writeErr.message}`) } } catch (err) { appendLog(`流程出错: ${err.message}`) } } </script>
关键超时参数说明:
scanTimeout:扫描设备的超时时间,单位毫秒,超时后自动停止扫描connectionTimeout:设备连接的超时时间,超过时长未连接成功则抛出超时错误readTimeout:读取特征值的超时时间writeTimeout:写入特征值的超时时间
注意事项:
- 所有UUID需要替换成你实际使用的BLE设备的服务/特征UUID
- 写入数据必须是Base64编码的字符串,普通文本可以用
btoa()转换 - 记得在
capacitor.config.ts中配置蓝牙权限(iOS和Android的权限声明) - 超时触发时会抛出包含
timeout关键词的错误,可以在catch块里针对性处理
内容的提问来源于stack exchange,提问作者Web CGAir
相关产品推荐
相关产品推荐

