app.LoadPlugin( "BluetoothLE" );
function OnStart()
{
//Create a layout.
lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
lay.SetBackColor( "#222222" );
//Create a text box for showing received data.
txt = app.CreateText( "", 0.9, 0.6, "Log" );
txt.SetBackColor( "black" );
lay.AddChild( txt );
//Create a connect button.
btn = app.CreateButton( "Connect", 0.4, 0.1 );
btn.SetOnTouch( btn_OnTouch );
lay.AddChild( btn );
//Create an Analog toggle button.
tglAn = app.CreateToggle( "Analog Read", 0.4 );
tglAn.SetMargins( 0, 0.02, 0, 0 );
tglAn.SetOnTouch( tglAn_OnTouch );
lay.AddChild( tglAn );
//Create an LED toggle button.
tglLed = app.CreateToggle( "LED", 0.4 );
tglLed.SetMargins( 0, 0.02, 0, 0 );
tglLed.SetOnTouch( tglLed_OnTouch );
lay.AddChild( tglLed );
//Add layout to app.
app.AddLayout( lay );
//Create BLE component.
ble = app.CreateBluetoothLE();
ble.SetUartMode( "Hex" );
ble.SetOnSelect( OnSelect );
ble.SetOnConnect( OnConnect );
ble.SetOnUartReceive( OnUartReceive );
}
//Called when 'Connect' button pressed.
function btn_OnTouch( isChecked )
{
ble.Select();
}
//Called when user selects BLE device.
function OnSelect( name, address )
{
ble.Connect( address, "UART,RedBear" );
}
//Called after device connects.
function OnConnect()
{
app.ShowPopup( "Connected!" );
}
//Called when user touches analog button.
function tglAn_OnTouch( isChecked )
{
if( isChecked ) ble.SendUart( "A0,01,00" );
else ble.SendUart( "A0,00,00" );
}
//Called when user touches LED button.
function tglLed_OnTouch( isChecked )
{
if( isChecked ) ble.SendUart( "05,01,00" );
else ble.SendUart( "05,00,00" );
}
//Called when data arrives from board.
function OnUartReceive( data )
{
txt.Log( data );
}