• Hi,

    I want to be able to flash the Puck on the fly with some Javascript code over BLE and inside my own Android app. What is the best way to do that?

    Cheers.

  • Take a look at http://www.espruino.com/About+Bluetooth+­LE

    You basically want to get the characteristic "6e400002-b5a3-f393-e0a9-e50e24dcca9e" on the service "6e400001-b5a3-f393-e0a9-e50e24dcca9e" and then write to it repeatedly with blocks of 20 characters or less.

    You basically just send the raw JavaScript code, although it's going in as if you typed it, so you need to be careful to put open brackets before newlines:

    // good
    function hello() {
      // stuff
    }
    // bad
    function hello()
    {
      // stuff
    }
    

    In fact, Nordic provide the source code to their apps - including the Nordic UART, so it should be pretty easy to get something working by copying their code.

  • Actually the easiest way is to use free coding tool DroidScript with the free Puck.js plugin.
    Here is the code (taken from the Puck.js plugin docs)

    app.LoadPlugin( "PuckJS" );
    
    function OnStart()
    {
      lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
    
      var code = "digitalWrite( LED1, 1 )"; 
    
      txt = app.CreateTextEdit( code, 0.9, 0.8, "MultiLine" );
      lay.AddChild( txt );
      app.AddLayout( lay );
    
      layHoriz = app.CreateLayout( "Linear", "Horizontal" );
      lay.AddChild( layHoriz );
    
      btnSend = app.CreateButton( "Send", 0.3, 0.1 );
      btnSend.SetOnTouch( btnSend_OnTouch );
      layHoriz.AddChild( btnSend );
    
      btnSave = app.CreateButton( "Save", 0.3, 0.1 );
      btnSave.SetOnTouch( btnSave_OnTouch );
      layHoriz.AddChild( btnSave );
    
      btnReset = app.CreateButton( "Reset", 0.3, 0.1 );
      btnReset.SetOnTouch( btnReset_OnTouch );
      layHoriz.AddChild( btnReset );
    
      microbit = app.CreatePuckJS();
      microbit.Scan( "Puck" );
    }
    
    function btnSend_OnTouch()
    {
      microbit.SendCode( txt.GetText() );
    }
    
    function btnSave_OnTouch()
    {
      microbit.Save();
    }
    
    function btnReset_OnTouch()
    {
      microbit.Reset();
    }
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Best way to send Javscript code with own Android app.

Posted by Avatar for user72846 @user72846

Actions