You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

蓝牙无法发送消息排查:JS向Python设备传输数据受阻

问题分析与修复

你的代码存在多处关键问题,同时有几个潜在卡点会导致程序无响应且无日志输出,具体排查和修复如下:

1. 核心代码错误

服务与特征值获取逻辑错误

  • server.getPrimaryServices() 无参数调用返回的是蓝牙服务数组(BluetoothRemoteGATTService[]),你直接将数组赋值给service,后续调用service.getCharacteristic()会因为数组不存在该方法抛出错误
  • getCharacteristic() 必须传入目标特征值的UUID,否则无法定位到可用于写入数据的特征值
  • 你指定的optionalServices仅包含0x2A00(设备名称服务),该服务通常为只读,不具备数据写入能力,需添加Python设备实际暴露的可写服务UUID

潜在的未定义变量

代码中使用的message变量未在片段中定义,若实际代码中也未定义,会导致编码步骤报错

2. 无响应卡点排查

程序无响应且无日志,大概率是卡在某个await步骤:

  • 设备选择弹窗未处理:navigator.bluetooth.requestDevice()会弹出设备选择框,若用户未选择设备,该Promise会一直处于pending状态,后续代码不执行也不会进入catch
  • GATT连接超时:device.gatt.connect()若遇到设备无响应,会无限等待,需添加超时处理

修复后的代码

const sendStringToDevice = async () => {
  try {
    // 替换为你的Python设备实际的服务UUID和可写特征值UUID
    const TARGET_SERVICE_UUID = '0000ffe0-0000-1000-8000-00805f9b34fb';
    const TARGET_CHARACTERISTIC_UUID = '0000ffe1-0000-1000-8000-00805f9b34fb';
    // 确保message变量已定义
    const message = 'Hello from JS';

    // 请求蓝牙设备,添加所需服务到optionalServices
    const device = await navigator.bluetooth.requestDevice({
      filters: [{ name: 'monocle' }],
      optionalServices: [0x2A00, TARGET_SERVICE_UUID],
    });

    // GATT连接添加超时处理,防止无限等待
    const server = await Promise.race([
      device.gatt.connect(),
      new Promise((_, reject) => setTimeout(() => reject(new Error('GATT连接超时')), 10000))
    ]);

    // 获取指定服务(传入UUID)
    const service = await server.getPrimaryService(TARGET_SERVICE_UUID);
    console.log("service got");

    // 获取指定特征值(传入UUID)
    const characteristic = await service.getCharacteristic(TARGET_CHARACTERISTIC_UUID);

    // 转换字符串为字节数组
    const encoder = new TextEncoder('utf-8');
    const data = encoder.encode(message);

    // 写入数据,部分设备需指定writeType
    await characteristic.writeValue(data, { writeType: 'withoutResponse' });

    console.log(`String "${message}" sent successfully to monocle`);
  } catch (error) {
    console.error('Error sending string to Bluetooth device:', error);
  }
};

额外排查步骤

  • 确认Python设备的蓝牙服务和特征值UUID正确,且特征值具备写权限
  • 在Chrome开发者工具的「Bluetooth」面板中,检查设备的服务和特征值是否能被正常识别
  • 确保浏览器处于HTTPS环境(localhost除外),蓝牙API仅在安全上下文可用
  • 检查浏览器蓝牙权限是否已授予

内容的提问来源于stack exchange,提问作者ProfessorSocks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 13:45:01