Android Programming Press on the image to return to the main documentation page.

Serial

List of types:

BluetoothAdmin
Serial

BluetoothAdmin

BluetoothAdmin allows you to administrate the Bluetooth adapter.
Using this object you can enable or disable the adapter, monitor its state and discover devices in range.
DiscoveryStarted and DiscoveryFinished events are raised when a discovery process starts or finishes.
StateChanged event is raised whenever the adapter state changes. The new state and the previous state are passed.
The values correspond to the STATE_xxxx constants.
DeviceFound event is raised when a device is discovered. The device name and mac address are passed.

Permissions:

android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.ACCESS_COARSE_LOCATION

Events:

StateChanged (NewState As Int, OldState As Int)
DiscoveryStarted
DiscoveryFinished
DeviceFound (Name As String, MacAddress As String)

Members:


  CancelDiscovery As Boolean

  Disable As Boolean

  Enable As Boolean

  Initialize (EventName As String)

  IsEnabled As Boolean

  IsInitialized As Boolean

  StartDiscovery As Boolean

  STATE_OFF As Int

  STATE_ON As Int

  STATE_TURNING_OFF As Int

  STATE_TURNING_ON As Int

Members description:

CancelDiscovery As Boolean
Cancels a discovery process.
Returns False if the operation has failed.
Disable As Boolean
Turns off the Bluetooth adapter. The adapter will not be immediately disabled. You should use the StateChanged event to monitor the adapter.
This method returns False if the adapter cannot be disabled.
Enable As Boolean
Turns on the Bluetooth adapter. The adapter will not be immediately ready. You should use the StateChanged event to find when it is enabled.
This method returns False if the adapter cannot be enabled.
Initialize (EventName As String)
Initializes the object and sets the subs that will handle the events.
IsEnabled As Boolean
Tests whether the Bluetooth adapter is enabled.
IsInitialized As Boolean
Tests whether the object is initialized.
StartDiscovery As Boolean
Starts a discovery process. You should handle DiscoveryStarted, DiscoveryFinished and DeviceFound events to get more information about the process.
Returns False if the operation has failed.
STATE_OFF As Int
STATE_ON As Int
STATE_TURNING_OFF As Int
STATE_TURNING_ON As Int

Serial

The Serial library allows you to connect with other Bluetooth devices using RFCOMM, also named virtual serial port.
This library requires Android 2.0 (API level 5) or above.
The Serial object should be declared as a process global object.
After initializing the object you can connect to other devices by calling Connect with the target device MAC address.
This can be done by first getting the paired devices map. This map contains the friendly name and address of each paired device.
To allow other devices to connect to your device you should call Listen. When a connection is established the Connected event will be raised.
There is no problem with both listening to connections and trying to connect to a different device (this allows you to use the same application on two devices without defining a server and client).
A Serial object can handle a single connection. If a new connection is established, it will replace the previous one.
See this tutorial for more information.

Permissions:

android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN

Events:

Connected (Success As Boolean)

Members:


  Address As String [read only]

  Connect (MacAddress As String)

  Connect2 (MacAddress As String, UUID As String)

  Connect3 (MacAddress As String, Port As Int)

  ConnectInsecure (Admin As BluetoothAdmin, MacAddress As String, Port As Int)

  Disconnect

  GetPairedDevices As Map

  Initialize (EventName As String)

  InputStream As java.io.InputStream [read only]

  IsEnabled As Boolean

  IsInitialized As Boolean

  Listen

  Listen2 (Name As String, UUID As String)

  ListenInsecure (Admin As BluetoothAdmin, Port As Int)

  Name As String [read only]

  OutputStream As java.io.OutputStream [read only]

  StopListening

Members description:

Address As String [read only]
Returns the current device MAC address.
Connect (MacAddress As String)
Tries to connect to a device with the given address. The connection is done in the background.
The Connected event will be raised when the connection is ready (or fails).
The UUID used for the connection is the default UUID: 00001101-0000-1000-8000-00805F9B34FB.
Connect2 (MacAddress As String, UUID As String)
Tries to connect to a device with the given address and UUID. The connection is done in the background.
The Connected event will be raised when the connection is ready (or fails).
Connect3 (MacAddress As String, Port As Int)
This method is a workaround for hardware devices that do not connect with Connect or Connect2.
See this
issue for more information.
ConnectInsecure (Admin As BluetoothAdmin, MacAddress As String, Port As Int)
Tries to connect to a device over an unencrypted connection.
Admin - Object of type BluetoothAdmin.
MacAddress - The address of the remote device.
Port - RCOMM channel.
Disconnect
Disconnects the connection (if such exists) and stops listening for new connections.
GetPairedDevices As Map
Returns a map with the paired devices friendly names as keys and addresses as values.
The following code shows a list of available devices and allows the user to connect to one:
Dim PairedDevices As Map
PairedDevices = Serial1.GetPairedDevices
Dim l As List
l.Initialize
For i = 0 To PairedDevices.Size - 1
  l.Add(PairedDevices.GetKeyAt(i))
Next
Dim res As Int
res = InputList(l, "Choose device", -1) 'show list with paired devices
If res <> DialogResponse.CANCEL Then
  Serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address and connect
End If
Initialize (EventName As String)
Initialized the object. You may want to call IsEnabled before trying to work with the object.
InputStream As java.io.InputStream [read only]
Returns the InputStream that is used to read data from the other device.
Should be called after a connection is established.
IsEnabled As Boolean
Tests whether the Bluetooth is enabled.
IsInitialized As Boolean
Listen
Starts listening for incoming connections using the default UUID.
The Connected event will be raised when the connection is established.
Nothing happens if the device already listens for connections.
Listen2 (Name As String, UUID As String)
Starts listening for incoming connections.
The Connected event will be raised when the connection is established.
Nothing happens if the device already listens for connections.
Name - An arbitrary string that will be used for internal registration.
UUID - The UUID defined for this record.
ListenInsecure (Admin As BluetoothAdmin, Port As Int)
Starts listening for incoming unencrypted connections.
Admin - An object of type BluetoothAdmin.
Port - The RFCOMM channel.
Name As String [read only]
Returns the current device friendly name.
OutputStream As java.io.OutputStream [read only]
Returns the OutputStream that is used to write data to the other device.
Should be called after a connection is established.
StopListening
Stops listening for incoming connections.
This will not disconnect any active connection.
Top