Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I followed this site: Communicating with Bluetooth devices over JavaScript[^] and written an API that will read in data from a skincare device.

JavaScript
<button id="pair" onclick="onPairButton()">Connect with BLE device</button>

<script>

  function onPairButton()
  {
    bleDevice=navigator.bluetooth.requestDevice({
      filters: [{
        name: [deviceName]
      }],
      optionalServices: [ bleService ]
    });
    pairedDevice=bleDevice.then(device => { console.log(device.name); return device.gatt.connect(); })
    psData=pairedDevice.then(server => server.getPrimaryService(bleService))
    .then(service => service.getCharacteristic(psCT))
    .then(characteristic => { return characteristic.readValue();})
    .then(value => { 
        var buf = new Uint8Array(value);
        var value1 = buf[0];
        var value2 = buf[1];
        console.log(`Values:`);
        console.log(buf);
        console.log(`${value1}`);
        console.log(`${value2}`);
    })
    .catch(error => { console.error(error); });
    }

</script>


What I have tried:

The raw data is in the format of a byte array in hex which is like this: x00/x00/x00, coming from the following block:

<
.then(value => {
    var buf = new Uint8Array(value);
    var value1 = buf[0];
    var value2 = buf[1];
    console.log(`Values:`);
    console.log(buf);
    console.log(`${value1}`);
    console.log(`${value2}`);
})
.catch(error => { console.error(error); });
}


The problem begins here. I ended up getting undefined value. How can I get JavaScript to read in a hex array? I am totally new to this.
Posted
Updated 18-Nov-21 19:47pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900