Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTDevice.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 import java.util.Map;
33 
34 /**
35  * Provides access to Bluetooth adapters.
36  *
37  * @see [Bluetooth Specification](https://www.bluetooth.com/specifications/bluetooth-core-specification/)
38  * @see [BlueZ device API](http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/device-api.txt)
39  *
40  */
41 public interface BTDevice extends BTObject
42 {
43  /** Find a BluetoothGattService. If parameter UUID is not null,
44  * the returned object will have to match it.
45  * It will first check for existing objects. It will not turn on discovery
46  * or connect to devices.
47  * @parameter UUID optionally specify the UUID of the BluetoothGattService you are
48  * waiting for
49  * @parameter timeoutMS the function will return after timeout time in milliseconds, a
50  * value of zero means wait forever. If object is not found during this time null will be returned.
51  * @return An object matching the UUID or null if not found before
52  * timeout expires or event is canceled.
53  */
54  BTGattService find(String UUID, long timeoutMS);
55 
56  /** Find a BluetoothGattService. If parameter UUID is not null,
57  * the returned object will have to match it.
58  * It will first check for existing objects. It will not turn on discovery
59  * or connect to devices.
60  * @parameter UUID optionally specify the UUID of the BluetoothGattService you are
61  * waiting for
62  * @return An object matching the UUID or null if not found before
63  * timeout expires or event is canceled.
64  */
65  BTGattService find(String UUID);
66 
67  /* Bluetooth method calls: */
68 
69  /**
70  * Add the given {@link AdapterStatusListener} to the list if not already present,
71  * listening only for events matching this device.
72  *
73  * The AdapterStatusListener is released at remove() or this device's destruction.
74  * <p>
75  * The newly added {@link AdapterStatusListener} will receive an initial
76  * {@link AdapterStatusListener#adapterSettingsChanged(BTAdapter, AdapterSettings, AdapterSettings, AdapterSettings, long) adapterSettingsChanged}
77  * event, passing an {@link AdapterSettings empty oldMask and changedMask}, as well as {@link AdapterSettings current newMask}. <br>
78  * This allows the receiver to be aware of this adapter's current settings.
79  * </p>
80  * @param listener A {@link AdapterStatusListener} instance
81  * @param deviceMatch Optional {@link BTDevice} to be matched before calling any
82  * {@link AdapterStatusListener} {@code device*} methods. Pass {@code null} for no filtering.
83  * @return true if the given listener is not element of the list and has been newly added, otherwise false.
84  * @since 2.3.0
85  * @see {@link BTDevice#addStatusListener(AdapterStatusListener, BTDevice)}
86  * @see {@link #removeStatusListener(AdapterStatusListener)}
87  * @see {@link #removeAllStatusListener()}
88  */
89  public boolean addStatusListener(final AdapterStatusListener listener);
90 
91  /**
92  * Remove the given {@link AdapterStatusListener} from the list.
93  * @param listener A {@link AdapterStatusListener} instance
94  * @return true if the given listener is an element of the list and has been removed, otherwise false.
95  * @since 2.3.0
96  */
98 
99  /**
100  * {@code jau.direct_bt}: Disconnect the LE or BREDR peer's GATT and HCI connection.
101  * <p>
102  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.1.6 Disconnect command
103  * </p>
104  * <p>
105  * The actual disconnect event will be delivered asynchronous and
106  * the connection event can be caught via AdapterStatusListener::deviceDisconnected(..).
107  * If unacceptable, {@link HCIStatusCode#UNACCEPTABLE_CONNECTION_PARAM} is being returned.
108  * </p>
109  * <p>
110  * The device will be removed from the managing adapter's connected devices
111  * when {@link AdapterStatusListener#deviceDisconnected(BTDevice, HCIStatusCode, long)} has been received.
112  * </p>
113  * <p>
114  * An open GATT connection will also be closed.<br>
115  * The connection to this device is removed, removing all connected profiles.
116  * </p>
117  * <p>
118  * An application using one thread per device and rapid connect, should either use {@link #disconnect()} or {@link #remove()},
119  * but never issue remove() after disconnect(). Doing so would eventually delete the device being already
120  * in use by another thread due to discovery post disconnect!
121  * </p>
122  * @return {@link HCIStatusCode#SUCCESS} if the command has been accepted, otherwise {@link HCIStatusCode} may disclose reason for rejection.
123  * @since 2.1.0 change API, i.e. return value from boolean to HCIStatusCode in favor of <i>direct_bt</i>
124  * @implNote {@code jau.direct_bt} does not throw a BTException on error, only a 'general' exception in case of fatality like NPE etc (FIXME: Remove throws)
125  */
127 
128  /**
129  * {@code jau.direct_bt}: Establish a default HCI connection to this device, using certain default parameter.
130  * <p>
131  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.8.12 LE Create Connection command <br>
132  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.1.5 Create Connection command
133  * </p>
134  * <p>
135  * Depending on this device's addressType,
136  * either a BREDR (BDADDR_BREDR) or LE (BDADDR_LE_PUBLIC, BDADDR_LE_RANDOM) connection is attempted.<br>
137  * If unacceptable, {@link HCIStatusCode#UNACCEPTABLE_CONNECTION_PARAM} is being returned.
138  * </p>
139  * <p>
140  * The actual new connection handle will be delivered asynchronous and
141  * the connection event can be caught via {@link AdapterStatusListener#deviceConnected(BTDevice, long)},
142  * or if failed via {@link AdapterStatusListener#deviceDisconnected(BTDevice, HCIStatusCode, long)}.
143  * </p>
144  * <p>
145  * The device is tracked by the managing adapter.
146  * </p>
147  * <p>
148  * flagged as auto-connectable.
149  * </p>
150  * @return {@link HCIStatusCode#SUCCESS} if the command has been accepted, otherwise {@link HCIStatusCode} may disclose reason for rejection.
151  * @see #connectLE(short, short, short, short, short, short)
152  * @since 2.1.0 change API, i.e. return value from boolean to HCIStatusCode in favor of {@code jau.direct_bt}.
153  * @implNote {@code jau.direct_bt} does not throw a BTException on error, only a 'general' exception in case of fatality like NPE etc (FIXME: Remove throws)
154  */
156 
157  /**
158  * Establish a HCI BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM connection to this device.
159  * <p>
160  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.8.12 LE Create Connection command
161  * </p>
162  * <p>
163  * If this device's {@link #getAddressType()} is not BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM,
164  * {@link HCIStatusCode#UNACCEPTABLE_CONNECTION_PARAM} is being returned.
165  * </p>
166  * <p>
167  * The actual new connection handle will be delivered asynchronous and
168  * the connection event can be caught via {@link AdapterStatusListener#deviceConnected(BTDevice, long)},
169  * or if failed via {@link AdapterStatusListener#deviceDisconnected(BTDevice, HCIStatusCode, long)}.
170  * </p>
171  * <p>
172  * The device is tracked by the managing adapter.
173  * </p>
174  * <p>
175  * Default parameter are used if {@code -1} has been passed for any of the arguments.
176  * </p>
177  * <p>
178  * Default parameter values are chosen for using public address resolution
179  * and usual connection latency, interval etc.
180  * </p>
181  * <p>
182  * Set window to the same value as the interval, enables continuous scanning.
183  * </p>
184  *
185  * @param le_scan_interval in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]
186  * @param le_scan_window in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]. Shall be <= le_scan_interval
187  * @param conn_interval_min in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
188  * @param conn_interval_max in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
189  * @param conn_latency slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3].
190  * @param supervision_timeout in units of 10ms, default value >= 10 x conn_interval_max, we use 500 ms minimum; Value range [0xA-0x0C80] for [100ms - 32s].
191  * @return {@link HCIStatusCode#SUCCESS} if the command has been accepted, otherwise {@link HCIStatusCode} may disclose reason for rejection.
192  * @see BTUtils#getHCIConnSupervisorTimeout(int, int, int, int)
193  * @see #connect()
194  */
195  HCIStatusCode connectLE(final short le_scan_interval, final short le_scan_window,
196  final short conn_interval_min, final short conn_interval_max,
197  final short conn_latency, final short supervision_timeout);
198 
199 
200  /**
201  * Returns the available {@link SMPKeyMask.KeyType} {@link SMPKeyMask} for the responder (LL slave) or initiator (LL master).
202  * @param responder if true, queries the responder (LL slave) key, otherwise the initiator (LL master) key.
203  * @return {@link SMPKeyMask.KeyType} {@link SMPKeyMask} result
204  * @since 2.2.0
205  */
206  SMPKeyMask getAvailableSMPKeys(final boolean responder);
207 
208  /**
209  * Returns a copy of the long term key (LTK) info, valid after connection and SMP pairing has been completed.
210  * @param responder true will return the responder's LTK info (remote device, LL slave), otherwise the initiator's (the LL master).
211  * @return the resulting key. {@link SMPLongTermKeyInfo#enc_size} will be zero if invalid.
212  * @see {@link SMPPairingState#COMPLETED}
213  * @see {@link AdapterStatusListener#deviceReady(BTDevice, long)}
214  * @since 2.2.0
215  */
216  SMPLongTermKeyInfo getLongTermKeyInfo(final boolean responder);
217 
218  /**
219  * Sets the long term ket (LTK) info of this device to reuse pre-paired encryption.
220  * <p>
221  * Must be called before connecting to this device, otherwise {@link HCIStatusCode#CONNECTION_ALREADY_EXISTS} will be returned.
222  * </p>
223  * @param ltk the pre-paired encryption LTK
224  * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the appropriate error code.
225  * @since 2.2.0
226  */
228 
229  /**
230  * Returns a copy of the Signature Resolving Key (LTK) info, valid after connection and SMP pairing has been completed.
231  * @param responder true will return the responder's LTK info (remote device, LL slave), otherwise the initiator's (the LL master).
232  * @return the resulting key
233  * @see {@link SMPPairingState#COMPLETED}
234  * @see {@link AdapterStatusListener#deviceReady(BTDevice, long)}
235  * @since 2.2.0
236  */
238 
239  /**
240  * Unpairs this device from the adapter while staying connected.
241  * <p>
242  * All keys will be cleared within the adapter and host implementation.<br>
243  * Should rarely being used by user.<br>
244  * Internally being used to re-start pairing if GATT connection fails
245  * in {@link PairingMode#PRE_PAIRED} mode.
246  * </p>
247  * @return {@link HCIStatusCode#SUCCESS} or an appropriate error status.
248  * @since 2.1.0
249  */
251 
252  /**
253  * Set the {@link BTSecurityLevel} used to connect to this device on the upcoming connection.
254  * <p>
255  * Method returns false if {@link BTSecurityLevel#UNSET} has been given,
256  * operation fails, this device has already being connected,
257  * or {@link #connectLE(short, short, short, short, short, short) connectLE} or {@link #connect()} has been issued already.
258  * </p>
259  * <p>
260  * To ensure a consistent authentication setup,
261  * it is advised to set {@link SMPIOCapability#NO_INPUT_NO_OUTPUT} for sec_level <= {@link BTSecurityLevel#ENC_ONLY}
262  * using {@link #setConnSecurity(BTSecurityLevel, SMPIOCapability, boolean) setConnSecurity(..)}
263  * as well as an IO capable {@link SMPIOCapability} value
264  * for {@link BTSecurityLevel#ENC_AUTH} or {@link BTSecurityLevel#ENC_AUTH_FIPS}.<br>
265  * You may like to consider using {@link #setConnSecurityBest(BTSecurityLevel, SMPIOCapability)}.
266  * </p>
267  * @param sec_level {@link BTSecurityLevel} to be applied, {@link BTSecurityLevel#UNSET} will be ignored and method fails.
268  * @return
269  * @since 2.1.0
270  * @see BTSecurityLevel
271  * @see SMPIOCapability
272  * @see #getConnSecurityLevel()
273  * @see #setConnIOCapability(SMPIOCapability)
274  * @see #getConnIOCapability()
275  * @see #setConnSecurity(BTSecurityLevel, SMPIOCapability)
276  * @see #setConnSecurityBest(BTSecurityLevel, SMPIOCapability)
277  */
278  boolean setConnSecurityLevel(final BTSecurityLevel sec_level);
279 
280  /**
281  * Return the {@link BTSecurityLevel}, determined when the connection is established.
282  * @since 2.1.0
283  * @see BTSecurityLevel
284  * @see SMPIOCapability
285  * @see #setConnSecurityLevel(BTSecurityLevel)
286  * @see #setConnIOCapability(SMPIOCapability, boolean)
287  * @see #getConnIOCapability()
288  * @see #setConnSecurity(BTSecurityLevel, SMPIOCapability, boolean)
289  * @see #setConnSecurityBest(BTSecurityLevel, SMPIOCapability)
290  */
292 
293  /**
294  * Sets the given {@link SMPIOCapability} used to connect to this device on the upcoming connection.
295  * <p>
296  * Method returns false if {@link SMPIOCapability#UNSET} has been given,
297  * operation fails, this device has already being connected,
298  * or {@link #connectLE(short, short, short, short, short, short) connectLE} or {@link #connect()} has been issued already.
299  * </p>
300  * @param io_cap {@link SMPIOCapability} to be applied, {@link SMPIOCapability#UNSET} will be ignored and method fails.
301  * @since 2.1.0
302  * @see BTSecurityLevel
303  * @see SMPIOCapability
304  * @see #setConnSecurityLevel(BTSecurityLevel)
305  * @see #getConnSecurityLevel()
306  * @see #getConnIOCapability()
307  * @see #setConnSecurity(BTSecurityLevel, SMPIOCapability)
308  * @see #setConnSecurityBest(BTSecurityLevel, SMPIOCapability)
309  */
310  boolean setConnIOCapability(final SMPIOCapability io_cap);
311 
312  /**
313  * Return the {@link SMPIOCapability} value, determined when the connection is established.
314  * @since 2.1.0
315  * @see BTSecurityLevel
316  * @see SMPIOCapability
317  * @see #setConnSecurityLevel(BTSecurityLevel)
318  * @see #getConnSecurityLevel()
319  * @see #setConnIOCapability(SMPIOCapability)
320  * @see #setConnSecurity(BTSecurityLevel, SMPIOCapability)
321  * @see #setConnSecurityBest(BTSecurityLevel, SMPIOCapability)
322  */
324 
325  /**
326  * Sets the given {@link BTSecurityLevel} and {@link SMPIOCapability} used to connect to this device on the upcoming connection.
327  * <p>
328  * Method returns false if this device has already being connected,
329  * or {@link #connectLE(short, short, short, short, short, short) connectLE} or {@link #connect()} has been issued already.
330  * </p>
331  * <p>
332  * Method either changes both parameter for the upcoming connection or none at all.
333  * </p>
334  * @param sec_level {@link BTSecurityLevel} to be applied.
335  * @param io_cap {@link SMPIOCapability} to be applied.
336  * @since 2.1.0
337  * @see BTSecurityLevel
338  * @see SMPIOCapability
339  * @see #setConnSecurityLevel(BTSecurityLevel)
340  * @see #getConnSecurityLevel()
341  * @see #setConnIOCapability(SMPIOCapability)
342  * @see #getConnIOCapability()
343  * @see #setConnSecurityBest(BTSecurityLevel, SMPIOCapability)
344  */
345  boolean setConnSecurity(final BTSecurityLevel sec_level, final SMPIOCapability io_cap);
346 
347  /**
348  * Convenience method to determine the best practice {@link BTSecurityLevel} and {@link SMPIOCapability}
349  * based on the given arguments, used to connect to this device on the upcoming connection.
350  * <pre>
351  * if( BTSecurityLevel::UNSET < sec_level && SMPIOCapability::UNSET != io_cap ) {
352  * return setConnSecurity(sec_level, io_cap);
353  * } else if( BTSecurityLevel::UNSET < sec_level ) {
354  * if( BTSecurityLevel::ENC_ONLY >= sec_level ) {
355  * return setConnSecurity(sec_level, SMPIOCapability::NO_INPUT_NO_OUTPUT);
356  * } else {
357  * return setConnSecurityLevel(sec_level);
358  * }
359  * } else if( SMPIOCapability::UNSET != io_cap ) {
360  * return setConnIOCapability(io_cap);
361  * } else {
362  * return false;
363  * }
364  * </pre>
365  * <p>
366  * Method returns false if {@link BTSecurityLevel#UNSET} and {@link SMPIOCapability#UNSET} has been given,
367  * operation fails, this device has already being connected,
368  * or {@link #connectLE(short, short, short, short, short, short) connectLE} or {@link #connect()} has been issued already.
369  * </p>
370  * @param sec_level {@link BTSecurityLevel} to be applied.
371  * @param io_cap {@link SMPIOCapability} to be applied.
372  * @since 2.1.0
373  * @see BTSecurityLevel
374  * @see SMPIOCapability
375  * @see #setConnSecurityLevel(BTSecurityLevel)
376  * @see #getConnSecurityLevel()
377  * @see #setConnIOCapability(SMPIOCapability)
378  * @see #getConnIOCapability()
379  * @see #setConnSecurity(BTSecurityLevel, SMPIOCapability)
380  * @see #setConnSecurityAuto(SMPIOCapability)
381  */
382  boolean setConnSecurityBest(final BTSecurityLevel sec_level, final SMPIOCapability io_cap);
383 
384  /**
385  * Set automatic security negotiation of {@link BTSecurityLevel} and {@link SMPIOCapability} pairing mode.
386  * <p>
387  * Disabled by default and if set to {@link SMPIOCapability#UNSET}
388  * </p>
389  * Implementation iterates through below setup from highest security to lowest,
390  * while performing a full connection attempt for each.
391  * <pre>
392  * BTSecurityLevel::ENC_AUTH_FIPS, iocap_auto*
393  * BTSecurityLevel::ENC_AUTH, iocap_auto*
394  * BTSecurityLevel::ENC_ONLY, SMPIOCapability::NO_INPUT_NO_OUTPUT
395  * BTSecurityLevel::NONE, SMPIOCapability::NO_INPUT_NO_OUTPUT
396  *
397  * (*): user SMPIOCapability choice of for authentication IO, skipped if ::SMPIOCapability::NO_INPUT_NO_OUTPUT
398  * </pre>
399  * <p>
400  * Implementation may perform multiple connection and disconnect actions
401  * until successful pairing or failure.
402  * </p>
403  * <p>
404  * Intermediate {@link AdapterStatusListener#deviceConnected(BTDevice, short, long) deviceConnected(..)} and
405  * {@link AdapterStatusListener#deviceDisconnected(BTDevice, HCIStatusCode, short, long) deviceDisconnected(..)}
406  * callbacks are not delivered while negotiating. This avoids any interference by the user application.
407  * </p>
408  * @param auth_io_cap user {@link SMPIOCapability} choice for negotiation
409  * @since 2.2.0
410  * @see #isConnSecurityAutoEnabled()
411  * @see BTSecurityLevel
412  * @see SMPIOCapability
413  */
414  boolean setConnSecurityAuto(final SMPIOCapability iocap_auto);
415 
416  /**
417  * Returns true if automatic security negotiation has been enabled via {@link #setConnSecurityAuto(SMPIOCapability)},
418  * otherwise false.
419  * @since 2.2.0
420  * @see #setConnSecurityAuto(SMPIOCapability)
421  */
423 
424  /**
425  * Method sets the given passkey entry, see {@link PairingMode#PASSKEY_ENTRY_ini}.
426  * <p>
427  * Call this method if the device shall be securely paired with {@link PairingMode#PASSKEY_ENTRY_ini},
428  * i.e. when notified via {@link AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long) devicePairingState}
429  * in state {@link SMPPairingState#PASSKEY_EXPECTED}.
430  * </p>
431  *
432  * @param passkey used for {@link PairingMode#PASSKEY_ENTRY_ini} method.
433  * Will be encrypted before sending to counter-party.
434  *
435  * @return {@link HCIStatusCode#SUCCESS} if the command has been accepted, otherwise {@link HCIStatusCode} may disclose reason for rejection.
436  * @see PairingMode
437  * @see SMPPairingState
438  * @see AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long)
439  * @see #setPairingPasskey(int)
440  * @see #setPairingNumericComparison(boolean)
441  * @see #getPairingMode()
442  * @see #getPairingState()
443  * @since 2.1.0
444  */
445  HCIStatusCode setPairingPasskey(final int passkey);
446 
447  /**
448  * Method replies with a negative passkey response, i.e. rejection, see {@link PairingMode#PASSKEY_ENTRY_ini}.
449  * <p>
450  * You may call this method if the device shall be securely paired with {@link PairingMode#PASSKEY_ENTRY_ini},
451  * i.e. when notified via {@link AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long) devicePairingState}
452  * in state {@link SMPPairingState#PASSKEY_EXPECTED}.
453  * </p>
454  * <p>
455  * Current experience exposed a roughly 3s immediate disconnect handshake with certain devices and/or Kernel BlueZ code.
456  *
457  * Hence using {@link #setPairingPasskey(int)} with {@code passkey = 0} is recommended, especially when utilizing
458  * automatic security negotiation via {@link #setConnSecurityAuto()}!
459  * </p>
460  *
461  * @return {@link HCIStatusCode#SUCCESS} if the command has been accepted, otherwise {@link HCIStatusCode} may disclose reason for rejection.
462  * @see PairingMode
463  * @see SMPPairingState
464  * @see AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long)
465  * @see #setPairingPasskey(int)
466  * @see #setPairingNumericComparison(boolean)
467  * @see #getPairingMode()
468  * @see #getPairingState()
469  * @since 2.1.0
470  */
472 
473  /**
474  * Method sets the numeric comparison result, see {@link PairingMode#NUMERIC_COMPARE_ini}.
475  * <p>
476  * Call this method if the device shall be securely paired with {@link PairingMode#NUMERIC_COMPARE_ini},
477  * i.e. when notified via {@link AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long) devicePairingState}
478  * in state {@link SMPPairingState#NUMERIC_COMPARE_EXPECTED}.
479  * </p>
480  *
481  * @param equal used for {@link PairingMode#NUMERIC_COMPARE_ini} method.
482  * Will be encrypted before sending to counter-party.
483  *
484  * @return {@link HCIStatusCode#SUCCESS} if the command has been accepted, otherwise {@link HCIStatusCode} may disclose reason for rejection.
485  * @see PairingMode
486  * @see SMPPairingState
487  * @see AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long)
488  * @see #setPairingPasskey(int)
489  * @see #setPairingNumericComparison(boolean)
490  * @see #getPairingMode()
491  * @see #getPairingState()
492  * @since 2.1.0
493  */
495 
496  /**
497  * Returns the current {@link PairingMode} used by the device.
498  * <p>
499  * If the device is not paired, the current mode is {@link PairingMode#NONE}.
500  * </p>
501  * <p>
502  * If the Pairing Feature Exchange is completed, i.e. {@link SMPPairingState#FEATURE_EXCHANGE_COMPLETED}
503  * as notified by
504  * {@link AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long) devicePairingState}
505  * the current mode reflects the currently used {@link PairingMode}.
506  * </p>
507  * <p>
508  * In case the Pairing Feature Exchange is in progress, the current mode is {@link PairingMode#NEGOTIATING}.
509  * </p>
510  * @return current PairingMode.
511  * @see PairingMode
512  * @see SMPPairingState
513  * @see AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long)
514  * @see #setPairingPasskey(int)
515  * @see #setPairingNumericComparison(boolean)
516  * @see #getPairingMode()
517  * @see #getPairingState()
518  * @since 2.1.0
519  */
521 
522  /**
523  * Returns the current {@link SMPPairingState}.
524  * <p>
525  * If the device is not paired, the current state is {@link SMPPairingState#NONE}.
526  * </p>
527  * @see PairingMode
528  * @see SMPPairingState
529  * @see AdapterStatusListener#devicePairingState(BTDevice, SMPPairingState, PairingMode, long)
530  * @see #setPairingPasskey(int)
531  * @see #setPairingNumericComparison(boolean)
532  * @see #getPairingMode()
533  * @see #getPairingState()
534  * @since 2.1.0
535  */
537 
538  /**
539  * Remove this device from the system (like an unpair).
540  * <p>
541  * Direct-BT: Disconnects this device via disconnect(..) if getConnected()==true
542  * and explicitly removes its shared references from the Adapter:
543  * connected-devices, discovered-devices and shared-devices.
544  * </p>
545  * <p>
546  * This method shall be issued to ensure no device reference will
547  * be leaked in a long lived adapter,
548  * as only its reference within connected-devices and discovered-devices are removed at disconnect.
549  * </p>
550  * <p>
551  * After calling this method, this instance is destroyed and shall not be used anymore!
552  * </p>
553  * <p>
554  * This method is automatically called @ destructor.
555  * </p>
556  * <p>
557  * This method is an atomic operation.
558  * </p>
559  * <p>
560  * An application using one thread per device and rapid connect, should either use disconnect() or remove(),
561  * but never issue remove() after disconnect() if the device is in use.
562  * </p>
563  * @return TRUE if the device has been removed
564  * @throws BTException
565  */
566  boolean remove() throws BTException;
567 
568  /**
569  * Returns whether the device is valid, i.e. reference is valid
570  * but not necessarily {@link #getConnected() connected}.
571  * @return true if this device's references are valid and hasn't been {@link #remove()}'ed
572  * @see #remove()
573  * @since 2.2.0
574  */
575  public boolean isValid();
576 
577  /** Returns a list of BluetoothGattServices available on this device.
578  * @return A list of BluetoothGattServices available on this device,
579  * NULL if an error occurred
580  */
582 
583  /**
584  * Issues a GATT ping to the device, validating whether it is still reachable.
585  * <p>
586  * This method could be periodically utilized to shorten the underlying OS disconnect period
587  * after turning the device off, which lies within 7-13s.
588  * </p>
589  * <p>
590  * In case the device is no more reachable, disconnect will be initiated due to the occurring IO error.
591  * </p>
592  * <p>
593  * GATT services must have been initialized via {@link #getServices()}, otherwise {@code false} is being returned.
594  * </p>
595  * @return {@code true} if successful or not implemented, otherwise false in case no GATT services exists or is not connected..
596  * @since 2.0.0
597  */
598  boolean pingGATT();
599 
600  /**
601  * Returns the timestamp in monotonic milliseconds when this device instance has been created,
602  * either via its initial discovery or its initial direct connection.
603  *
604  * @see BTUtils#currentTimeMillis()
605  * @since 2.0.0
606  */
608 
609  /**
610  * Returns the timestamp in monotonic milliseconds when this device instance has
611  * discovered or connected directly the last time.
612  *
613  * @see BTUtils#currentTimeMillis()
614  * @since 2.0.0
615  */
617 
618  /**
619  * Returns the timestamp in monotonic milliseconds when this device instance underlying data
620  * has been updated the last time.
621  *
622  * @see BTUtils#currentTimeMillis()
623  * @since 2.0.0
624  */
626 
627  /**
628  * Returns the unique device {@link EUI48} address and {@link BDAddressType} type.
629  * @since 2.2.0
630  */
632 
633  /** Returns the remote friendly name of this device.
634  * @return The remote friendly name of this device, or NULL if not set.
635  */
636  String getName();
637 
638  /** Returns the Received Signal Strength Indicator of the device.
639  * @return The Received Signal Strength Indicator of the device.
640  */
641  short getRSSI();
642 
643  /** Returns the connected state of the device.
644  * @return The connected state of the device.
645  */
646  boolean getConnected();
647 
648  /**
649  * Return the HCI connection handle to the LE or BREDR peer, zero if not connected.
650  * @since 2.1.0
651  */
653 
654  /** Returns the adapter on which this device was discovered or
655  * connected.
656  * @return The adapter.
657  */
659 
660  /** Returns a map containing manufacturer specific advertisement data.
661  * An entry has a uint16_t key and an array of bytes.
662  * @return manufacturer specific advertisement data.
663  */
664  Map<Short, byte[]> getManufacturerData();
665 
666  /** Returns the transmission power level (0 means unknown).
667  * @return the transmission power level (0 means unknown).
668  */
669  short getTxPower();
670 
671  /**
672  * Add the given {@link BTGattCharListener} to the listener list if not already present.
673  * <p>
674  * To enable the actual BLE notification and/or indication, one needs to call
675  * {@link BTGattChar#configNotificationIndication(boolean, boolean, boolean[])}
676  * or {@link BTGattChar#enableNotificationOrIndication(boolean[])}.
677  * </p>
678  * @param listener A {@link BTGattCharListener} instance, listening to all {@link BTGattChar} events of this device
679  * @return true if the given listener is not element of the list and has been newly added, otherwise false.
680  * @throws IllegalStateException if the {@link BTDevice}'s BTGattHandler is null, i.e. not connected
681  * @throws IllegalStateException if the given {@link BTGattCharListener} is already in use, i.e. added.
682  * @see BTGattChar#configNotificationIndication(boolean, boolean, boolean[])
683  * @see BTGattChar#enableNotificationOrIndication(boolean[])
684  * @since 2.0.0
685  */
686  public boolean addCharListener(final BTGattCharListener listener)
687  throws IllegalStateException;
688 
689  /**
690  * Remove the given {@link BTGattCharListener} from the listener list.
691  * <p>
692  * If the {@link BTDevice}'s BTGattHandler is null, i.e. not connected, {@code false} is being returned.
693  * </p>
694  * @param listener A {@link BTGattCharListener} instance
695  * @return true if the given listener is an element of the list and has been removed, otherwise false.
696  * @since 2.0.0
697  */
698  public boolean removeCharListener(final BTGattCharListener l);
699 
700  /**
701  * Remove all {@link BTGattCharListener} from the list, which are associated to the given {@link BTGattChar}.
702  * <p>
703  * Implementation tests all listener's {@link BTGattCharListener#getAssociatedChar()}
704  * to match with the given associated characteristic.
705  * </p>
706  * @param associatedCharacteristic the match criteria to remove any BTGattCharListener from the list
707  * @return number of removed listener.
708  * @since 2.0.0
709  */
710  public int removeAllAssociatedCharListener(final BTGattChar associatedCharacteristic);
711 
712  /**
713  * Remove all {@link BTGattCharListener} from the list.
714  * @return number of removed listener.
715  * @since 2.0.0
716  */
717  public int removeAllCharListener();
718 }
org.direct_bt.BTDevice.getLastUpdateTimestamp
long getLastUpdateTimestamp()
Returns the timestamp in monotonic milliseconds when this device instance underlying data has been up...
org.direct_bt.BTDevice.find
BTGattService find(String UUID)
Find a BluetoothGattService.
org.direct_bt.BTAdapter
Provides access to Bluetooth adapters.
Definition: BTAdapter.java:41
org.direct_bt.BTDevice.getPairingState
SMPPairingState getPairingState()
Returns the current SMPPairingState.
org.direct_bt.SMPIOCapability
SMP IO Capability value.
Definition: SMPIOCapability.java:37
org.direct_bt.BTSecurityLevel
Bluetooth Security Level.
Definition: BTSecurityLevel.java:38
org.direct_bt.BTDevice.getServices
List< BTGattService > getServices()
Returns a list of BluetoothGattServices available on this device.
org.direct_bt.BTDevice.getConnIOCapability
SMPIOCapability getConnIOCapability()
Return the SMPIOCapability value, determined when the connection is established.
org.direct_bt.BTDevice.setConnSecurityAuto
boolean setConnSecurityAuto(final SMPIOCapability iocap_auto)
Set automatic security negotiation of BTSecurityLevel and SMPIOCapability pairing mode.
org.direct_bt.PairingMode
Bluetooth secure pairing mode.
Definition: PairingMode.java:40
org.direct_bt.BTDevice.getLongTermKeyInfo
SMPLongTermKeyInfo getLongTermKeyInfo(final boolean responder)
Returns a copy of the long term key (LTK) info, valid after connection and SMP pairing has been compl...
org.direct_bt.BTDevice.removeAllCharListener
int removeAllCharListener()
Remove all BTGattCharListener from the list.
org.direct_bt.BTDevice.getConnected
boolean getConnected()
Returns the connected state of the device.
org.direct_bt.BTDevice.setConnSecurity
boolean setConnSecurity(final BTSecurityLevel sec_level, final SMPIOCapability io_cap)
Sets the given BTSecurityLevel and SMPIOCapability used to connect to this device on the upcoming con...
org.direct_bt.BTDevice.isValid
boolean isValid()
Returns whether the device is valid, i.e.
org.direct_bt.BTDevice.getRSSI
short getRSSI()
Returns the Received Signal Strength Indicator of the device.
org.direct_bt.BTDevice.getManufacturerData
Map< Short, byte[]> getManufacturerData()
Returns a map containing manufacturer specific advertisement data.
org.direct_bt.BTDevice.removeStatusListener
boolean removeStatusListener(final AdapterStatusListener l)
Remove the given AdapterStatusListener from the list.
org.direct_bt.BTDevice.getConnectionHandle
short getConnectionHandle()
Return the HCI connection handle to the LE or BREDR peer, zero if not connected.
org.direct_bt.BTDevice.pingGATT
boolean pingGATT()
Issues a GATT ping to the device, validating whether it is still reachable.
org.direct_bt.SMPLongTermKeyInfo
SMP Long Term Key Info, used for platform agnostic persistence.
Definition: SMPLongTermKeyInfo.java:39
org.direct_bt.SMPKeyMask
SMP Key Type for Distribution, indicates keys distributed in the Transport Specific Key Distribution ...
Definition: SMPKeyMask.java:44
org.direct_bt.BTDevice.setConnSecurityLevel
boolean setConnSecurityLevel(final BTSecurityLevel sec_level)
Set the BTSecurityLevel used to connect to this device on the upcoming connection.
org.direct_bt.BTGattService
Provides access to Bluetooth GATT characteristic.
Definition: BTGattService.java:41
org.direct_bt.BTDevice.removeCharListener
boolean removeCharListener(final BTGattCharListener l)
Remove the given BTGattCharListener from the listener list.
org.direct_bt.BTDevice.connectLE
HCIStatusCode connectLE(final short le_scan_interval, final short le_scan_window, final short conn_interval_min, final short conn_interval_max, final short conn_latency, final short supervision_timeout)
Establish a HCI BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM connection to this device.
org.direct_bt.BTDevice.find
BTGattService find(String UUID, long timeoutMS)
Find a BluetoothGattService.
org.direct_bt.BTDevice.connect
HCIStatusCode connect()
org.direct_bt.BTDevice.getConnSecurityLevel
BTSecurityLevel getConnSecurityLevel()
Return the BTSecurityLevel, determined when the connection is established.
org.direct_bt.BTDevice.getAvailableSMPKeys
SMPKeyMask getAvailableSMPKeys(final boolean responder)
Returns the available SMPKeyMask.KeyType SMPKeyMask for the responder (LL slave) or initiator (LL mas...
org.direct_bt.BTDevice.setPairingNumericComparison
HCIStatusCode setPairingNumericComparison(final boolean equal)
Method sets the numeric comparison result, see PairingMode#NUMERIC_COMPARE_ini.
org.direct_bt.BTDevice.addStatusListener
boolean addStatusListener(final AdapterStatusListener listener)
Add the given AdapterStatusListener to the list if not already present, listening only for events mat...
org.direct_bt.BTDevice.getCreationTimestamp
long getCreationTimestamp()
Returns the timestamp in monotonic milliseconds when this device instance has been created,...
org.direct_bt.SMPPairingState
SMP Pairing Process state definition.
Definition: SMPPairingState.java:38
org.direct_bt.BDAddressAndType
Unique Bluetooth EUI48 address and BDAddressType tuple.
Definition: BDAddressAndType.java:36
org.direct_bt.BTDevice.addCharListener
boolean addCharListener(final BTGattCharListener listener)
Add the given BTGattCharListener to the listener list if not already present.
org.direct_bt.BTDevice.getLastDiscoveryTimestamp
long getLastDiscoveryTimestamp()
Returns the timestamp in monotonic milliseconds when this device instance has discovered or connected...
org.direct_bt.HCIStatusCode
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
Definition: HCIStatusCode.java:34
org.direct_bt.BTDevice.getTxPower
short getTxPower()
Returns the transmission power level (0 means unknown).
org.direct_bt.BTDevice.removeAllAssociatedCharListener
int removeAllAssociatedCharListener(final BTGattChar associatedCharacteristic)
Remove all BTGattCharListener from the list, which are associated to the given BTGattChar.
org.direct_bt.BTDevice.getPairingMode
PairingMode getPairingMode()
Returns the current PairingMode used by the device.
org.direct_bt.BTGattChar
Provides access to Bluetooth GATT characteristic.
Definition: BTGattChar.java:40
org.direct_bt.BTDevice
Provides access to Bluetooth adapters.
Definition: BTDevice.java:42
org.direct_bt.BTDevice.setConnIOCapability
boolean setConnIOCapability(final SMPIOCapability io_cap)
Sets the given SMPIOCapability used to connect to this device on the upcoming connection.
org.direct_bt.BTException
Definition: BTException.java:32
org.direct_bt.BTDevice.getAddressAndType
BDAddressAndType getAddressAndType()
Returns the unique device EUI48 address and BDAddressType type.
org.direct_bt.BTDevice.unpair
HCIStatusCode unpair()
Unpairs this device from the adapter while staying connected.
org.direct_bt.BTDevice.setPairingPasskey
HCIStatusCode setPairingPasskey(final int passkey)
Method sets the given passkey entry, see PairingMode#PASSKEY_ENTRY_ini.
org.direct_bt.BTDevice.isConnSecurityAutoEnabled
boolean isConnSecurityAutoEnabled()
Returns true if automatic security negotiation has been enabled via setConnSecurityAuto(SMPIOCapabili...
org.direct_bt.BTDevice.setPairingPasskeyNegative
HCIStatusCode setPairingPasskeyNegative()
Method replies with a negative passkey response, i.e.
org.direct_bt.BTDevice.setLongTermKeyInfo
HCIStatusCode setLongTermKeyInfo(final SMPLongTermKeyInfo ltk)
Sets the long term ket (LTK) info of this device to reuse pre-paired encryption.
org.direct_bt.BTDevice.setConnSecurityBest
boolean setConnSecurityBest(final BTSecurityLevel sec_level, final SMPIOCapability io_cap)
Convenience method to determine the best practice BTSecurityLevel and SMPIOCapability based on the gi...
org.direct_bt.BTGattCharListener
BTGattChar event listener for notification and indication events.
Definition: BTGattCharListener.java:57
org.direct_bt.BTObject
Definition: BTObject.java:31
org.direct_bt.BTDevice.getName
String getName()
Returns the remote friendly name of this device.
org.direct_bt.SMPSignatureResolvingKeyInfo
SMP Signature Resolving Key Info, used for platform agnostic persistence.
Definition: SMPSignatureResolvingKeyInfo.java:39
org.direct_bt.AdapterStatusListener
BTAdapter status listener for BTDevice discovery events: Added, updated and removed; as well as for c...
Definition: AdapterStatusListener.java:56
org.direct_bt.BTDevice.remove
boolean remove()
Remove this device from the system (like an unpair).
org.direct_bt.BTDevice.getSignatureResolvingKeyInfo
SMPSignatureResolvingKeyInfo getSignatureResolvingKeyInfo(final boolean responder)
Returns a copy of the Signature Resolving Key (LTK) info, valid after connection and SMP pairing has ...
org.direct_bt.BTDevice.disconnect
HCIStatusCode disconnect()
org.direct_bt.BTDevice.getAdapter
BTAdapter getAdapter()
Returns the adapter on which this device was discovered or connected.