You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • I don't understand. I ran this on a Pixl, talking to a Puck.js, and it works great using print:

    function bleSendJS(_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\x10print(${_cmd})\n`);
        setTimeout(function() {
            uart.disconnect();
            console.log("Disconnected");
        }, 1E3);
      });
    }
    
    var aJS = JSON.stringify({txt : "012345\nabcde\näöüß"}),    
    id =  "c8:c1:f1:47:c7:79 random";
    bleSendJS(id,aJS);
    

    Reports:

    Got:"{ \r\n  \"txt\": \"012345"
    Got:"\\nabcde\\n\\xE4\\xF6\\xF"
    Got:"C\\xDF\"\r\n }\r\n>"
    

    Using var aJS = E.toJS({txt : "012345\nabcde\näöüß"}) instead does exactly the same.

    Is your transfer function really:

    transfer = function (saJS5) {
            var aJS5;
            try {
                aJS5 = eval('('+saJS5+')');
            } catch(e){
                console.log("error in eval:",e);
            }
    }
    

    If so then the issue is that in transfer you're trying to evaluate something that isn't a string. It's already a properly decoded JSON object.

    So...

    transfer = function (saJS5) {
            var aJS5;
            try {
                aJS5 = eval('('+saJS5+')');
            } catch(e){
                console.log("error in eval:",e);
            }
    }
    
    // What you think you're doing
    transfer("{txt:\"012345\\nabcde\\n\\xE4\­\xF6\\xFC\\xDF\"}") // ok
    
    // what you were doing just by adding '' around a string
    transfer('{"txt":"012345\nabcde\n\u00E4\­u00F6\u00FC\u00DF"}') // error
    
    // what you're actually doing
    transfer({"txt":"012345\nabcde\n\u00E4\u­00F6\u00FC\u00DF"}) // error
    
About

Avatar for Gordon @Gordon started