Debug Bluetooth communication?

Posted on
  • Hello, I try to debug my source code for creating a bluetooth communication. The sourcecode looks like this:

    NRF.connect(addr).then(function(d) {
      device = d;
      return d.getPrimaryService("service_uuid");
    }).then(function(s) {
      LED2.set();
      console.log("Service ",s);
      return s.getCharacteristic("characteristic_uuid­");
    }).then(function(c) {
      return c.readValue();
    }).then(function(d) {
      console.log("Got:", JSON.stringify(d.buffer));
      device.disconnect();
    }).catch(function() {
      console.log("Something's broken.");
    LED1.set();
    });
    

    I know that the example above is not valid.
    My general question: How can I debug a bluetooth communication? The problem:
    I connect the Puck with Windows to transmit the program to the puck. For establishing a connection from another device (e.g. Android Smartphone), I have to disconnect the Puck from Windows to be able to connect the Android Smartphone with the Puck. If I am disconnected, I will not see the console output on my computer. So how can I see the console result after disconnecting the puck? Could it be stored on the device?

  • Puck.js can have both an incoming and an outgoing bluetooth connection, so in the example you're trying you should be fine - you can be connected with your PC while also running the code above and seeing the response.

    But yes, even if you're connected to something else at the same time you have other ways to debug.

    Serial connection

    You can attach a USB-TTL serial converter to Puck.js: espruino.com/Puck.js#serial-c­onsole

    That way you can get the command-prompt even when you are connected to something else via bluetooth. Only thing to watch out for is to type Serial.setConsole(1) to force the command prompt to stay on serial, even when Bluetooth is connected.

    If you're doing serious development then I'd recommend just leaving a Puck wired up like that.

    Loopback

    You can also force the console onto 'Loopback' which means that anything printed to LoopbackA appears on LoopbackB and vice-versa.

    Not tested, but something like the following should work:

    var log = "";
    LoopbackB.on('data',function(d) {log+=d;});
    setWatch(function() {
      LoopbackA.setConsole();
    }, BTN, {repeat:true,edge:"rising"});
    

    Basically you'd connect with the other device, then press the button and from then on, stuff gets logged in 'log'. Next time you connect with your PC (without pressing the button) log will contain everything that happened.

  • This works. I tried to connect the Smartphone with the Puck instead of the connecting Puck with the smartphone.
    However I always get the error "Connection timeout". Any idea how to solve this? I used the Bluetooth Mac Address I found on my Android Smartphone (Settings -> About the device -> Status).

    FindDevices also returns an empty array:
    NRF.findDevices(function(devices) { console.log(devices); }, 1000);

  • By default, mobile phones can't be connected to by other Bluetooth LE devices - that's almost certainly your problem. You'll need to make an app for the phone that enables it, and even then it won't work on all phones.

    The best way to test would be to try anf connect with the nRF Connect app first, and when that works, try with Puck.js

  • Thank you for your help. I have tried to connect the smartphone with the Puck. The Puck sets a service and updates it on a click. The NRF Connect app shows the data. Everything works fine. Now I would like to send data from the smartphone (central), which is connected, to the puck (peripheral).
    Are there functions to do this or is it only a one-way-communication?

  • Take a look at the examples on espruino.com/Reference#l_NRF_­setServices

    When specifying a service, you can add an onWrite handler function which will get called whenever the service is written to (obviously it also has to be set to writable as well)

  • Thank you so much. I have looked in the API for an own function instead of a handler. I will try it later.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Debug Bluetooth communication?

Posted by Avatar for MobiTech @MobiTech

Actions