v148 · Web Serial · Android

Sensor Data Logger

Connect to a serial sensor device (like an Arduino sending temperature and humidity readings) and plot the data in real time. Chrome 148 brings navigator.serial to Android, so this same code now works on mobile.

Checking Web Serial API support…
No port selected
Disconnected Packets: 0
°C
Temperature
%RH
Humidity
hPa
Pressure
0
pkt/s
Throughput

Serial data log

Waiting for connection…

Serial read pattern

// Chrome 148: navigator.serial now works on Android
const port = await navigator.serial.requestPort();
await port.open({ baudRate: 9600 });

const decoder = new TextDecoderStream();
port.readable.pipeTo(decoder.writable);

const reader = decoder.readable.getReader();
let buffer = '';

while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  buffer += value;
  const lines = buffer.split('\n');
  buffer = lines.pop(); // incomplete last line
  for (const line of lines) {
    const [temp, hum, pres] = line.split(',').map(Number);
    updateDisplay(temp, hum, pres);
  }
}

references

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗