The btSetPin function is used to set the PIN for incoming connections (btListen). Once set, there is no need to update the PIN unless to change it to another. When an outgoing connection is initiated with btConnect, the PIN for the module is temporarily changed to the one provide by btConnect. When this connection has been processed, the PIN will automatically be returned.
Input:
Pin : STRING
The PIN code. Maximum 16 characters.
Return: INT
0
|
- Success.
|
-1
|
- Bluetooth library is not open.
|
-11
|
- Invalid PIN code.
|
Declaration:
FUNCTION btSetPin : INT;
VAR_INPUT
Pin : STRING;
END_VAR;
Example:
INCLUDE rtcu.inc
PROGRAM test;
VAR
rc : INT;
SignalLevel : SINT;
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;
btOpen(name := "RTCU MX2");
btSetPin(pin := "1234");
ConID_out:=btConnect(address:="00:e0:98:ae:17:23",pin:="0000");
DebugFmt(message:="Outgoing Connection ID =\1", v1:=ConID_out);
ConID_in:=btListen();
DebugFmt(message:="Incoming Connection ID =\1", v1:=ConID_in);
btRX_out.Data:=ADDR(RXbuffer_out);
btRX_out.MaxSize:=SIZEOF(RXbuffer_out);
btRX_out.id:=ConID_out;
btCon_out.id:=ConID_out;
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;
|