//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a button 1/3 of screen width and 1/4 screen height.
btn = app.CreateButton( "Connect", 0.4, 0.15 );
btn.SetOnTouch( btn_OnTouch );
lay.AddChild( btn );
Send = app.CreateButton("send", 0.4, 0.15);
Send.SetOnTouch(send);
app.AddLayout( lay );
//Create Bluetooth serial object.
bt = app.CreateBluetoothSerial();
bt.SetOnConnect( bt_OnConnect )
bt.SetOnReceive( bt_OnReceive );
bt.SetSplitMode( "End", "\n" );
}
//Called when user touches the button.
function btn_OnTouch()
{
bt.Connect( "HC-06" );
}
//Called when we are connected.
function bt_OnConnect( ok )
{
if( ok )
{
bt.Write( "Connecté\n" );
lay.AddChild(Send);
}
else app.ShowPopup( "Failed to connect!" );
}
//Called when we get data from device.
function bt_OnReceive( data )
{
txt = app.CreateButton("hello");
lay.AddChild(txt);
}
function send()
{
if(bt.IsConnected())
{
bt.Write("abc");
}
}Hi Dave,
Thanks for a fast reply. I am using the "USB Arduino" example
with just SetSplitMode added. The phone is an Alcatel Idol 4 with
an OTG cable adapter.
Tried it on both an Arduino Uno clone, and a legit Duelimove, to no avail. You
can see the Tx light on the Arduino flicker when it tries to send
data, so I suspect the problem is with the phone. When connected
to a PC, data from Arduino to PC works fine.
Code for Droidscript and Arduino follows...
// Example showing how to control an Arduino Uno via OTG cable.
//
// This demo requires the following sketch on your Uno:
// http://androidscript.org/demos/arduino/DS_USB_Uno.ino
function OnStart()
{
// Create a layout with objects vertically centered
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
// Create a toggle button
btn = app.CreateToggle( "LED On/Off", 0.4 );
btn.SetOnTouch( btn_OnTouch );
lay.AddChild( btn );
// Add layout to app
app.AddLayout( lay );
// Create USB serial object
usb = app.CreateUSBSerial();
usb.SetSplitMode( "End", "\n" );
if( !usb )
{
app.ShowPopup( "Please connect your Arduino and restart"
);
return;
}
usb.SetOnReceive( usb_OnReceive );
app.ShowPopup( "USB Connected" );
}
// Called when user touches our toggle button
function btn_OnTouch( isChecked )
{
if( !usb ) return;
//Send LED command to Uno
if( isChecked ) usb.Write( "ledh");
else usb.Write( "ledl");
}
// Called when we get data from Arduino
function usb_OnReceive( data )
{
app.ShowPopup( "USB received" );
console.log( data );
}