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

150
Views
why is Bluetooth.then not a function?

I am writing a Javascript program to print out device information.

I have two services: 'battery_service' and 'device_information', and the characteristic under 'battery_service' is 'battery_information'.

I want to implement some code in separate functions to make it neater, but when I want to do so, I get the following error:

deviceInfo3.js:11 Uncaught TypeError: bluetoothDevice is not a function

from calling the function below: My getBatteryFunction:

 function getBatteryPercent()
  {
    bluetoothDevice
    .then(
      value => {
        console.console.log("Battery = "+value.getUint8(0))
        document.getElementById("battery").value=value.getUint8(0);
      }
    )
  }

and this is my main driver function when the button is clicked:

var bluetoothDevice;

function onScanButtonClick() {
  let options = {filters: []};

  
    options.filters.push({services: ['battery_service', 'device_information' ]});

  console.log('Requesting Bluetooth Device...');
  navigator.bluetooth.requestDevice(options)
  .then(device => {
    bluetoothDevice = device;
    bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
    return device.gatt.connect();
  })
  .then((server) => {
    // Set the isConnected variable to true
    bluetoothDevice.isConnected = true;
    console.log('Getting Accelerometer service (UUID: ' + 'battery_service' + ')... ');
    // Get the accelerometer service (if it's not enabled, the error will be caught below)
    return server.getPrimaryServices();
  })
  .then(services => {
    console.log("Getting characteristics...");
    let queue=Promise.resolve();
    services.forEach(service => {
      console.log(service.uuid);
      switch(service.uuid) {
        
        case ('0000180f-0000-1000-8000-00805f9b34fb'): {
          getBatteryPercent();
        }
      }
      
    })
  })
}

How can I achieve this?

about 4 years ago · Juan Pablo Isaza
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!