我想在网络应用程序上创建室内定位。我尝试使用 Web 蓝牙 API 来接收 RSSI,但不起作用。
这里是接收蓝牙RSSI的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Bluetooth RSSI Example</title>
</head>
<body>
<button id="connectButton">Connect to ESP32</button>
<p id="rssiValue">RSSI: <span id="rssi"></span> dBm</p>
<script>
const connectButton = document.getElementById('connectButton');
const rssiValue = document.getElementById('rssi');
async function connect() {
try {
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: ['4fafc201-1fb5-459e-8fcc-c5c9c331914a'] }],
});
// Continuously read RSSI
device.addEventListener('advertisementreceived', (event) => {
let rssi = event.rssi;
rssiValue.textContent = `RSSI: ${rssi} dBm`;
});
connectButton.disabled = true; // Disable connect button after successful connection
} catch (error) {
console.error('Error connecting to Bluetooth device:', error);
}
}
connectButton.addEventListener('click', connect);
</script>
</body>
</html>
Web Bluetooth WatchAdvertisement API(因此事件advertisementreceived)目前仅在功能标志
enable-web-bluetooth-new-permissions-backend
后面可用。
另请注意,目前尚无计划推出 WatchAdvertisement API,未来将被其他 API 取代。请参阅 https://g-issues.chromium.org/issues/40488594 了解更多详情。