btWaitEvent (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

NX-200, NX-400, NX-900

Firmware version:

1.10.00

 


 

This function will wait until a Bluetooth event is raised. It has an optional timeout.

 

An event is a notification that something has happened on the Bluetooth interface.

 

The events are queued until btEventDone has been called.

For some events, an appropriate action should be done before calling btEventDone, as described in the table:

 

Event#

Event

Action needed

Action

1

An incoming connection has been received.

Yes

Call btHandleSerialPortIncomingConnection to handle the serial port.

2

A pairing request have been received.

Yes

Call btHandlePairingRequest to find more information about the request and call btSendPairResponse to send the response.

3

A new device has been found.

No

Call btDeviceInfo to find more information about the device.

Note that this event is not generated for known devices, e.g. paired or connected devices, or devices that have been found previously.

4

A known device has been lost.

No

None.

16

A BLE notification has been received.

Yes

Call btleHandleNotification to get the value from the notification.

 

 

btWaitEvent notifies the application of the oldest queued event.

 

This means that until btEventDone is called, btWaitEvent will continue to report the same event.

 

Note: waiting for an event using this function will block the calling thread.

 

 

Input:

timeout : INT (-1,0..32767) (default -1)

Timeout period in milliseconds to wait.

0

-

Disable timeout. The function will return immediately.

-1

-

Wait forever. The function will only return when data is received.

 

 

Output:

dev : STRING

The address of the device the event is related to.

 

Returns: INT

16

-

_BT_EVENT_NOTIFY


A BLE notification has been received.

4

-

_BT_EVENT_DEV_LOST


A known device has been lost.

3

-

_BT_EVENT_DEV_FOUND


A new device has found.

2

-

_BT_EVENT_PAIR_REQ


A pairing request have been received.

1

-

_BT_EVENT_INCOMING


An incoming connection has been received.

0

-

_BT_ERR_NOT_SUPPORTED


The API is not supported.

-1

-

_BT_ERR_NOT_OPEN


The adapter is not powered(see btPower).

-5

-

_BT_ERR_NO_ADAP


Could not find adapter.

-8

-

_BT_ERR_INVAL


Invalid argument.

-9

-

_BT_ERR_NODATA


No valid data found.

-17

-

_BT_ERR_TIMEOUT


Timeout

 

 

Declaration:

FUNCTION btWaitEvent : INT;
VAR_INPUT
  timeout  : INT := -1;
  dev      : ACCESS STRING;
END_VAR;

 

Example:

...
THREAD_BLOCK btThread
VAR
  rc      : INT;
  address : STRING;
  ch      : SINT;
  pass    : DINT;
END_VAR;
  WHILE TRUE DO
     //
    rc := btWaitEvent(timeout:=10000, dev := address);
    IF rc <> _BT_ERR_TIMEOUT THEN
        DebugFmt(message:="event \1: "+address, v1:=rc);
        CASE rc OF
        _BT_EVENT_INCOMING:
          rc := btHandleSerialPortIncomingConnection(ch := ch, port := port);
          DebugFmt(message:="btHandleSerialPortIncomingConnection(\2) : \1, \3", v1:=rc, v2:=ch, v3:=port);
        _BT_EVENT_DEV_FOUND:
          DebugFmt(message:="Found "+address);
        _BT_EVENT_DEV_LOST:
          DebugFmt(message:="Lost "+address);
        _BT_EVENT_PAIR_REQ:
          DebugFmt(message:="Requested confirm for "+address);
          rc := btHandlePairRequest(passkey:=pass);
          DebugFmt(message:="Pass: \4, \1", v1:=rc, v4:=pass);
          rc := guiShowMessage(message:="Is the pairing code "+dintToStr(v:=pass)+" correct?", type := 2, timeout := 20);
          DebugFmt(message:="guiShowMessage: \1", v1:=rc);
           // Accept if "Yes" was pressed
          rc := btSendPairResponse(accept:=(rc = 3));
          DebugFmt(message:="btSendPairResponse: \1", v1:=rc);
        ELSE
          DebugFmt(message:="unknown event: \1", v1:=rc);
        END_CASE;
        btEventDone();
    END_IF;
 
  END_WHILE;
END_THREAD_BLOCK;
...