Home > Language Reference > Classes > StreamBluetooth

DataArrival Event

Event raised as new data are received.

Syntax

Public Event DataArrival(ByVal lSize As Long)

ParameterDescription
lSizeAmount of data available for reading, in bytes.

Remarks

The DataArrival event is raised as soon as new data are available. 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.

The lSize parameter specify the number of bytes available in the input stream buffer. You cannot read more bytes than specified by this value, otherwise a runtime error occurs. On the other hand, you can safely read less bytes than available; the remaining data are simply buffered until the next call to the Read function.

If the amount of data to read can be determined in advance (for example because your application only exchange fixed size packets), a typical implementation for this event could be:

Private Const PACKET_SIZE As Long = 12
Private sSock As New StreamBluetooth

Private Sub sSock_DataArrival(ByVal lSize As Long)
  If lSize >= PACKET_SIZE Then
    ' Code that read and process exactly PACKET_SIZE bytes
    ' goes here. For example:
    Dim Foo(1 To PACKET_SIZE) As Byte
    sSock.Read Foo
  End If
End Sub

If the amount of data cannot be determined in advance (for example when receiving strings separated by line feeds), you can use a StreamMemory object in destructive mode, as shown below:

Private sbSock As New StreamBluetooth
Private smBuffer As New StreamMemory

Private Sub Form_Load()
  sBuffer.Destructive=True
  ' Code that initialize sSock omitted for clarity
End Sub

Private Sub sSock_DataArrival(ByVal lSize As Long)
  Dim sLine As String, k As Integer
  
  smBuffer.SeekToEnd
  sbSock.Read smBuffer, lSize
  
  Do
    k = smBuffer.FindByte(0,Asc("\n"))
    If k<0 Then Exit Do
    
    smBuffer.SeekToBegin
    smBuffer.Read sLine, k+1

    MsgBox TrimCrlf(sLine)
  Loop
End Sub

The BtChat, BtWhiteboard and BtGPS samples illustrate various implementations for this event.

System requirements

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