Back
CreateBluetoothSerial
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='Text' )
→
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!" );
}
from native import app
def OnStart():
global bt
app.ShowProgress( "Enabling Bluetooth" )
if not app.IsBluetoothEnabled():
app.SetBluetoothEnabled( True )
while not 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)
def lst_OnTouch( name, address ):
app.ShowProgress( "Connecting..." )
bt.Connect( address )
def bt_OnConnect( ok, data ):
app.HideProgress()
if ok:
if typeof data == "object":
app.ShowPopup( "Connected!" )
else:
app.Alert( "Connected to " + ok + " (" + data + ")" )
bt.Write("hello from " + app.GetBluetoothName())
else:
app.ShowPopup( "Failed to connect!" )
Properties
The following properties are available on the BluetoothSerial object:
Methods
The following methods are available on the BluetoothSerial object:
GetType() →
String: “BluetoothSerial”
all types
Boolean
String
Number: milliseconds
String: “Text” or “Int” or “Hex”
String: comma “,” separated: “boolean”, “char”, “byte”, “short”, “int”, “long”, “float”, “double”, “String”, “CharSequence”, “...”
String: “Hex” or
“Int” or
“Text” or
“<encoding>”
String: “End” or “Start-End” or “Size” or “Head”
String
Number: integer
Object: {
command:
args }
function(
name,
address
)
bls.Batch
Batch method calls to be able to set all object's properties at once.
Note that you need to specify each parameter (use “” or null to leave some out)
Inherited methods can be called by appending an underscore to the function name (ie. txt.Batch({ SetBackColor_: [“red”] })
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.data
An object for saving individual extra properties.
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.MethodAllows 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.SetDataMode
Enable sending data in several modes.
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.SetSplitModeTells 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:
mode | p1 | p2 |
---|
Size | Size of one data package | - |
End | Byte indicating end of data | - |
Start-End | Byte indicating start of data | Byte indicating end of data |
bls.SetTimeout
Set a timeout after which the connection will canceled when no communication happened
bls.Write
Send data over the Bluetooth serial connection to the other device.