You are reading a single comment by @MaBe and its replies. Click here to read the full conversation.
  • Ok, let's look at JSON.

    // receiving device
    transfer = function (sJS) {
            try {
                print(JSON.parse(sJS));
            } catch(e){
                print(e);
           }
    }
    
    // sender device
    bleSendJS = function(_id, _cmd) {
        print('_cmd:',_cmd);
        NRF.requestDevice({ filters: [{ id: id }] }).then(function(device) {
            return require("ble_uart").connect(device);
        }).then(function(uart) {
            uart.on('data', function(d) { print("Got:" + JSON.stringify(d)); });
            //uart.write(`\x03\x10transfer('${_cmd}'­)\n`);
            uart.write("\x03\x10transfer('"+_cmd+"')­\n");
            setTimeout(function() {
                uart.disconnect();
                console.log("Disconnected");
            }, 1E3);
        });
    };
    
    var aJS = JSON.stringify({txt : "012345\nabcde\näöüß"}),
            aJSe = JSON.stringify({txt : "012345\nabcde\näöüß"}).replace(/\\n/g, '\\\\n').replace(/\\u/g, '\\\\u');
        id = "ed:4f:05:32:73:ad random";
    
    bleSendJS(id,aJS);
    /* output
    _cmd: {"txt":"012345\nabcde\n\u00E4\u00F6\u00F­C\u00DF"}
    Got:"<- Serial1\r\n>"
    Got:"SyntaxError: SyntaxE"
    Got:"rror: Expecting a va"
    Got:"lid value, got UNFIN"
    Got:"ISHED STRING\r\n>"
    Disconnected
    */
    
    setTimeout(()=>{bleSendJS(id,aJSe);},2E4­);
    /* output
    _cmd: {"txt":"012345\\nabcde\\n\\u00E4\\u00F6\­\u00FC\\u00DF"}
    Got:"<- Serial1\r\n>"
    Got:"{ \r\n  \"txt\": \"012345"
    Got:"\\nabcde\\n\\xE4\\xF6\\xF"
    Got:"C\\xDF\"\r\n }\r\n>"
    Disconnected
    
    */
    

    Hope you have an idea why the extra escaping is needed?

About

Avatar for MaBe @MaBe started