我需要使用Web Bluetooth API在Web应用程序中读取Leica Disto D2的距离测量值。我的目标是最近的Android设备,所以Chrome/三星浏览器should support it.
093gszye1#
通过HTTPS提供HTML,除非使用localhost进行测试。必须以用户手势开始:
<button onclick="discoverDevices()">Discover Devices</button>
字符串连接到我们的蓝牙设备,当距离变化时请求通知,在控制台日志中打印距离。
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.`); } }
型来源:
azpvetkf2#
我改编了罗宾逊的答案中的代码,修正了一些东西,并增加了对页面刷新时自动连接的支持现场演示https://murkle.github.io/utils/webbluetooth/leica_distoD2_laser_measurer.html
// info from https://stackoverflow.com/questions/69629692/how-to-read-laser-distance-measure-via-web-bluetooth const DISTO_SERVICEID = '3ab10100-f831-4395-b29d-570977d5bf94'; 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"; const BATTERY_SERVICE = '0000180f-0000-1000-8000-00805f9b34fb'; const DEVICE_INFORMATION = '0000180a-0000-1000-8000-00805f9b34fb'; const namePrefix = "DISTO "; let service; let device; const logElement = document.getElementById('logging'); ; function log(message) { const logEntry = document.createElement('div'); logEntry.classList.add('log-entry'); logEntry.innerText = message; logElement.appendChild(logEntry); } function handleDisconnect() { log('Connection lost. Device disconnected.'); document.getElementById('connectButton').disabled = false; document.getElementById('disconnectButton').disabled = true; alert('Connection lost. Device disconnected.'); } function disconnectFromDevice() { if (device && device.gatt.connected) { device.gatt.disconnect(); } } function forgetDevice() { if (device) { device.forget(); } } async function discoverDevices() { log("discoverDevices"); let filters = []; // filter on EITHER namePrefix OR services filters.push({ namePrefix: namePrefix }); //filters.push({ // services: [DISTO_SERVICEID] //}); let options = { optionalServices: [DEVICE_INFORMATION, BATTERY_SERVICE, DISTO_SERVICEID], acceptAllDevices: false } options.filters = filters; device = await navigator.bluetooth.requestDevice(options); log('> Name:' + device.name); log('> Id:' + device.id); log(device); await connectToDevice(device); log('Notifications have been started.'); } async function connectToDevice(device) { device.addEventListener('gattserverdisconnected', onDisconnected); log('Connecting to GATT Server...'); const server = await device.gatt.connect(); log('Getting Service...'); const service = await server.getPrimaryService(DISTO_SERVICEID); log('Getting Distance Characteristic...'); const characteristic = await service.getCharacteristic(DISTO_DISTANCE); characteristic.addEventListener('characteristicvaluechanged', handleDistanceChanged); log('Enabling notifications...'); await characteristic.startNotifications(); log('Connected to ' + device.name); } function handleDistanceChanged(event) { const value = event.target.value; log('Got distance: ' + value.getFloat32(0, true)); document.getElementById("result").innerHTML = "Distance = " + value.getFloat32(0, true).toFixed(4) + " m"; } function onDisconnected(event) { const device = event.target; log(`Device ${device.name} is disconnected.`); } function log(message) { const logEntry = document.createElement('div'); logEntry.classList.add('log-entry'); logEntry.innerText = message; logElement.appendChild(logEntry); } // https://docs.google.com/document/d/1RF4D-60cQJWR1LoQeLBxxigrxJwYS8nLOE0qWmBF1eo/edit // try to connect to existing device async function getPermittedBluetoothDevices() { let devices = await navigator.bluetooth.getDevices(); for (let device0 of devices) { // Start a scan for each device before connecting to check that they're in // range. let abortController = new AbortController(); await device0.watchAdvertisements({ signal: abortController.signal }); device0.addEventListener('advertisementreceived', async(evt) => { // Stop the scan to conserve power on mobile devices. abortController.abort(); // Advertisement data can be read from |evt|. let deviceName = evt.name; let uuids = evt.uuids; let appearance = evt.appearance; let pathloss = evt.txPower - evt.rssi; let manufacturerData = evt.manufacturerData; let serviceData = evt.serviceData; if (evt.device.name.startsWith(namePrefix)) { log("Found previously connected device " + device0.name) // At this point, we know that the device is in range, and we can attempt // to connect to it. device = evt.device; await connectToDevice(device); } else { log("Ignoring device " + device0.name + " as it doesn't start with " + namePrefix); } }); } } getPermittedBluetoothDevices();
字符串
2条答案
按热度按时间093gszye1#
通过HTTPS提供HTML,除非使用localhost进行测试。必须以用户手势开始:
字符串
连接到我们的蓝牙设备,当距离变化时请求通知,在控制台日志中打印距离。
型
来源:
azpvetkf2#
我改编了罗宾逊的答案中的代码,修正了一些东西,并增加了对页面刷新时自动连接的支持
现场演示https://murkle.github.io/utils/webbluetooth/leica_distoD2_laser_measurer.html
字符串