btListen (Function)

Top  Previous  Next

Architecture:

X32 / NX32

Device support:

MX2 pro, DX4 pro, AX9 pro, MX2 turbo/encore/warp, AX9 turbo

Firmware version:

1.05


This function allows an incoming connection from another Bluetooth device. The function returns with a connection ID like the btConnect function. When an incoming connection is established, it is shown with the btConnection function block.

The PIN used to allow access is set with btSetPin. The connection ID can be closed with btDisconnect. Please see btConnect for further details on connection ID, PIN etc.

 

By calling btListen, the Bluetooth module becomes visible for other Bluetooth devices. This means that if another Bluetooth device makes a search, it will see the Name provided with btOpen. When a device is connected through btListen, the Bluetooth module will become invisible again, but only if no more btListen is waiting for connections.

 

When a connection to a device is established, the Bluetooth smart antenna stores the pairing info (address, passkey etc.) which allows for a lost connection to be reestablished without user interaction.

The Bluetooth smart antenna supports up to 16 simultaneous pairings, and when 16 devices have been paired, no new pairings will be stored.

 

 

Input:

none

 

Returns: SINT

The connection ID

-1

- Bluetooth library is not open.

-9

- Failed to listen for a connection.

-14

- Maximum number of connections exceeded.

 

Declaration:

FUNCTION btListen : SINT;

 

 

Example:

INCLUDE rtcu.inc
 
PROGRAM test;
VAR
  rc           : INT;
  ConID_out   : SINT;
  ConID_in     : SINT;
  buffer       : ARRAY[1..127] OF SINT;
  RXbuffer_out : ARRAY[1..127] OF SINT;
  RXbuffer_in  : ARRAY[1..127] OF SINT;
  btRX_out     : btReceiveData;
  btRX_in      : btReceiveData;
  btCon_out   : btConnection;
  btCon_in   : btConnection;
END_VAR;
 
// Open the Bluetooth library
btOpen(name := "RTCU MX2");
 
// Set the Pin for incoming connections
btSetPin(pin := "1234");
 
// Initiate the outgoing connection
ConID_out:=btConnect(address:="00:e0:98:ae:17:23",pin:="0000");
DebugFmt(message:="Outgoing Connection ID =\1", v1:=ConID_out);
 
// Initiate the incoming connection
ConID_in:=btListen();
DebugFmt(message:="Incoming Connection ID =\1", v1:=ConID_in);
 
// setup the receive and connection function blocks
btRX_out.Data:=ADDR(RXbuffer_out);
btRX_out.MaxSize:=SIZEOF(RXbuffer_out);
btRX_out.id:=ConID_out;
btCon_out.id:=ConID_out;
 
// setup the receive and connection function blocks
btRX_in.Data:=ADDR(RXbuffer_in);
btRX_in.MaxSize:=SIZEOF(RXbuffer_in);
btRX_in.id:=ConID_in;
btCon_in.id:=ConID_in;
 
BEGIN
btCon_out();
btCon_in();
btRX_out();
btRX_in();
 
IF btCon_out.changed THEN
  DebugMsg(message:="Connection info changed:");
  DebugFmt(message:="    Connected=\1",v1:=INT(btCon_out.connected));
  DebugMsg(message:="    Address="+btCon_out.Address);
  DebugFmt(message:="    Error=\1", v1:=btCon_out.Errorcode);
END_IF;
 
IF btCon_in.changed THEN
  DebugMsg(message:="Connection info changed:");
  DebugFmt(message:="    Connected=\1",v1:=INT(btCon_in.connected));
  DebugMsg(message:="    Address="+btCon_in.Address);
  DebugFmt(message:="    Error=\1", v1:=btCon_in.Errorcode);
END_IF;
 
IF btRX_out.ready THEN
  DebugMsg(message:="Data Received from outgoing connection");
END_IF;
 
IF btRX_in.ready THEN
  DebugMsg(message:="Data Received from incoming connection");
END_IF;
 
  ...
 
END;
 
END_PROGRAM;