2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.bluegiga.internal.command.gap;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.bluetooth.bluegiga.internal.BlueGigaCommand;
17 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.BluetoothAddressType;
20 * Class to implement the BlueGiga command <b>connectDirect</b>.
22 * This command will start the GAP direct connection establishment procedure to a dedicated
23 * Smart Bluetooth device. The module will enter a state where it continuously scans for the
24 * connectable advertisement packets Bluetooth from the remote device which matches the
25 * Bluetooth address gives as a parameter. Upon receiving the advertisement packet, the
26 * module will send a connection request packet to the target device to imitate a Bluetooth
27 * connection. A successful connection will be indicated by an event. Status If the device is
28 * configured to support more than one connection, the smallest connection interval which is
29 * divisible by maximum_connections * 2.5ms will be selected. Thus, it is important to provide
30 * minimum and maximum connection intervals so that such a connection interval is available
31 * within the range. The connection establishment procedure can be cancelled with End
34 * This class provides methods for processing BlueGiga API commands.
36 * Note that this code is autogenerated. Manual changes may be overwritten.
38 * @author Chris Jackson - Initial contribution of Java code generator
39 * @author Pauli Anttila - Added message builder
42 public class BlueGigaConnectDirectCommand extends BlueGigaCommand {
43 public static int COMMAND_CLASS = 0x06;
44 public static int COMMAND_METHOD = 0x03;
46 private BlueGigaConnectDirectCommand(CommandBuilder builder) {
47 this.address = builder.address;
48 this.addrType = builder.addrType;
49 this.connIntervalMin = builder.connIntervalMin;
50 this.connIntervalMax = builder.connIntervalMax;
51 this.timeout = builder.timeout;
52 this.latency = builder.latency;
56 * Bluetooth address of the target device.
58 * BlueGiga API type is <i>bd_addr</i> - Java type is {@link String}
60 private String address;
63 * see: Bluetooth Address Types.
65 * BlueGiga API type is <i>BluetoothAddressType</i> - Java type is {@link BluetoothAddressType}
67 private BluetoothAddressType addrType;
70 * Minimum Connection Interval (in units of 1.25ms). Range: 6 - 3200 The lowest possible
71 * Connection Interval is 7.50ms and the largest is 4000ms.
73 * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
75 private int connIntervalMin;
78 * Maximum Connection Interval (in units of 1.25ms). Range: 6 - 3200 Must be equal or bigger than
79 * minimum Connection Interval.
81 * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
83 private int connIntervalMax;
86 * Supervision Timeout (in units of 10ms). The Supervision Timeout defines how long the
87 * devices can be out of range before the connection is closed. Range: 10 - 3200 Minimum time for
88 * the Supervision Timeout is 100ms and maximum value is 32000ms. According to the
89 * specification, the Supervision Timeout in milliseconds shall be larger than (1 + latency) *
90 * conn_interval_max * 2, where conn_interval_max is given in milliseconds.
92 * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
97 * This parameter configures the slave latency. Slave latency defines how many connection
98 * intervals a slave device can skip. Increasing slave latency will decrease the energy
99 * consumption of the slave in scenarios where slave does not have data to send at every
100 * connection interval. Range: 0 - 500 0 : Slave latency is disabled. Example: Connection
101 * interval is 10ms and slave latency is 9: this means that the slave is allowed to communicate
102 * every 100ms, but it can communicate every 10ms if needed.
104 * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
109 public int[] serialize() {
110 // Serialize the header
111 serializeHeader(COMMAND_CLASS, COMMAND_METHOD);
113 // Serialize the fields
114 serializeAddress(address);
115 serializeBluetoothAddressType(addrType);
116 serializeUInt16(connIntervalMin);
117 serializeUInt16(connIntervalMax);
118 serializeUInt16(timeout);
119 serializeUInt16(latency);
125 public String toString() {
126 final StringBuilder builder = new StringBuilder();
127 builder.append("BlueGigaConnectDirectCommand [address=");
128 builder.append(address);
129 builder.append(", addrType=");
130 builder.append(addrType);
131 builder.append(", connIntervalMin=");
132 builder.append(connIntervalMin);
133 builder.append(", connIntervalMax=");
134 builder.append(connIntervalMax);
135 builder.append(", timeout=");
136 builder.append(timeout);
137 builder.append(", latency=");
138 builder.append(latency);
140 return builder.toString();
143 public static class CommandBuilder {
144 private String address = "";
145 private BluetoothAddressType addrType = BluetoothAddressType.UNKNOWN;
146 private int connIntervalMin;
147 private int connIntervalMax;
152 * Bluetooth address of the target device.
154 * @param address the address to set as {@link String}
156 public CommandBuilder withAddress(String address) {
157 this.address = address;
162 * see: Bluetooth Address Types.
164 * @param addrType the addrType to set as {@link BluetoothAddressType}
166 public CommandBuilder withAddrType(BluetoothAddressType addrType) {
167 this.addrType = addrType;
172 * Minimum Connection Interval (in units of 1.25ms). Range: 6 - 3200 The lowest possible
173 * Connection Interval is 7.50ms and the largest is 4000ms.
175 * @param connIntervalMin the connIntervalMin to set as {@link int}
177 public CommandBuilder withConnIntervalMin(int connIntervalMin) {
178 this.connIntervalMin = connIntervalMin;
183 * Maximum Connection Interval (in units of 1.25ms). Range: 6 - 3200 Must be equal or bigger than
184 * minimum Connection Interval.
186 * @param connIntervalMax the connIntervalMax to set as {@link int}
188 public CommandBuilder withConnIntervalMax(int connIntervalMax) {
189 this.connIntervalMax = connIntervalMax;
194 * Supervision Timeout (in units of 10ms). The Supervision Timeout defines how long the
195 * devices can be out of range before the connection is closed. Range: 10 - 3200 Minimum time for
196 * the Supervision Timeout is 100ms and maximum value is 32000ms. According to the
197 * specification, the Supervision Timeout in milliseconds shall be larger than (1 + latency) *
198 * conn_interval_max * 2, where conn_interval_max is given in milliseconds.
200 * @param timeout the timeout to set as {@link int}
202 public CommandBuilder withTimeout(int timeout) {
203 this.timeout = timeout;
208 * This parameter configures the slave latency. Slave latency defines how many connection
209 * intervals a slave device can skip. Increasing slave latency will decrease the energy
210 * consumption of the slave in scenarios where slave does not have data to send at every
211 * connection interval. Range: 0 - 500 0 : Slave latency is disabled. Example: Connection
212 * interval is 10ms and slave latency is 9: this means that the slave is allowed to communicate
213 * every 100ms, but it can communicate every 10ms if needed.
215 * @param latency the latency to set as {@link int}
217 public CommandBuilder withLatency(int latency) {
218 this.latency = latency;
222 public BlueGigaConnectDirectCommand build() {
223 return new BlueGigaConnectDirectCommand(this);