node.jsbluetoothbleno

Bleno : How to send a message back to the sender?


I'm a bit new to the BLE environment, my code works just fine, buy I can't manage to get some sort of response,

I'll show you the code, and I'll tell you more about my objective

WriteReadCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
    const output = Buffer.from(data, 'hex');
    let payload = "";
    if (hasJsonStructure(String(output))) {
        payload = JSON.parse(String(output));
    }
   
    wifi.scan((error, networks) => {
        if (error) {
            console.log(error);
        } else {
            exec('sudo iwlist scan && echo '+ password);
            console.log(payload.ssid);
            networks.forEach(network =>{
                if(payload.ssid === network.ssid){
                    console.log("Success, wifi found")
                }
                else {
                    console.log("Failed, not found")
                    let data = new Buffer(31);
                    data.writeUInt32LE(this.RESULT_UNLIKELY_ERROR);
                    callback(data);
                }
            });

        }
    });
    callback(this.RESULT_SUCCESS);
};

I'm using two npm depedencies : Bleno, and Node-wifi

When my code runs, my devices shows up, and I can send data to it via nRF Connect (Android App)

As you can see, I have a condition, if true, it returns a success message ...

But when it fails, I'd like to send back a message to the sender device (In my case, a phone)

edit : Don't mind the exec + sudo line, that's a temporary fix for a specific problem I face with my Rpi


Solution

  • For Write Requests, the BLE standard only defines that an error code can be returned. Nothing else. The error code must also be one of the allowed ones. You could send a Notification with some error text if you want, or place the last error message in a characteristic that can be read.