Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read laser distance measure via web-bluetooth

I need to read distance measurements from a Leica Disto D2 in a Web App using Web Bluetooth API. I'm targeting recent Android devices so Chrome/Samsung Browser should support it.

like image 408
Mark Robinson Avatar asked Jan 24 '26 07:01

Mark Robinson


1 Answers

Serve HTML over HTTPS unless testing with localhost. Must start with user gesture:

<button onclick="discoverDevices()">Discover Devices</button>

Connect to our bluetooth device, request notifications when distance changes, print distance in console log.

function discoverDevices() {
  console.log("discoverDevices");
  var options = {
    filters: [{ services: ['3ab10100-f831-4395-b29d-570977d5bf94'] }],
    optionalServices: ['0000180a-0000-1000-8000-00805f9b34fb', '0000180f-0000-1000-8000-00805f9b34fb', '3ab10100-f831-4395-b29d-570977d5bf94'],
    acceptAllDevices: false
  }
  const DISTO_DISTANCE = "3ab10101-f831-4395-b29d-570977d5bf94";
  const DISTO_DISTANCE_UNIT = "3ab10102-f831-4395-b29d-570977d5bf94";
  const DISTO_COMMAND = "3ab10109-f831-4395-b29d-570977d5bf94";
  const STATE_RESPONSE = "3ab1010a-f831-4395-b29d-570977d5bf94";
  const DS_MODEL_NAME = "3ab1010c-f831-4395-b29d-570977d5bf94";
  navigator.bluetooth.requestDevice(options)
    .then(device => {
      console.log('> Name:' + device.name);
      console.log('> Id:' + device.id);
      console.log(device);
      return device.gatt.connect();
    })
    .then(device => {
      device.addEventListener('gattserverdisconnected', onDisconnected);
      return device.gatt.connect();
    })
    .then(server => server.getPrimaryService('3ab10100-f831-4395-b29d-570977d5bf94'))
    .then(service => service.getCharacteristic(DISTO_DISTANCE))
    .then(characteristic => characteristic.startNotifications())
    .then(characteristic => {
      characteristic.addEventListener('characteristicvaluechanged',
                                      handleDistanceChanged);
      console.log('Notifications have been started.');
    })
    .catch(error => {
      console.log('ERROR: ' + error);
    });

  function handleDistanceChanged(event) {
    const value = event.target.value;
    console.log('Got distance: ' + value.getFloat32(0, true));
  }
  function onDisconnected(event) {
    const device = event.target;
    console.log(`Device ${device.name} is disconnected.`);
  }
}

Sources:

  • https://web.dev/bluetooth/
  • https://leica-geosystems.com/
like image 137
Mark Robinson Avatar answered Jan 25 '26 20:01

Mark Robinson