Back

CreateBluetoothSerial

Hello World
Content:
- Methods

The CreateBluetoothSerial object is used for communicating with other Bluetooth devices.The 'Text' mode is set by default, but integer and hexadecimal values can also be sent.

bls = app.CreateBluetoothSerial( mode ) → app object: BluetoothSerial

Example - Connect to Device

function OnStart()
{
    app.ShowProgress( "Enabling Bluetooth" );
    if( !app.IsBluetoothEnabled() )
        app.SetBluetoothEnabled( true );

    while( !app.IsBluetoothOn() ) app.Wait(.4);
    app.HideProgress();

    bt = app.CreateBluetoothSerial();
    bt.SetOnConnect( bt_OnConnect );
    bt.SetSplitMode( "End", "\n" );
    bt.Listen( true );


    lst = app.CreateBluetoothList();
    lst.SetOnTouch(lst_OnTouch);
}

function lst_OnTouch( name, address )
{
    app.ShowProgress( "Connecting..." );
    bt.Connect( address );
}

function bt_OnConnect( ok, data )
{
    app.HideProgress();

    if( ok ) {
        if( typeof data == "object" )
            app.ShowPopup( "Connected!" );
        else
            alert( "Connected to " + ok + " (" + data + ")" );

        bt.Write("hello from " + app.GetBluetoothName());
    } else
        app.ShowPopup( "Failed to connect!" );
}
    Copy     Copy All       Run      

Methods

The following methods are available on the BluetoothSerial object:

Clear()
GetType() → string: “BluetoothSerial”
IsBluetoothEnabled() → boolean
IsConnected() → boolean
IsPaired( name ) → boolean
Method( name, types, p1, p2, p3, p4 ) → all types
Write( data )
boolean
string
unknown
number: milliseconds
string: “Text” or “Int” or “Hex”
string: “End” or “Start-End” or “Size”
string
number: integer
list: boolean,char,byte,short,int,long,float,double,String,CharSequence,...
function( name, address )
function( data )
bls.Clear
Clears the Bluetooth buffer of the serial connection.
bls.Connect
Connect to a Bluetooth device via its address. The oppenent must have called bt.Listen before.
bls.Disconnect
Disconnect your device from an eventually existant connection. Calls the OnDisconnect callback function on both devices.
bls.GetType
Returns the control class name.
bls.IsBluetoothEnabled
Checks if Bluetooth is enabled or not.
bls.IsConnected
Checks if a Bluetooth connection exists to another device.
bls.IsPaired
Checks if a specific device is paired using its Bt name.
bls.Listen
Listen to your serial connection for any incoming mesages by passing true as first argument, or stop listening by passing false. It has to be called before an other device can connect with yours via bt.Connect.
bls.Method
Allows access to other functions defined on the object in Java via reflection.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.
bls.RequestEnable
If Bluetooth is disabled, shows an android dialog which asks the user to enable bluetooth connection. If granted, bluetooth will be enabled automatically. No callback fired.
bls.SetOnConnect
If the device has sent the connection request
    name is of type boolean (true if the connection was established successful)
    and address is your BluetoothSerial object

if the device has received the connection request
    name is a string with the clients bluetooth name
    and address includes the bluetooth address.
bls.SetOnDisconnect
SetOnDisconnect will be called on both devices after disconnecting from an existing bluetooth connection.
bls.SetOnReceive
The SetOnReceive callback is called automatically after data has been received via the Bluetooth serial connection.
bls.SetSplitMode
Tells the serial listener how to split received data. Splitted data will result in multiple OnReceive calls.
p2 and p3 have different purposes for different modes:
modep1p2
SizeSize of one data package-
EndByte indicating end of data-
Start-EndByte indicating start of dataByte indicating end of data
bls.SetTimeout
SetTimeout
bls.Write
Send data over the Bluetooth serial connection to the other device.