Home > Language Reference > Classes

StreamBluetooth Class

  + Object
    + Stream
      + StreamBluetooth

Description

The StreamBluetooth class encapsulates Bluetooth sockets, which allow data transfers between two devices over a Bluetooth link, using either the RfComm or the L2Cap protocol. This class is instanciable, but can neither be derived nor duplicated. For more information about streams, see also Data Streams.

Note that instantiating a StreamBluetooth object on a device that does not have a Bluetooth support causes a runtime error to occur. To prevent the default, unfriendly error message to be displayed, it is advised to instantiates this class from a function protected by an error handler, as described on the Bluetooth page.

The L2CAP protocol

This protocol doesn't support flow control. On the other hand, it allows multiple inbound connections. It is typically used for implementing network games, where multiple client devices have to connect to a single game server. To set up a L2Cap server, you'll do the following;

Your socket is now listening, and ready to accept inbound connections. Each time a remote device tries to connect to your application, the following happens:

If the local device is the master of a piconet, the socket remains in the listening state and can accept new inbound connections. Otherwise, the socket is placed in a hold state, rejecting any new connection until the current one is closed. For more information about managing piconet, refer to the Bluetooth class.

To establish a connection as a client to a remote L2Cap server, you'll do the following:

The RfComm protocol

This protocol emulates a serial connection. On the other hand, it only allows peer-to-peer connections. It is typically used for reliable data exchange between two devices, such as a GPS and a computer. To set up a RfComm server, you'll do the following:

Your socket is now listening, and ready to accept inbound connections. Each time a remote device tries to connect to your application, the following happens:

After an inbound connection is established, the socket is always placed in a hold state, rejecting any new connection until the current one is closed. To establish a connection as a client to a remote RfComm server, you'll do the following:

A typical application

Most applications behave both as client and server. They maintain two sockets: one for accepting inbound connections, and one for the data connection itself. The following example shows the skeleton of a typical Bluetooth application:

Private Const sUUID As String = "{00000000-0000-0000-0000-000000000000}" ' Insert your own UUID here

Private sServer As StreamBluetooth  ' The listening socket
Private sClient As StreamBluetooth  ' Socket to exchange data

Private Sub Form_Load()
  Set sServer=New StreamBluetooth ' Error handling omitted for clarity
  Set sServer.Recipient=Me
  sServer.RfCommListen sUUID, "MyApplication"
End Sub

Private Sub Form_Unload()
  If Not sServer Is Nothing Then sServer.Close
  If Not sClient Is Nothing Then sClient.Close
End Sub

Private Sub sServer_ConnectRequest(ByVal dfAddr As Double, ByRef bAccept As Boolean)
  bAccept=sClient Is Nothing
End Sub

Private Sub sServer_ConnectedInbound(ByVal sConn As StreamBluetooth)
  Set sClient=sConn
  Set sClient.Recipient=Me
End Sub

Private Sub sClient_Disconnected()
  sClient.Close
  Set sClient=Nothing
End Sub

Private Sub sClient_DataArrival(ByVal lSize As Long)
  ' Code that process data exchange with clients
  ' omitted for clarity
End Sub

Private Sub Button1_Click()
  Double addr

  If Not sClient Is Nothing Then
    MsgBox "Already connected!"
    Exit Sub
  End If

  addr=ChooseRemoteDevice() ' This function omitted for clarity

  Set sClient=New StreamBluetooth
  Set sClient.Recipient=Me
  sClient.RfCommConnect addr, sUUID, False
End Sub

The BtChat and BtWhiteboard samples show full functionnal implementation of such an application. You can also refer to the BtGPS sample, that shows how to connect to an arbitrary Bluetooth device, such as a GPS.

Receiving data

Unlike other streams, you should not call the Read function on a StreamBluetooth object and expect the call to block until the requested number of bytes are received. Instead, you should wait for the system to notify your code that enough data are available, and then read it. This behavior is imposed by the underlying architecture of the Bluetooth API. For more information on receiving data, refer to the DataArrival event.

Members

MembersDescription
Close Closes an open socket.
ConnectedInbound Event raised after an inbound connection is accepted.
ConnectRequest Event raised when a remote device performs a connection request.
DataArrival Event raised as new data are received.
Disconnected Event raised when the socket is closed by the remote device.
L2CapConnect Creates an outbound L2Cap connection.
L2CapListen Sets up a socket as a listener using the L2Cap protocol.
RfCommConnect Creates an outbound RfComm connection.
RfCommListen Sets up a socket as a listener using the RfComm protocol.
Inherited from StreamDescription
Position Stream pointer position.
Read Reads data from a stream.
Seek Moves the stream pointer.
SeekToBegin Moves the pointer to the beginning of the stream.
SeekToEnd Moves the pointer to the end of the stream.
Size Total size of the data in a stream.
Write Writes data to a stream.
Inherited from ObjectDescription
ClassID Returns the type identifier corresponding to the actual class of the object.
Implements Determines whether the object implements the features of a given class.
Iterate Event raised to iterate over the elements of a container object.
Recipient Recipient of events sent by the object.
Serialize Event raised to serialize the object content into a stream.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 5.0Requires a Bluetooth enabled device.