bleConnect (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

LX2

Firmware version:

2.25.00


 

This function makes the Bluetooth interface start connecting to a peripheral device.

 

The default parameters should work in most cases, so it is normally only necessary to provide the mac and dev parameters.

 

Once this function has returned successfully, the function bleDeviceStatus can be used to monitor the actual status of the peripheral device.

 

For advanced usage, some of the other connection parameters can be adjusted.

 

Scan interval and window

These parameters determine how it searches for the device to connect to, if they are set to the same value, it will search continuously.

 

Connection interval

This is how often the central asks the connected peripheral for data (connection event), which determines how often data can be transferred. A longer connection interval can reduce the power consumption of the peripheral but increases the latency and reduces the throughput.

 

Latency

The latency determines how many connection events the peripheral is allowed to skip before it must respond.

This enables the peripheral to sleep through events if it does not have anything to send, reducing the power consumption.

 

Supervision timeout

The supervision timeout specifies how much time may pass without receiving a packet before considering the connection lost.

Must be larger than (1 + latency_max) * con_interval_max * 2.

 

Connection length

The connection length is used to indicate how long the connection must be, to plan the time allocation for multiple connections.

 

Note: Scanning must be stopped before it is possible to connect.

 

 

Input:

mac: STRING

The MAC address of the device to connect to.

 

scan_interval: UINT (4..16384) (default 16384, 10.24ms)

The time interval from from one scan is started until the next is started.

The time is provided in ticks of 0.625 ms. This gives a range from 2.5ms to 10.24 s.

 

scan_window: UINT (4..16384) (default 16384, 10.24ms)

The duration to scan for. Must be less than or equal to the scan interval.

The time is provided in ticks of 0.625 ms. This gives a range from 2.5ms to 10.24 s.

 

con_interval_min: UINT (6..3200) (default 24, 30ms)

The minimum connection interval.

The time is provided in ticks of 1.25 ms. This gives a range from 7.5ms to 4 s.

 

con_interval_max: UINT (6..3200) (default 40, 50ms)

The maximum connection interval.

The time is provided in ticks of 1.25 ms. This gives a range from 7.5ms to 4 s.

 

latency_max: UINT (0..499) (default 0)

The maximum latency for the connection.

If set to 0, the peripheral must respond to every event.

 

supervision_timeout: UINT (10..3200) (default 3200)

If more than this time has elapsed since the most recent packet, the connection will be terminated.

The value will be multiplied by 10 ms. This gives a range from 100ms to 32s.

 

con_length_min: UINT (0..65535) (default 16, 10ms)

The minimum length of the connection needed.

The time is provided in ticks of 0.625 ms. This gives a range from 0ms to 40.96 s.

 

con_length_max: UINT (0..65535) (default 16, 10ms)

The maximum length of the connection needed.

The time is provided in ticks of 0.625 ms. This gives a range from 0ms to 40.96 s.

 

 

Output:

dev : SYSHANDLE

A handle to the device.

 

 

Returns: INT

1

-

_BLE_OK


Success. Make sure to call bleDisconnect when done with the connection.

0

-

_BLE_ERR_NOT_SUPPORTED


The API is not supported.

-1

-

_BLE_ERR_NOT_OPEN


The interface is not powered(see blePower).

-7

 

_BLE_ERR_NO_RES


All connections are in use.

-8

-

_BLE_ERR_INVAL


Invalid parameter.

-13

-

_BLE_ERR_STATE


Can not connect at this time, try again.

-99

-

_BLE_ERR_GENERIC


Failed to start connection

 

 

Declaration:

FUNCTION bleConnect : INT;
VAR_INPUT
  mac                 : STRING;
  scan_interval       : UINT  := 16#4000;
  scan_window         : UINT  := 16#4000;
  con_interval_min    : UINT  := 16#0018;
  con_interval_max    : UINT  := 16#0028;
  latency_max         : UINT  := 0;
  supervision_timeout : UINT  := 16#0C80;
  con_length_min      : UINT  := 16;
  con_length_max      : UINT  := 16;
  dev                 : ACCESS SYSHANDLE;
END_VAR;

 

Example:

...
  rc := bleConnect(dev := dev, mac := bleMacCreate(mac:="E3:1C:E6:43:F5:2D", type:=1));
  DebugFmt(message:="bleConnect: \1", v1:=rc);
...