btScanStart (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

NX-200, NX-400, NX-900

Firmware version:

1.10.00

 


 

This function makes the adapter start scanning for other devices.

It will find all the paired devices as well as the devices that match the provided parameters.

It is also used to receive BLE advertisement data.

 

Input:

transport : SINT (0..2) (default 0)

The type of device to search for:

 0        Any kind

 1        Classic Bluetooth.

 2        BLE.

 

UUID : STRING

Scan for a specific profile that has this UUID. Note that some devices may not have all profiles available to unpaired and unconnected devices.

The official UUIDs can be found in the Bluetooth specification (https://www.bluetooth.com/specifications/assigned-numbers/service-discovery).

 

RSSI : INT (default 0)

The required signal strength in dB.

 

pathloss: INT (default 0)

The device must advertise TX Power and have a computed pathloss less than this value.

 

timeout : DINT (default 0)

The timeout in seconds before stopping the scan automatically. If set to 0, it will keep scanning until btScanStop is called.

 

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).

-5

-

_BT_ERR_NO_ADAP


Could not find adapter.

-12

-

_BT_ERR_BUSY


Adapter is already scanning.

-17

-

_BT_ERR_TIMEOUT


Failed to start scan withing the expected time.

 

 

Declaration:

FUNCTION btScanStart : INT;
VAR_INPUT
  transport   : SINT   := 0;
  UUID        : STRING := "";
  RSSI        : INT    := 0;
  pathloss    : INT    := 0;
  timeout     : DINT   := 0;
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;