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

向Cellocator设备蓝牙发送参数遇无服务问题求助

问题解决思路

1. 修复「无可用服务」报错

你遇到的No Services found in device错误,核心原因是请求蓝牙设备时未指定目标服务UUID,导致Cellocator设备未暴露其GATT服务。多数蓝牙设备(尤其是厂商自定义设备)不会在无明确服务请求时开放全部服务权限。

修正操作:

  • 先通过蓝牙调试工具(如nRF Connect)或设备手册,获取Cellocator设备的GATT服务UUID(可能是标准UUID或厂商自定义的128位UUID)
  • 修改requestDevice参数,明确指定目标服务:
navigator.bluetooth.requestDevice({
  filters: [{ services: ['你的设备服务UUID'] }], // 精准匹配目标服务
  optionalServices: ['额外需要访问的服务UUID'] // 若有其他服务需调用,在此声明
})
.then(device => device.gatt.connect())
.then(async server => {
  // 用await处理异步操作,确保获取到服务后再执行后续逻辑
  const services = await server.getPrimaryServices('你的设备服务UUID');
  console.log('已获取服务:', services);
})
.catch(error => {
  console.error(`设备连接失败: ${error}`);
});

2. 向设备发送数值的完整流程

成功连接并获取服务后,需找到对应的数据接收特征(Characteristic),再通过特征的写入方法发送数值:

示例代码:

navigator.bluetooth.requestDevice({
  filters: [{ services: ['CELLOCATOR_SERVICE_UUID'] }],
  optionalServices: ['CELLOCATOR_SERVICE_UUID']
})
.then(device => device.gatt.connect())
.then(async server => {
  // 1. 获取目标服务
  const targetService = await server.getPrimaryService('CELLOCATOR_SERVICE_UUID');
  // 2. 获取用于接收数据的特征
  const writeCharacteristic = await targetService.getCharacteristic('CELLOCATOR_WRITE_CHAR_UUID');
  
  // 3. 按设备协议准备数据(此处以发送数值123为例,转成Uint8Array格式)
  const sendData = new Uint8Array([123]);
  // 4. 写入数据:writeValueWithResponse会等待设备响应,writeValueWithoutResponse则无需等待
  await writeCharacteristic.writeValueWithResponse(sendData);
  console.log('数值发送成功');
})
.catch(error => {
  console.error(`操作失败: ${error}`);
});

关键注意事项:

  • 必须确认UUID信息:服务UUID和特征UUID是与设备通信的核心标识,需通过调试工具或设备文档准确获取
  • 匹配设备数据协议:不同设备对数据的编码格式(如字节序、数据类型)有不同要求,需根据设备文档调整ArrayBuffer的构造方式
  • 正确处理异步逻辑:所有GATT操作都是异步的,需用await或.then()确保操作顺序,避免出现未完成操作就执行后续代码的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 18:42:48