[Sample] List and connect to Paired Bluetooth Serial devices

147 views
Skip to first unread message

Dave Smart

unread,
Jul 17, 2017, 10:58:15 AM7/17/17
to DroidScript



//Called when application is started.
function OnStart()
{
   
//Create a layout with objects vertically centered.
    lay
= app.CreateLayout( "linear", "VCenter,FillXY" );
    lay
.SetBackColor( "#222222" );
 
   
//Create a list view.
    lst
= app.CreateList( "", 0.9, 0.5 );
    lst
.SetBackColor( "black" );
    lst
.SetOnTouch( lst_OnTouch );
    lay
.AddChild( lst );
   
   
//Get a list of paired devices into listview.
   
var devices = app.GetPairedBtDevices();
   
for( d in devices)
       lst
.AddItem( devices[d].name, devices[d].address );
 
   
//Create an edit with commands to send.
    edt
= app.CreateTextEdit( "Hello from Android!", 0.9, 0.2, "NoSpell" );
    edt
.SetMargins( 0,0.04,0,0 );
    edt
.SetBackColor( "black" );
    lay
.AddChild( edt );
   
   
//Create an send button.
    btnSend
= app.CreateButton( "Send", 0.4, 0.1 );
    btnSend
.SetMargins( 0,0.02,0,0 );
    btnSend
.SetOnTouch( btnSend_OnTouch );
    lay
.AddChild( btnSend );
 
   
//Create Bluetooth serial object.
   
//(also set newline as command separator)
    bt
= app.CreateBluetoothSerial();
    bt
.SetOnConnect( bt_OnConnect )
    bt
.SetOnReceive( bt_OnReceive );
    bt
.SetSplitMode( "End", "\n" );
 
   
//Add main layout to app.
   
app.AddLayout( lay );
}


//Handle list selection.
function lst_OnTouch( title, body, type, index )
{
    app
.ShowProgress( "Connecting..." );
    bt
.Connect( body );
    lst
.SelectItemByIndex( index );
}


//Called when we are connected.
function bt_OnConnect( ok )
{
    app
.HideProgress();
   
   
if( ok ) {
        app
.ShowPopup( "Connected!" );
   
}
   
else {
        app
.ShowPopup( "Failed to connect!" );
        lst
.SelectItemByIndex( -1 );
   
}
}


//Called when user touches send button.
function btnSend_OnTouch()
{  
   
var s = edt.GetText();
   bt
.Write( s + "\n" );
}


//Called when we get data from remote device.
function bt_OnReceive( data )
{
    app
.ShowPopup( data );
}




Lee Tarry

unread,
Jul 19, 2017, 9:03:19 AM7/19/17
to DroidScript
dave would this work phone to phone?

Steve Garman

unread,
Jul 19, 2017, 11:30:27 AM7/19/17
to DroidScript
No Lee, this will not work phone to phone.

Lee Tarry

unread,
Jul 19, 2017, 5:48:07 PM7/19/17
to DroidScript
ok thanks.

Dave Smart

unread,
Jul 19, 2017, 5:54:34 PM7/19/17
to DroidScript
Actually, I've just implemented device to device BT serial.  Will be available for experimentation in next alpha :)

Lee Tarry

unread,
Jul 20, 2017, 2:32:56 AM7/20/17
to DroidScript
Ooooh how do we get on the alpha list?

Dave Smart

unread,
Jul 20, 2017, 8:42:59 AM7/20/17
to DroidScript
You just need to be on the beta forum. I've just invited you :)

Artem Kolesnik

unread,
Jul 21, 2017, 10:44:59 AM7/21/17
to DroidScript
Can you invite me too, please?

Steve Garman

unread,
Jul 22, 2017, 7:50:16 AM7/22/17
to DroidScript
Please send an email from the address you want to join the group requesting to join the beta group to support[at]droidscript.org
Reply all
Reply to author
Forward
0 new messages