Home > Language Reference > Classes > StreamBluetooth

L2CapConnect Method

Creates an outbound L2Cap connection.

Syntax

Public Sub L2CapConnect(ByVal dfAddr As Double, ByRef sUUID As String, ByVal iFrameSize As Integer, ByRef bCancel As Boolean)

ParameterDescription
dfAddrBluetooth address of the remote device.
sUUIDIndentifier of the requested service.
iFrameSizePacket size.
bCancelA reference to a boolean variable you can use to cancel the operation.

Remarks

The L2CapConnect method creates an outbound connection to a given service on a remote device using the L2Cap protocol. After the operation completes, the Bluetooth socket behaves as a client, and you can use the Read and Write methods to exchange data with the remote device.

The dfAddr parameter specifies the Bluetooth address of the remote device. You usually retrieve the address of a remote device using the DiscoverSingleDevice methods of the Bluetooth object. Note that a device address never changes; thus, if your application is designed to always connect to the same device, you can retrieve its address once, and then store it into a database or into the application's preferences.

The sUUID parameter specifies the identifier of the service you wish to connect to. This string must conform to one of the following formats, where x is any valid hexadecimal digit:

FormatDescription
{xxxx}One of the 16 bits UUID predefined by the Bluetooth consortium for standard services.
{xxxxxxxx}One of the 32 bits UUID predefined by the Bluetooth consortium for standard services.
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}Any 128 bits UUID.

If your application is designed to connect to a third party application or to a device such as a GPS, you'll have to find out in their programmer's documentation the services they advertise and the corresponding UUIDs. Note however that most devices communicate using the virtual serial port service, whose UUID is {1101}.

On the other hand, if you are writing a server application, you'll probably have to generate a UUID that uniquely identifies the service your application provides. This UUID will then be used by any client application wishing to connect to your service. Such an identifier can be generated within the HB++ IDE, using the Bluetooth UUID Generator.

The iFrameSize parameter specifies the size of the packets exchanged by the two devices. You should not use the Write method to write less data than this limit. This parameter can range from 1 to 672 inclusive.

The bCancel parameter provides a way to cancel the connection process, which can be very lengthy. It is a reference to any boolean variable that the L2CapConnect method will test periodically while the operation is in progress. As soon as this variable is set to True, the operation is cancelled and the method returns. If you don't need this feature, simply specify False for this parameter.

The following example illustrates how to establish an outbound L2Cap connection. Determination of the remote device address and error handling have been removed for clarity:

Private sConn As StreamBluetooth

Public Sub Connect()
  Dim addr As Double
  Dim f As New frmWait
  
  addr = RetrieveRemoteAddress()
  f.Show hbFormModeless+hbFormPopup

  Set sConn=New StreamBluetooth
  Set sConn.Recipient=Me
  sConn.L2CapConnect addr, "{1101}", f.bCancel
 
  Unload f
End Sub

In this sample, the frmWait form simply contains an informative message and a Cancel button. Its implementation can be as simple as:

Public bCancel  As Boolean

Private Sub cmdCancel_Click()
  Label1.Caption="Cancelling..."
  bCancel=True
End Sub

Note that the L2CapConnect method internally calls the DoEvents function. This implies that any event (such as timers, menu and button clicks, etc) can be raised and processed while your code waits for this function to return. If necessary, you could use a boolean flag to avoid your application initiates another operation while the connection is being established.

The BtWhiteboard sample illustrates how to properly call this method, and notably how to catch the exceptions it can raise.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 5.0Requires a Bluetooth enabled device.
Windows CEWindows CE 3.0-