btDeviceGet (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

NX-200, NX-400, NX-900

Firmware version:

1.10.00

 


 

This function will retrieve the MAC address of a known remote Bluetooth device.

This will include both already known devices such as paired devices and connected BLE devices, as well as new devices found during scanning (see btScanStart).

The known devices are kept across reset, until btDeviceRemove is used to remove them.

To list all the known devices, keep calling this function with incrementing index until it returns -4 (_BT_ERR_NOT_FOUND).

Note: Scanning must be stopped for this function to work.

 

Input:

idx: INT (1..150) (default 1)

The index of the device to get the address of.

 

Output:

dev : STRING

The address of the device.

 

Returns: INT

1

-

_BT_OK


Success

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


No device is available at this or higher indexes.

-5

-

_BT_ERR_NO_ADAP


Could not find adapter.

 

 

Declaration:

FUNCTION btDeviceGet : INT;
VAR_INPUT
  idx      : INT := 1;
  address  : ACCESS STRING;
END_VAR;

 

Example:

 
INCLUDE rtcu.inc
 
PROGRAM test;
// These are the local variables of the program block
VAR
  rc      : INT;
  idx     : INT;
  address : STRING;
  devInfo : btDeviceInfo;
  i       : INT;
END_VAR;
 
  rc := btPower();
  DebugFmt(message:="btPower: \1 ", v1:=rc);
 
BEGIN
  // If not scanning; List the found devices and start a new scan
  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;