GATT operation not permitted

70 Views Asked by At

I build FTMS webApp using JavaScript and web Bluetooth . I connect to the indoor bike the error message will pop ( GATT operation not permitted. ) browser used (chrome) how to fix this bug help me !

<script>
        async function connectToDevice() {
            try {
                const device = await navigator.bluetooth.requestDevice({
                    filters: [{ services: ['cycling_power'] }],
                });
              
                const server = await device.gatt.connect();
                const cpsService = await server.getPrimaryService('cycling_power');
                const powerCharacteristic = await cpsService.getCharacteristic('cycling_power_measurement');
                // const cadenceCharacteristic = await cpsService.getCharacteristic('0x0483');    
                const powerValueSpan = document.getElementById('powerValue');
                // const cadenceValueSpan = document.getElementById('cadenceValue');
                device.addEventListener('gattserverdisconnected', () => {
                    // Handle device disconnection here
                });

                const updateValues = async () => {
                    console.log("8");
                    const powerValue = await powerCharacteristic.readValue();
                    console.log(powerValue);
                    // const cadenceValue = await cadenceCharacteristic.readValue();

                    // Parse power and cadence values according to the Bluetooth standard.
                    const power = powerValue.getInt16(0, true) / 10; // Divide by 10 for 1 decimal place
                    // const cadence = cadenceValue.getUint8(0);
                     console.log(power);
                    powerValueSpan.textContent = power + ' watts';
                    // cadenceValueSpan.textContent = cadence + ' RPM';
                };

                updateValues();

                // Set up a periodic update (e.g., every 0.5 seconds)
                setInterval(updateValues, 500);
            }
 catch (error) {
  if (error instanceof DOMException && error.name === 'NotAllowedError') {
    // Handle permission-related errors
    console.error('Permission to access Bluetooth denied.');
  } else {
    // Handle other GATT-related errors
    console.error('GATT operation failed:', error);
  }
}
        }

        const connectButton = document.getElementById('connectButton');
        connectButton.addEventListener('click', connectToDevice);
    </script>

i need solution to fix this bug

0

There are 0 best solutions below