The btDeviceInfo function block is used to read information about a remote device.
Input:
dev : STRING
The address of the device to get information about, retrieved from either btDeviceGet or from the _BT_EVENT_DEV_FOUND event on btWaitEvent.
Output:
Status : INT
The status of the function:
1
|
-
|
_BT_OK
|
|
The information has been retrieved and is valid.
|
0
|
-
|
_BT_ERR_NOT_SUPPORTED
|
|
The API is not supported.
|
-1
|
-
|
_BT_ERR_NOT_OPEN
|
|
The adapter is not powered(see btPower).
|
-4
|
-
|
_BT_ERR_NOT_FOUND
|
|
Could not find device.
|
-5
|
-
|
_BT_ERR_NO_ADAP
|
|
Could not find adapter.
|
name : STRING
The name of the device.
paired : BOOL
TRUE if the device is paired. Note that if the pairing has been removed on the remote device, this will still report TRUE, as it reflects the local status.
trusted : BOOL
TRUE if the device is trusted (this currently happens automatically when it is paired).
clss : DINT
The Class Of Device(COD) of the device.
addr_type : SINT
The type of address. 0 =Classic Bluetooth, 1 = BLE Public address, 2 = BLE Random address.
connected : BOOL
TRUE if the device is currently connected.
tx_power : SINT
The transmit power advertised by the device in dB.
RSSI : INT
The signal strength of the device in dB. This value is only valid while scanning, otherwise it is set to 0.
profiles : INT
The number of supported profiles reported by the device.
UUID[n] : STRING
The UUID of the supported profiles.
The official UUIDs can be found in the Bluetooth specification (https://www.bluetooth.com/specifications/assigned-numbers/service-discovery).
Declaration:
FUNCTION_BLOCK btDeviceInfo;
VAR_INPUT
dev : STRING;
END_VAR;
VAR_OUTPUT
status : INT;
name : STRING;
paired : BOOL;
trusted : BOOL;
clss : DINT;
addr_type: SINT;
connected: BOOL;
tx_power : SINT;
RSSI : INT;
profiles : INT;
UUID : ARRAY [1..32] OF STRING;
END_VAR;
Example:
INCLUDE rtcu.inc
PROGRAM test;
VAR
rc : INT;
idx : INT;
address : STRING;
devInfo : btDeviceInfo;
i : INT;
END_VAR;
rc := btPower();
DebugFmt(message:="btPower: \1 ", v1:=rc);
BEGIN
IF btIsScanning() <> 1 THEN
rc := _BT_OK;
idx:=1;
WHILE rc >= _BT_OK DO
rc := btDeviceGet(idx:=idx, dev := address);
IF rc >= _BT_OK THEN
DebugFmt(message:="Address(\2): \1 , " + address, v1:=rc, v2:=idx);
devInfo(dev := address);
DebugFmt(message:=" " + devInfo.name+" , Class: \4, type: \3, connected: \1, tx power: \2",
v2:=devInfo.tx_power, v4:=devInfo.clss, v3:=devInfo.addr_type, v1:=SINT(devInfo.connected));
DebugFmt(message:=" status: \1, paired: \2, trusted: \3", v1:=devInfo.status,
v2:=INT(devInfo.paired), v3:=INT(devInfo.trusted));
DebugFmt(message:=" Profiles: \1", v1:=devInfo.profiles);
FOR i:= 1 TO 16 DO
IF devInfo.uuid[i] <> "" THEN
DebugFmt(message:=" [\1]: " + devInfo.uuid[i], v1:=i);
END_IF;
END_FOR;
END_IF;
idx := idx+1;
END_WHILE;
DebugFmt(message:="btDeviceGet: \1 ", v1:=rc);
rc := btScanStart(timeout := 5, transport := 1);
DebugFmt(message:="btScanStart: \1 ", v1:=rc);
ELSE
Sleep(delay:=100);
END_IF;
END;
END_PROGRAM;
|