Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
BLE mantener viva la conexión (sitio web/pwa) usando javascript

Descargo de responsabilidad: soy un programador novato.

Estoy tratando de mantener viva la conexión hasta que finalice la sesión (el usuario cierra el navegador/navega a otra cosa)

el código aquí muestra la muestra modificada por Chrome Samples

 function readData() { console.log('Requesting Bluetooth Device...'); navigator.bluetooth.requestDevice({ filters: [{ services: [serviceUuid] }] }) .then(device => { return device.gatt.connect(); }) .then(server => { return server.getPrimaryService(serviceUuid); }) .then(service => { return service.getCharacteristic(characterUuid); }) .then(characteristic => characteristic.readValue()) .then(result => decodeValues(result)) .then(result => document.getElementById("output").innerHTML = result) .catch(error => { console.log('Argh! ' + error); }); }

pero el problema aquí es que si quiero escribir en el mismo servicio de una característica diferente cuando el usuario hace clic en otro botón, tengo que usar navigator.bluetooth.requestDevice(...) nuevamente, lo que abrirá el cuadro de diálogo emergente para elegir el dispositivo. Esto se volverá muy poco intuitivo al usar el sitio. Por favor ayuda.

Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Entonces, funcionó usando async await .....

explicación para el código a continuación ... Voy a llamar a readData primero antes de writeData .... por lo tanto, almacenar el servicio en una variable global

 let device, server, service; let serviceUuid = '4fafc201-1fb5-459e-8fcc-c5c9c331914b'; let writeUuid = 'beb5483e-36e1-4688-b7f5-ea07361b26a8'; let readUuid = '9fde8759-ffd6-40d7-a50e-f0ffa74abd25'; async function readData() { device = await navigator.bluetooth.requestDevice({ filters: [{ services: [serviceUuid] }] }) .catch(error => console.log(error)); server = await device.gatt.connect().catch(error => console.log(error)); service = await server.getPrimaryService(serviceUuid).catch(error => console.log(error)); let characteristic = await service.getCharacteristics(readUuid).catch(error => console.log(error)); let value = decodeValues(await characteristic[0].readValue().catch(error => console.log(error))); document.getElementById('time-left').innerHTML = value; } async function writeData() { let characteristic = await service.getCharacteristics(writeUuid).catch(error => console.log(error)); characteristic[0].writeValue(encodeValues('5')); } function decodeValues(result) { let decoder = new TextDecoder('utf-8'); return decoder.decode(result.buffer); } function encodeValues(stringValue) { let encoder = new TextEncoder('utf-8'); return encoder.encode(stringValue); }

espero que esto ayude a cualquiera que busque la respuesta

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!