• Hi!

    @Robin put up a good link for detecting double-clicks. While it's not that hard, it's trivial to detect long vs short clicks, so you could always just do that?

    I reckon the bluetooth flag tutorial you found is a good place to start. The initial example (using UART) uses ble_simple_uart - which abstracts away so much that you can't connect separately.

    However you could just use the ble_uart module referenced at https://www.espruino.com/BLE+UART

    So the receiver stays exactly as-is on http://www.espruino.com/BLE+Communications#receiver-bluetooth-peripheral-device-

    Then for the transmitter

    var log = print;// or function(){} to avoid printing anything
    
    var uart;
    function connect() {
      log("Searching for device...");
      NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }] }).then(function(device) {
        log("Connecting...");
        device.on('gattserverdisconnected', function() {
          log("Disconnected");
          uart = undefined;
          setTimeout(connect,100);
        });
        return require("ble_uart").connect(device);
      }).then(function(u) {
        log("Connected");    
        uart = u;
        uart.write("\x03"); // send ctrl-c to clear anything on the input line
      }).catch(function(e) {
        log("Error: "+e);
        setTimeout(connect,100);
      });
    }
    
    function flag(e) {
      var l = e.time-e.lastTime;
      log("Button press length",l,(l>0.3)?"long":"short");
      log("Sending command");
      if (!uart) {
        log("Not connected!");
        return;
      }
      uart.write("digitalPulse(LED,1,10);\n");
    }
    
    // When button pressed
    setWatch(flag, BTN, {repeat:true, edge:"falling"});
    // start connecting...
    connect();
    

    The page with the flag example had another option too (using custom characteristics): http://www.espruino.com/BLE+Communications#sending-commands-custom-service-

    Working with that is basically the same - you just use device.gatt.connect() rather than require("ble_uart").connect(device)

About

Avatar for Gordon @Gordon started