Back
CreateBluetoothSerial
The BluetoothSerial 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();
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
lst = app.CreateList( "", 0.9, 0.5 );
lst.SetOnTouch( lst_OnTouch );
lay.AddChild( lst );
var devices = app.GetPairedBtDevices();
for( var d in devices )
lst.AddItem( devices[d].name, devices[d].address );
app.AddLayout( lay );
bt = app.CreateBluetoothSerial();
bt.SetOnConnect( bt_OnConnect );
bt.SetSplitMode( "End", "\n" );
bt.Listen( true );
}
function lst_OnTouch( title, body, type, index )
{
app.ShowProgress( "Connecting..." );
bt.Connect( title );
}
function bt_OnConnect( ok, data )
{
app.HideProgress();
if( ok ) {
if( typeof data == "object" )
app.ShowPopup( "Connected!" );
else
alert( "Connected to " + ok + " (" + data + ")" );
} else
app.ShowPopup( "Failed to connect!" );
}
The following methods are available on the BluetoothSerial object:
GetType() →
string: "BluetoothSerial"
string: "Text" or "Int" or "Hex"
Clears the Bluetooth buffer stored in the serial connection.
string
unknown
Connect to a Bluetooth device via its name or address. The oppenent must have called bt.Listen before.
Disconnect your device from an eventually existant connection. Calls the OnDisconnect callback function on both devices.
Returns the control class name.
Returns a boolean indicating whether the Bluetooth is enabled or not.
Returns a boolean indicating whether a Bluetooth connection exists to another device.
Returns a Boolean indicating whether the device was been paired with a particular bluetooth name.
boolean
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.
string: comma separated: "boolean", "char", "byte", "short", "int", "long", "float", "double"
Allows access to other functions defined on the object in Java via reflection.
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.
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.
SetOnDisconnect will be called on both devices after disconnecting from an existing bluetooth connection.
SetOnReceive is called automatically after data has been received via the Bluetooth serial connection.
string: "End"
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:
number: milliseconds
SetTimeout
Send data over the serial Bluetooth connection to the other device.