Examples - BLE Scanner (LX)

Top  Previous  Next

//-----------------------------------------------------------------------------
// ex_scan.vpl
// This example starts scanning for BLE advertising and prints out all
// advertising data that contains the Complete Local Name.
//
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
// Uncomment math.inc to add math library support.
//INCLUDE math.inc
 
//  Input variables that can be configured via the configuration dialog (These are global)
VAR_INPUT
 
END_VAR;
 
//  Output variables that can be configured via the configuration dialog (These are global)
VAR_OUTPUT
 
END_VAR;
 
//  These are the global variables of the program
VAR

 
END_VAR;
 
// Callback for showing the name of the device
FUNCTION CALLBACK cbAdvName;
VAR_INPUT
  mac      : STRING;
  ev_type  : UINT;
  adv_type : USINT;
  data     : PTR;
  len      : INT;
  rssi     : SINT;
  arg      : DINT;
END_VAR;
VAR
  str      : STRING;
END_VAR;
  str:=strFromMemory(src:=data, len:=len);
  // Show MAC, name and RSSI
  DebugFmt(message:=mac+" (\1): "+str, v1:=rssi);
END_FUNCTION;
 
 
 
PROGRAM ex_scan;
// These are the local variables of the program block
VAR
  rc : INT;
END_VAR;
// The next code will only be executed once after the program starts
  // Enable BT
  rc := blePower(power:=ON);
  DebugFmt(message:="blePower: \1", v1:=rc);
 
  // Register calblack for advertising data type 16#09, Complete Local Name
  rc := bleRegisterAdvData(cb_Adv:=@cbAdvName, adv_type := 16#09);
  DebugFmt(message:="BleRegisterAdvData: \1", v1:=rc);
 
  // Start listening
  rc := bleObserverStart(
    scan_type:=1,// Active
    filter_policy := 0//Accept all
     );
  DebugFmt(message:="bleObserverStart: \1", v1:=rc);
BEGIN
  // Code from this point until END will be executed repeatedly
  Sleep(delay:=10000);
 
END;
 
END_PROGRAM;