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

如何通过Web Bluetooth获取BLE设备的RSSI信号强度?

Web Bluetooth 获取 BLE 设备 RSSI 支持情况及实现方法

目前Web Bluetooth API已经支持获取BLE设备的RSSI,可分两种场景实现:

1. 扫描发现设备时获取RSSI

调用requestDevice()方法时,可通过监听advertisementreceived事件获取未连接设备的RSSI,需先启动广告包监听。

示例代码:

navigator.bluetooth.requestDevice({
  acceptAllDevices: true,
  optionalServices: ['battery_service'] // 按需指定需要访问的服务
})
.then(device => {
  // 监听广告包接收事件,实时获取RSSI
  device.addEventListener('advertisementreceived', (event) => {
    console.log(`设备 ${event.device.name || '未知设备'} 的RSSI: ${event.rssi}`);
  });
  // 启动持续监听广告包
  return device.watchAdvertisements();
})
.catch(error => {
  console.error('请求蓝牙设备失败:', error);
});

2. 连接设备后获取RSSI

设备连接成功后,可直接读取device.rssi属性获取当前RSSI,也能监听rssichanged事件接收RSSI更新通知。

示例代码:

navigator.bluetooth.requestDevice({ acceptAllDevices: true })
.then(device => {
  return device.gatt.connect();
})
.then(server => {
  // 连接后立即获取当前RSSI
  console.log(`当前RSSI值: ${device.rssi}`);
  // 监听RSSI变化
  device.addEventListener('rssichanged', () => {
    console.log('RSSI已更新:', device.rssi);
  });
})
.catch(error => {
  console.error('蓝牙操作失败:', error);
});

注意事项

  • 浏览器兼容性:Chromium内核浏览器(Chrome、Edge等)已全面支持,Safari最新版本也已适配该特性。
  • 环境要求:必须在HTTPS环境下使用,且需用户手动授权蓝牙设备访问权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 09:01:18