I want to create an indoor positioning on a web application. I tried using the Web Bluetooth API to receive RSSI, but it doesn’t work.
Here is the code for receive bluetooth RSSI:
<code> <!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>
</code>
<code> <!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>
</code>
<!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>
New contributor
Setthasak Boonprasert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.