Direct-BT  2.3.1
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  * Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
7  * Copyright (c) 2016 Intel Corporation.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 package org.direct_bt;
30 
31 import java.util.List;
32 
33 /**
34  * Provides access to Bluetooth GATT characteristic.
35  *
36  * @see [Bluetooth Specification](https://www.bluetooth.com/specifications/bluetooth-core-specification/)
37  * @see [BlueZ GATT API](http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/gatt-api.txt)
38  */
39 public interface BTGattChar extends BTObject
40 {
41  /**
42  * {@link BTGattChar} event listener for notification and indication events.
43  * <p>
44  * This listener instance is attached to a {@link BTGattChar} via
45  * {@link BTGattChar#addCharListener(Listener)} or {@link BTGattChar#addCharListener(Listener, boolean[])}
46  * to listen to events associated with the {@link BTGattChar} instance.
47  * </p>
48  * <p>
49  * The listener manager maintains a unique set of listener instances without duplicates.
50  * </p>
51  * <p>
52  * Implementation will utilize a {@link BTGattCharListener) for the listener manager,
53  * delegating matching {@link BTGattChar} events to this instance.
54  * </p>
55  */
56  static public interface Listener {
57  /**
58  * Called from native BLE stack, initiated by a received notification associated
59  * with the given {@link BTGattChar}.
60  * @param charDecl {@link BTGattChar} related to this notification
61  * @param value the notification value
62  * @param timestamp the indication monotonic timestamp, see {@link BTUtils#currentTimeMillis()}
63  */
64  public void notificationReceived(final BTGattChar charDecl,
65  final byte[] value, final long timestamp);
66 
67  /**
68  * Called from native BLE stack, initiated by a received indication associated
69  * with the given {@link BTGattChar}.
70  * @param charDecl {@link BTGattChar} related to this indication
71  * @param value the indication value
72  * @param timestamp the indication monotonic timestamp, see {@link BTUtils#currentTimeMillis()}
73  * @param confirmationSent if true, the native stack has sent the confirmation, otherwise user is required to do so.
74  */
75  public void indicationReceived(final BTGattChar charDecl,
76  final byte[] value, final long timestamp,
77  final boolean confirmationSent);
78  };
79 
80  /** Find a BluetoothGattDescriptor. If parameter UUID is not null,
81  * the returned object will have to match it.
82  * It will first check for existing objects. It will not turn on discovery
83  * or connect to devices.
84  * @parameter UUID optionally specify the UUID of the BluetoothGattDescriptor you are
85  * waiting for
86  * @parameter timeoutMS the function will return after timeout time in milliseconds, a
87  * value of zero means wait forever. If object is not found during this time null will be returned.
88  * @return An object matching the UUID or null if not found before
89  * timeout expires or event is canceled.
90  */
91  public BTGattDesc find(final String UUID, final long timeoutMS);
92 
93  /** Find a BluetoothGattDescriptor. If parameter UUID is not null,
94  * the returned object will have to match it.
95  * It will first check for existing objects. It will not turn on discovery
96  * or connect to devices.
97  * @parameter UUID optionally specify the UUID of the BluetoothGattDescriptor you are
98  * waiting for
99  * @return An object matching the UUID or null if not found before
100  * timeout expires or event is canceled.
101  */
102  public BTGattDesc find(final String UUID);
103 
104  /* D-Bus method calls: */
105 
106  /** Reads the value of this characteristic.
107  * @return A std::vector<unsgined char> containing the value of this characteristic.
108  */
109  public byte[] readValue() throws BTException;
110 
111  /**
112  * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
113  * <p>
114  * Method enables notification and/or indication for this characteristic at BLE level.
115  * </p>
116  * <p>
117  * Implementation masks this Characteristic properties PropertyBitVal::Notify and PropertyBitVal::Indicate
118  * with the respective user request parameters, hence removes unsupported requests.
119  * </p>
120  * <p>
121  * Notification and/or indication configuration is only performed per characteristic if changed.
122  * </p>
123  * <p>
124  * It is recommended to utilize notification over indication, as its link-layer handshake
125  * and higher potential bandwidth may deliver material higher performance.
126  * </p>
127  * @param enableNotification
128  * @param enableIndication
129  * @param enabledState array of size 2, holding the resulting enabled state for notification and indication.
130  * @return false if this characteristic has no PropertyBitVal::Notify or PropertyBitVal::Indication present,
131  * or there is no GATTDescriptor of type ClientCharacteristicConfiguration, or if the operation has failed.
132  * Otherwise returns true.
133  * @throws IllegalStateException if notification or indication is set to be enabled
134  * and the {@link BTDevice}'s GATTHandler is null, i.e. not connected
135  * @see #enableNotificationOrIndication(boolean[])
136  * @since 2.0.0
137  */
138  public boolean configNotificationIndication(final boolean enableNotification, final boolean enableIndication, final boolean enabledState[/*2*/])
139  throws IllegalStateException;
140 
141  /**
142  * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
143  * <p>
144  * Method will attempt to enable notification on the BLE level, if available,
145  * otherwise indication if available.
146  * </p>
147  * <p>
148  * Notification and/or indication configuration is only performed per characteristic if changed.
149  * </p>
150  * <p>
151  * It is recommended to utilize notification over indication, as its link-layer handshake
152  * and higher potential bandwidth may deliver material higher performance.
153  * </p>
154  * @param enabledState array of size 2, holding the resulting enabled state for notification and indication.
155  * @return false if this characteristic has no PropertyBitVal::Notify or PropertyBitVal::Indication present,
156  * or there is no GATTDescriptor of type ClientCharacteristicConfiguration, or if the operation has failed.
157  * Otherwise returns true.
158  * @throws IllegalStateException if notification or indication is set to be enabled
159  * and the {@link BTDevice}'s GATTHandler is null, i.e. not connected
160  * @see #configNotificationIndication(boolean, boolean, boolean[])
161  * @since 2.0.0
162  */
163  public boolean enableNotificationOrIndication(final boolean enabledState[/*2*/])
164  throws IllegalStateException;
165 
166  /**
167  * Add the given {@link BTGattChar.Listener} to the listener list if not already present.
168  * <p>
169  * Occurring notifications and indications for this characteristic,
170  * if enabled via {@link #configNotificationIndication(boolean, boolean, boolean[])}
171  * or {@link #enableNotificationOrIndication(boolean[])},
172  * will call the respective {@link BTGattChar.Listener} callback method.
173  * </p>
174  * <p>
175  * Implementation wraps given {@link BTGattChar.Listener} into a {@link BTGattCharListener}
176  * to restrict the listener to listen only to this BTGattChar instance.
177  * </p>
178  * @param listener A {@link BTGattChar.Listener} instance, listening to this {@link BTGattChar}'s events
179  * @return true if the given listener is not element of the list and has been newly added, otherwise false.
180  * @throws IllegalStateException if the DBTDevice's GATTHandler is null, i.e. not connected
181  * @throws IllegalStateException if the given {@link BTGattChar.Listener} is already in use, i.e. added.
182  * @see #enableNotificationOrIndication(boolean[])
183  * @see #configNotificationIndication(boolean, boolean, boolean[])
184  * @see #addCharListener(Listener, boolean[])
185  * @see #removeCharListener(Listener, boolean)
186  * @see #removeAllAssociatedCharListener(boolean)
187  * @since 2.0.0
188  */
189  public boolean addCharListener(final Listener listener)
190  throws IllegalStateException;
191 
192  /**
193  * Add the given {@link BTGattChar.Listener} to the listener list if not already present
194  * and if enabling the notification <i>or</i> indication for this characteristic at BLE level was successful.<br>
195  * Notification and/or indication configuration is only performed per characteristic if changed.
196  * <p>
197  * Implementation will enable notification if available,
198  * otherwise indication will be enabled if available. <br>
199  * Implementation uses {@link #enableNotificationOrIndication(boolean[])} to enable either.
200  * </p>
201  * <p>
202  * Occurring notifications and indications for this characteristic
203  * will call the respective {@link BTGattChar.Listener} callback method.
204  * </p>
205  * @param listener A {@link BTGattChar.Listener} instance, listening to this {@link BTGattChar}'s events
206  * @param enabledState array of size 2, holding the resulting enabled state for notification and indication
207  * using {@link #enableNotificationOrIndication(boolean[])}
208  * @return true if enabling the notification and/or indication was successful
209  * and if the given listener is not element of the list and has been newly added, 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 #enableNotificationOrIndication(boolean[])
213  * @see #configNotificationIndication(boolean, boolean, boolean[])
214  * @see #addCharListener(Listener)
215  * @see #removeCharListener(Listener, boolean)
216  * @see #removeAllAssociatedCharListener(boolean)
217  * @since 2.0.0
218  */
219  public boolean addCharListener(final Listener listener, final boolean enabledState[/*2*/])
220  throws IllegalStateException;
221 
222  /**
223  * Disables the notification and/or indication for this characteristic BLE level
224  * if {@code disableIndicationNotification == true}
225  * and removes all associated {@link BTGattChar.Listener} or {@link BTGattCharListener} from the listener list,
226  * which are associated with this characteristic instance.
227  * <p>
228  * If the DBTDevice's GATTHandler is null, i.e. not connected, {@code false} is being returned.
229  * </p>
230  *
231  * @param disableIndicationNotification if true, disables the notification and/or indication for this characteristic
232  * using {@link #configNotificationIndication(boolean, boolean, boolean[])}
233  * @return number of removed listener.
234  * @see #configNotificationIndication(boolean, boolean, boolean[])
235  * @see BTDevice#removeAllAssociatedCharListener(BTGattChar)
236  * @see #addCharListener(Listener)
237  * @see #addCharListener(Listener, boolean[])
238  * @see #removeCharListener(Listener, boolean)
239  * @since 2.0.0
240  */
241  public int removeAllAssociatedCharListener(final boolean disableIndicationNotification);
242 
243  /**
244  * Disables notifications of the value and unregisters the callback object
245  * passed through the corresponding enable method. It disables notifcations
246  * at BLE level for this characteristic.
247  */
249 
250  /**
251  * Writes the value of this characteristic,
252  * using one of the following methods depending on {@code withResponse}
253  * <pre>
254  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value
255  * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response
256  * </pre>
257  * @param[in] arg_value The data as vector<uchar>
258  * to be written packed in a GBytes struct
259  * @param withResponse if {@code true} a subsequent ATT_WRITE_RSP is expected, otherwise not.
260  * @return TRUE if value was written successfully
261  * @since 2.0.0
262  * @implNote {@code withResponse} parameter has been added since 2.0.0
263  */
264  public boolean writeValue(byte[] argValue, boolean withResponse) throws BTException;
265 
266  /* D-Bus property accessors: */
267 
268  /** Get the UUID of this characteristic.
269  * @return The 128 byte UUID of this characteristic, NULL if an error occurred
270  */
271  public 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  public 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  */
296  public List<BTGattDesc> getDescriptors();
297 }
org.direct_bt.BTGattChar.getUUID
String getUUID()
Get the UUID of this characteristic.
org.direct_bt.BTGattChar.getNotifying
boolean getNotifying(final boolean enabledState[])
Returns true if notification for changes of this characteristic are activated.
org.direct_bt.BTGattChar.find
BTGattDesc find(final String UUID)
Find a BluetoothGattDescriptor.
org.direct_bt.BTGattChar.configNotificationIndication
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.
org.direct_bt.BTGattChar.getProperties
GattCharPropertySet getProperties()
Returns the properties of this characteristic.
org.direct_bt.BTGattChar.enableNotificationOrIndication
boolean enableNotificationOrIndication(final boolean enabledState[])
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
org.direct_bt.BTGattService
Provides access to Bluetooth GATT characteristic.
Definition: BTGattService.java:41
org.direct_bt.BTGattChar.find
BTGattDesc find(final String UUID, final long timeoutMS)
Find a BluetoothGattDescriptor.
org.direct_bt.BTGattChar.Listener.indicationReceived
void indicationReceived(final BTGattChar charDecl, final byte[] value, final long timestamp, final boolean confirmationSent)
Called from native BLE stack, initiated by a received indication associated with the given BTGattChar...
org.direct_bt.BTGattChar.Listener
BTGattChar event listener for notification and indication events.
Definition: BTGattChar.java:56
org.direct_bt.BTGattChar.writeValue
boolean writeValue(byte[] argValue, boolean withResponse)
Writes the value of this characteristic, using one of the following methods depending on.
org.direct_bt.BTGattChar
Provides access to Bluetooth GATT characteristic.
Definition: BTGattChar.java:40
org.direct_bt.BTGattChar.removeAllAssociatedCharListener
int removeAllAssociatedCharListener(final boolean disableIndicationNotification)
Disables the notification and/or indication for this characteristic BLE level if.
org.direct_bt.BTGattDesc
Provides access to Bluetooth GATT descriptor.
Definition: BTGattDesc.java:38
org.direct_bt.BTGattChar.disableValueNotifications
void disableValueNotifications()
Disables notifications of the value and unregisters the callback object passed through the correspond...
org.direct_bt.GattCharPropertySet
Bit mask of GATT Characteristic Properties.
Definition: GattCharPropertySet.java:35
org.direct_bt.BTException
Definition: BTException.java:32
org.direct_bt.BTObject
Definition: BTObject.java:31
org.direct_bt.BTGattChar.addCharListener
boolean addCharListener(final Listener listener)
Add the given BTGattChar.Listener to the listener list if not already present.
org.direct_bt.BTGattChar.getDescriptors
List< BTGattDesc > getDescriptors()
Returns a list of BluetoothGattDescriptors this characteristic exposes.
org.direct_bt.BTGattChar.getService
BTGattService getService()
Returns the service to which this characteristic belongs to.
org.direct_bt.BTGattChar.readValue
byte[] readValue()
Reads the value of this characteristic.
org.direct_bt.BTGattChar.Listener.notificationReceived
void notificationReceived(final BTGattChar charDecl, final byte[] value, final long timestamp)
Called from native BLE stack, initiated by a received notification associated with the given BTGattCh...