简体   繁体   English

Bleno:如何将消息发回给发件人?

[英]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,我对 BLE 环境有点陌生,我的代码工作得很好,买我无法得到某种响应,

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我正在使用两个 npm 依赖项:Bleno 和 Node-wifi

When my code runs, my devices shows up, and I can send data to it via nRF Connect (Android App)当我的代码运行时,我的设备会出现,我可以通过 nRF Connect(Android 应用程序)向它发送数据

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编辑:不要介意 exec + sudo 行,这是针对我的 Rpi 面临的特定问题的临时修复

For Write Requests, the BLE standard only defines that an error code can be returned.对于写入请求,BLE 标准仅定义可以返回错误代码。 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.如果需要,您可以发送带有一些错误文本的通知,或者将最后一条错误消息放在可以读取的特征中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM