Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTGattChar.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26package org.direct_bt;
27
28import java.util.List;
29
30/**
31 * Representing a Gatt Characteristic object from the GATT client perspective.
32 *
33 * A list of shared BTGattChar instances is available from BTGattService
34 * via BTGattService::getChars().
35 *
36 * See [Direct-BT Overview](namespaceorg_1_1direct__bt.html#details).
37 *
38 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3 Characteristic Definition
39 *
40 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.6.1 Discover All Characteristics of a Service
41 *
42 * The handle represents a service's characteristics-declaration
43 * and the value the Characteristics Property, Characteristics Value Handle _and_ Characteristics UUID.
44 *
45 * See {@link DBGattChar.UUID16} for selected standard GATT characteristic numbers in UUID16 format
46 * and {@link BTUtils#toUUID128(String)} for their conversion to UUID128.
47 */
48public interface BTGattChar extends BTObject
49{
50 /**
51 * Find a {@link BTGattDesc} by its desc_uuid.
52 *
53 * @parameter desc_uuid the UUID of the desired {@link BTGattDesc}
54 * @return The matching descriptor or null if not found
55 */
56 BTGattDesc findGattDesc(final String desc_uuid);
57
58 /**
59 * Return the Client Characteristic Configuration BTGattDesc if available or null.
60 *
61 * The {@link BTGattDesc#UUID128#CCC_DESC} has been indexed while
62 * retrieving the GATT database from the server.
63 */
65
66 /**
67 * Return the User Description BTGattDesc if available or null.
68 *
69 * The {@link BTGattDesc#UUID128#USER_DESC} has been indexed while
70 * retrieving the GATT database from the server.
71 */
73
74 /**
75 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
76 *
77 * Method enables notification and/or indication for this characteristic at BLE level.
78 *
79 * Implementation masks this Characteristic properties PropertyBitVal::Notify and PropertyBitVal::Indicate
80 * with the respective user request parameters, hence removes unsupported requests.
81 *
82 * Notification and/or indication configuration is only performed per characteristic if changed.
83 *
84 * It is recommended to utilize notification over indication, as its link-layer handshake
85 * and higher potential bandwidth may deliver material higher performance.
86 *
87 * @param enableNotification
88 * @param enableIndication
89 * @param enabledState array of size 2, holding the resulting enabled state for notification and indication.
90 * @return false if this characteristic has no PropertyBitVal::Notify or PropertyBitVal::Indication present,
91 * or there is no GATTDescriptor of type ClientCharacteristicConfiguration, or if the operation has failed.
92 * Otherwise returns true.
93 *
94 * @throws IllegalStateException if notification or indication is set to be enabled
95 * and the {@link BTDevice}'s GATTHandler is null, i.e. not connected
96 *
97 * @see #disableIndicationNotification()
98 * @see #enableNotificationOrIndication(boolean[])
99 * @see #configNotificationIndication(boolean, boolean, boolean[])
100 * @see #addCharListener(BTGattCharListener)
101 * #see #addCharListener(BTGattCharListener, boolean[])
102 * @see #removeCharListener(BTGattCharListener)
103 * @see #removeAllAssociatedCharListener(boolean)
104 * @since 2.0.0
105 */
106 boolean configNotificationIndication(final boolean enableNotification, final boolean enableIndication, final boolean enabledState[/*2*/])
107 throws IllegalStateException;
108
109 /**
110 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
111 *
112 * Method will attempt to enable notification on the BLE level, if available,
113 * otherwise indication if available.
114 *
115 * Notification and/or indication configuration is only performed per characteristic if changed.
116 *
117 * It is recommended to utilize notification over indication, as its link-layer handshake
118 * and higher potential bandwidth may deliver material higher performance.
119 *
120 * @param enabledState array of size 2, holding the resulting enabled state for notification and indication.
121 * @return false if this characteristic has no PropertyBitVal::Notify or PropertyBitVal::Indication present,
122 * or there is no GATTDescriptor of type ClientCharacteristicConfiguration, or if the operation has failed.
123 * Otherwise returns true.
124 *
125 * @throws IllegalStateException if notification or indication is set to be enabled
126 * and the {@link BTDevice}'s GATTHandler is null, i.e. not connected
127 *
128 * @see #disableIndicationNotification()
129 * @see #enableNotificationOrIndication(boolean[])
130 * @see #configNotificationIndication(boolean, boolean, boolean[])
131 * @see #addCharListener(BTGattCharListener)
132 * #see #addCharListener(BTGattCharListener, boolean[])
133 * @see #removeCharListener(BTGattCharListener)
134 * @see #removeAllAssociatedCharListener(boolean)
135 * @since 2.0.0
136 */
137 boolean enableNotificationOrIndication(final boolean enabledState[/*2*/])
138 throws IllegalStateException;
139
140 /**
141 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
142 *
143 * Method will attempt to disable notification and indication on the BLE level.
144 *
145 * Notification and/or indication configuration is only performed per characteristic if changed.
146 *
147 * @return false if this characteristic has no PropertyBitVal::Notify or PropertyBitVal::Indication present,
148 * or there is no BTGattDesc of type ClientCharacteristicConfiguration, or if the operation has failed.
149 * Otherwise returns true.
150 *
151 * @throws IllegalStateException if notification or indication is set to be enabled
152 * and the {@link BTDevice}'s GATTHandler is null, i.e. not connected
153 *
154 * @see #disableIndicationNotification()
155 * @see #enableNotificationOrIndication(boolean[])
156 * @see #configNotificationIndication(boolean, boolean, boolean[])
157 * @see #addCharListener(BTGattCharListener)
158 * #see #addCharListener(BTGattCharListener, boolean[])
159 * @see #removeCharListener(BTGattCharListener)
160 * @see #removeAllAssociatedCharListener(boolean)
161 * @since 2.4.0
162 */
163 boolean disableIndicationNotification() throws IllegalStateException;
164
165 /**
166 * Add the given BTGattCharListener to the listener list if not already present.
167 *
168 * Occurring notifications and indications for this characteristic,
169 * if enabled via {@link #configNotificationIndication(boolean, boolean, boolean[])}
170 * or {@link #enableNotificationOrIndication(boolean[])},
171 * will call the respective BTGattCharListener callback method.
172 *
173 * Returns true if the given listener is not element of the list and has been newly added,
174 * otherwise false.
175 *
176 * @param listener A {@link {@link BTGattCharListener}} instance, listening to this {@link BTGattChar}'s events
177 * @return if successful, true is being returned, otherwise false.
178 * @throws IllegalStateException if the DBTDevice's GATTHandler is null, i.e. not connected
179 * @see #disableIndicationNotification()
180 * @see #enableNotificationOrIndication(boolean[])
181 * @see #configNotificationIndication(boolean, boolean, boolean[])
182 * @see #addCharListener(BTGattCharListener)
183 * #see #addCharListener(BTGattCharListener, boolean[])
184 * @see #removeCharListener(BTGattCharListener)
185 * @see #removeAllAssociatedCharListener(boolean)
186 * @since 2.4.0
187 */
188 boolean addCharListener(final BTGattCharListener listener) throws IllegalStateException;
189
190 /**
191 * Add the given BTGattCharListener to the listener list if not already present
192 * and if enabling the notification <i>or</i> indication for this characteristic at BLE level was successful.<br>
193 * Notification and/or indication configuration is only performed per characteristic if changed.
194 *
195 * Implementation will enable notification if available,
196 * otherwise indication will be enabled if available. <br>
197 * Implementation uses {@link #enableNotificationOrIndication(boolean[])} to enable either.
198 *
199 * Occurring notifications and indications for this characteristic
200 * will call the respective BTGattCharListener callback method.
201 *
202 * Returns true if enabling the notification and/or indication was successful
203 * and if the given listener is not element of the list and has been newly added,
204 * otherwise false.
205 *
206 * @param listener A {@link BTGattChar.Listener} instance, listening to this {@link BTGattChar}'s events
207 * @param enabledState array of size 2, holding the resulting enabled state for notification and indication
208 * using {@link #enableNotificationOrIndication(boolean[])}
209 * @return if successful, true is being returned, otherwise false.
210 * @throws IllegalStateException if the {@link BTDevice}'s GATTHandler is null, i.e. not connected
211 * @throws IllegalStateException if the given {@link BTGattChar.Listener} is already in use, i.e. added.
212 * @see #disableIndicationNotification()
213 * @see #enableNotificationOrIndication(boolean[])
214 * @see #configNotificationIndication(boolean, boolean, boolean[])
215 * @see #addCharListener(BTGattCharListener)
216 * #see #addCharListener(BTGattCharListener, boolean[])
217 * @see #removeCharListener(BTGattCharListener)
218 * @see #removeAllAssociatedCharListener(boolean)
219 * @since 2.4.0
220 */
221 boolean addCharListener(final BTGattCharListener listener, final boolean enabledState[/*2*/])
222 throws IllegalStateException;
223
224 /**
225 * Remove the given associated {@link BTGattCharListener} from the listener list if present.
226 *
227 * To disables the notification and/or indication for this characteristic at BLE level
228 * use {@link #disableIndicationNotification()} when desired.
229 *
230 * @param listener returned {@link BTGattCharListener} from {@link #addCharListener(Listener)} ...
231 * @return true if successful, otherwise false.
232 *
233 * @see #disableIndicationNotification()
234 * @see #enableNotificationOrIndication(boolean[])
235 * @see #configNotificationIndication(boolean, boolean, boolean[])
236 * @see #addCharListener(BTGattCharListener)
237 * #see #addCharListener(BTGattCharListener, boolean[])
238 * @see #removeCharListener(BTGattCharListener)
239 * @see #removeAllAssociatedCharListener(boolean)
240 * @since 2.4.0
241 */
242 boolean removeCharListener(final BTGattCharListener listener);
243
244 /**
245 * Disables the notification and/or indication for this characteristic BLE level
246 * if {@code disableIndicationNotification == true}
247 * and removes all associated {@link BTGattChar.Listener} or {@link BTGattCharListener} from the listener list,
248 * which are associated with this characteristic instance.
249 * <p>
250 * If the DBTDevice's GATTHandler is null, i.e. not connected, {@code false} is being returned.
251 * </p>
252 *
253 * @param shallDisableIndicationNotification if true, disables the notification and/or indication for this characteristic
254 * using {@link #disableIndicationNotification()}
255 * @return number of removed listener.
256 *
257 * @see #disableIndicationNotification()
258 * @see #enableNotificationOrIndication(boolean[])
259 * @see #configNotificationIndication(boolean, boolean, boolean[])
260 * @see #addCharListener(BTGattCharListener)
261 * #see #addCharListener(BTGattCharListener, boolean[])
262 * @see #removeCharListener(BTGattCharListener)
263 * @see #removeAllAssociatedCharListener(boolean)
264 * @since 2.0.0
265 */
266 int removeAllAssociatedCharListener(final boolean shallDisableIndicationNotification);
267
268 /** Get the UUID of this characteristic.
269 * @return The 128 byte UUID of this characteristic, NULL if an error occurred
270 */
271 String getUUID();
272
273 /** Returns the service to which this characteristic belongs to.
274 * @return The service.
275 */
277
278 /** Returns true if notification for changes of this characteristic are activated.
279 * @param enabledState array of size 2, storage for the current enabled state for notification and indication.
280 * @return True if either notification or indication is enabled
281 */
282 boolean getNotifying(final boolean enabledState[/*2*/]);
283
284 /**
285 * Returns the properties of this characteristic.
286 * <p>
287 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.1.1 Characteristic Properties
288 * </p>
289 */
291
292 /** Returns a list of BluetoothGattDescriptors this characteristic exposes.
293 * @return A list of BluetoothGattDescriptors exposed by this characteristic
294 * NULL if an error occurred
295 */
297
298 /** Reads the value of this characteristic.
299 * @return A std::vector<unsgined char> containing the value of this characteristic.
300 */
301 byte[] readValue() throws BTException;
302
303 /**
304 * Writes the value of this characteristic,
305 * using one of the following methods depending on {@code withResponse}
306 * <pre>
307 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value
308 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response
309 * </pre>
310 * @param[in] arg_value The data as vector<uchar>
311 * to be written packed in a GBytes struct
312 * @param withResponse if {@code true} a subsequent ATT_WRITE_RSP is expected, otherwise not.
313 * @return TRUE if value was written successfully
314 * @since 2.0.0
315 * @implNote {@code withResponse} parameter has been added since 2.0.0
316 */
317 boolean writeValue(byte[] argValue, boolean withResponse) throws BTException;
318
319 @Override
320 String toString();
321}
BTGattChar event listener for notification and indication events.
Bit mask of GATT Characteristic Properties.
Representing a Gatt Characteristic object from the GATT client perspective.
Definition: BTGattChar.java:49
String getUUID()
Get the UUID of this characteristic.
BTGattService getService()
Returns the service to which this characteristic belongs to.
boolean getNotifying(final boolean enabledState[])
Returns true if notification for changes of this characteristic are activated.
BTGattDesc getUserDescription()
Return the User Description BTGattDesc if available or null.
boolean writeValue(byte[] argValue, boolean withResponse)
Writes the value of this characteristic, using one of the following methods depending on withRespons...
boolean removeCharListener(final BTGattCharListener listener)
Remove the given associated BTGattCharListener from the listener list if present.
List< BTGattDesc > getDescriptors()
Returns a list of BluetoothGattDescriptors this characteristic exposes.
int removeAllAssociatedCharListener(final boolean shallDisableIndicationNotification)
Disables the notification and/or indication for this characteristic BLE level if disableIndicationNot...
boolean enableNotificationOrIndication(final boolean enabledState[])
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
boolean configNotificationIndication(final boolean enableNotification, final boolean enableIndication, final boolean enabledState[])
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
BTGattDesc findGattDesc(final String desc_uuid)
Find a BTGattDesc by its desc_uuid.
boolean addCharListener(final BTGattCharListener listener)
Add the given BTGattCharListener to the listener list if not already present.
BTGattDesc getClientCharConfig()
Return the Client Characteristic Configuration BTGattDesc if available or null.
boolean disableIndicationNotification()
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
GattCharPropertySet getProperties()
Returns the properties of this characteristic.
byte[] readValue()
Reads the value of this characteristic.
Representing a Gatt Characteristic Descriptor object from the GATT client perspective.
Definition: BTGattDesc.java:42
Representing a Gatt Service object from the GATT client perspective.