DOMException: GATT operation not permitted.

Hi!

I used the next code from ready JS example from Chrome WEB bluetooth API to send bytes to BLE app UART example.

Connection is established ok.

But I can't send data from function due to error: DOMException: GATT operation not permitted.

Please tell me how can I fix it? Thank you!

  var myCharacteristic;

  var deviceName;
  
  function connect() {

     let serviceUuid = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
	 let characteristicUuid = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
	 let characteristicRx = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";


    navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
    .then(device => {
      log('Connecting...');
      deviceName = device.name;
      return device.gatt.connect();
    })
    .then(server => {
      console.log('Getting Service...');
      return server.getPrimaryService(serviceUuid);
    })
    .then(service => {
      console.log('Getting Characteristic...');
      return service.getCharacteristic(characteristicUuid);
    })
    .then(characteristic => {
      myCharacteristic = characteristic;
      return myCharacteristic.startNotifications().then(_ => {
        console.log('> Notifications started');
        log("Connected to: " + deviceName);
        myCharacteristic.addEventListener('characteristicvaluechanged',
            handleNotifications);
      });
    })
    .catch(error => {
      console.log('Argh! ' + error);
    });
  }

  function disconnect() {
    if (myCharacteristic) {
      myCharacteristic.stopNotifications()
      .then(_ => {
        console.log('> Notifications stopped');
        log("Disconnected")
        myCharacteristic.removeEventListener('characteristicvaluechanged',
            handleNotifications);
      })
      .catch(error => {
        console.log('Argh! ' + error);
      });
    }
  }

  function handleNotifications(event) {
    let value = event.target.value;
    log(deviceName + "> " + new TextDecoder().decode(value));
    console.log('> Received: ' + new TextDecoder().decode(value));
  }

  function send() {
    var data = document.getElementById("input").value;
    log("You> " + data);
    myCharacteristic.writeValue(str2ab(data+"\n"))
    document.getElementById("input").value = "";
  }

  • The way I understand Chrome, this means, that for some reason the Write bit for that characteristic isn't set? Argh! Please help! )

  • Looks like I have exactly the same problem.  I am running the code in HTTPS, the code below

    $(document).ready(function(){
        let bluetoothDevice = null;
        let requestDeviceParams = {
            filters: [
                {name: ["ko"]}
            ],
            optionalServices: ['10001001-0000-1000-8000-00805f9b34fb']
        }
       
        let name = document.querySelector('#date-input')
        $("#synch-date-time").click(() => {
            if (document.querySelector('#date-input').value === '') {
                console.error('empty date field, please fill the fields')
                return
            }
            asyncResultNotif();
        })
    
    
    
        async function asyncResultNotif(){
            return await navigator.bluetooth.requestDevice(requestDeviceParams)
            .then(device => {
                bluetoothDevice = device.gatt;
               
                return device.gatt.connect();
            })
            .then(server => {
                if (bluetoothDevice.connected) {
                    return server.getPrimaryService('10001001-0000-1000-8000-00805f9b34fb');
                } else {
                    console.log('cant connect to prime server')
                }
            })
            .then(service => {
                if (bluetoothDevice.connected) {
                    return service.getCharacteristic('10001111-0000-1000-8000-00805f9b34fb'); // write one value
                } else {
                    console.log('cant connect to characteristic')
                }
            })
            .then(characteristic => {
                if (bluetoothDevice.connected) {
                    // const resetEnergyExpended = Uint8Array.of(1);
                    // return characteristic.writeValue(resetEnergyExpended);
                    let data = '{"ssid": "' +name.value
                        data +='"}'
                        console.log(data)
                    let encoder = new TextEncoder('utf-8');
                    let val = encoder.encode(data);
                    // return characteristic.writeValue(val.buffer)
                   
                    return characteristic.writeValue(new Uint8Array([1]))
                } else {
                    console.log('cant send message over BLE')
                }
            }). then(() => {
                    if (bluetoothDevice.connected) {
                        bluetoothDevice.disconnect();
                        console.log('> Bluetooth Device now disconnected');
                    } else {
                        console.log('> Bluetooth Device is already disconnected');
                    }
            }).catch(error => {
                console.error('Argh! ' + error)
            });
        }
    });
  • Looks like my issue was actually related to an incorrect UUID set in characteristic I had "10001111-0000-1000-8000-00805f9b34fb" and when changed to "10001000-0000-1000-8000-00805f9b34fb" it start working.

Related