Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTAdapter.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 * BTAdapter represents one local Bluetooth Controller.
32 * <p>
33 * @anchor BTAdapterRoles
34 * Local BTAdapter roles (see {@link #getRole()}):
35 *
36 * - {@link BTRole::Master}: The local adapter is discovering remote {@link BTRole::Slave} {@link BTDevice}s and may initiate connections. Enabled via {@link #startDiscovery(boolean, boolean, short, short, byte) startDiscovery(..)}, but also per default at construction.
37 * - {@link BTRole::Slave}: The local adapter is advertising to remote {@link BTRole::Master} {@link BTDevice}s and may accept connections. Enabled explicitly via {@link #startAdvertising(short, short, byte, byte, byte) startAdvertising(..)} until {@link #startDiscovery(boolean, boolean, short, short, byte) startDiscovery(..)}.
38 *
39 * Note the remote {@link BTDevice}'s [opposite role](@ref BTDeviceRoles).
40 * </p>
41 *
42 * @see BTDevice
43 * @see [BTDevice roles](@ref BTDeviceRoles).
44 * @see [Bluetooth Specification](https://www.bluetooth.com/specifications/bluetooth-core-specification/)
45 * @see [Direct-BT Overview](namespaceorg_1_1direct__bt.html#details)
46 */
47public interface BTAdapter extends BTObject
48{
49 /**
50 * Returns the used singleton {@link BTManager} instance, used to create this adapter.
51 */
53
54 /**
55 * Return the current {@link BTRole} of this adapter.
56 * @see @ref BTAdapterRoles
57 * @since 2.4.0
58 */
60
61 /**
62 * Returns the current {@link BTMode} of this adapter.
63 * @since 2.4.0
64 */
66
67 /** Find a BluetoothDevice. If parameters name and address are not null,
68 * the returned object will have to match them.
69 * It will first check for existing objects. It will not turn on discovery
70 * or connect to devices.
71 * @parameter name optionally specify the name of the object you are
72 * waiting for
73 * @parameter address optionally specify the MAC address of the device you are
74 * waiting for
75 * @parameter timeoutMS the function will return after timeout time in milliseconds, a
76 * value of zero means wait forever. If object is not found during this time null will be returned.
77 * @return An object matching the name and address or null if not found before
78 * timeout expires.
79 */
80 BTDevice find(String name, BDAddressAndType addressAndType, long timeoutMS);
81
82 /** Find a BluetoothDevice. If parameters name and address are not null,
83 * the returned object will have to match them.
84 * It will first check for existing objects. It will not turn on discovery
85 * or connect to devices.
86 * @parameter name optionally specify the name of the object you are
87 * waiting for
88 * @parameter address optionally specify the MAC address of the device you are
89 * waiting for
90 * @return An object matching the name and address.
91 */
92 BTDevice find(String name, BDAddressAndType addressAndType);
93
94 /* Bluetooth specific API */
95
96 /**
97 * Returns true, if the adapter's device is already whitelisted.
98 * @since 2.0.0
99 */
100 boolean isDeviceWhitelisted(final BDAddressAndType addressAndType);
101
102 /**
103 * Add the given device to the adapter's autoconnect whitelist.
104 * <p>
105 * The given LE connection parameter will be uploaded to the kernel for the given device first,
106 * if the device is of type {@link BDAddressType#BDADDR_LE_PUBLIC} or {@link BDAddressType#BDADDR_LE_RANDOM}.
107 * </p>
108 * <p>
109 * Method will reject duplicate devices, in which case it should be removed first.
110 * </p>
111 *
112 * @param address
113 * @param address_type
114 * @param ctype
115 * @param conn_interval_min default value 0x000F
116 * @param conn_interval_max default value 0x000F
117 * @param conn_latency default value 0x0000
118 * @param timeout in units of 10ms, default value 1000 for 10000ms or 10s.
119 * @return {@code true} if successful, otherwise {@code false}.
120 *
121 * @see #addDeviceToWhitelist(String, BDAddressType, HCIWhitelistConnectType)
122 * @since 2.0.0
123 */
124 boolean addDeviceToWhitelist(final BDAddressAndType addressAndType,
125 final HCIWhitelistConnectType ctype,
126 final short conn_interval_min, final short conn_interval_max,
127 final short conn_latency, final short timeout);
128
129 /**
130 * Add the given device to the adapter's autoconnect whitelist.
131 * <p>
132 * This variant of {@link #addDeviceToWhitelist(String, BDAddressType, HCIWhitelistConnectType, short, short, short, short)}
133 * uses default connection parameter, which will be uploaded to the kernel for the given device first.
134 * </p>
135 * <p>
136 * Method will reject duplicate devices, in which case it should be removed first.
137 * </p>
138 *
139 * @param address
140 * @param address_type
141 * @param ctype
142 * @return {@code true} if successful, otherwise {@code false}.
143 *
144 * @see #addDeviceToWhitelist(String, BDAddressType, HCIWhitelistConnectType, short, short, short, short)
145 * @since 2.0.0
146 */
147 boolean addDeviceToWhitelist(final BDAddressAndType addressAndType,
148 final HCIWhitelistConnectType ctype);
149
150
151 /**
152 * Remove the given device from the adapter's autoconnect whitelist.
153 * @since 2.0.0
154 */
155 boolean removeDeviceFromWhitelist(final BDAddressAndType addressAndType);
156
157
158 /**
159 * Starts discovery using all default arguments, see {@link #startDiscovery(DiscoveryPolicy, boolean, short, short, byte, boolean)} for details.
160 *
161 * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
162 * @throws BTException
163 * @see #startDiscovery(DiscoveryPolicy, boolean, short, short, byte, boolean)
164 * @see #getDiscovering()
165 * @see DiscoveryPolicy
166 * @since 2.5.0
167 */
169
170 /**
171 * Starts discovery using default arguments, see {@link #startDiscovery(DiscoveryPolicy, boolean, short, short, byte, boolean)} for details.
172 *
173 * @param policy defaults to {@link DiscoveryPolicy#PAUSE_CONNECTED_UNTIL_READY}, see {@link DiscoveryPolicy}
174 * @param le_scan_active true enables delivery of active scanning PDUs like EIR w/ device name (default), otherwise no scanning PDUs shall be sent.
175 * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
176 * @throws BTException
177 * @see #startDiscovery(DiscoveryPolicy, boolean, short, short, byte, boolean)
178 * @see #getDiscovering()
179 * @see DiscoveryPolicy
180 * @since 2.5.0
181 */
182 HCIStatusCode startDiscovery(final DiscoveryPolicy policy, final boolean le_scan_active) throws BTException;
183
184 /**
185 * Starts discovery.
186 *
187 * Returns {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state;
188 *
189 * Depending on given {@link DiscoveryPolicy} `policy`, the discovery mode may be turned-off,
190 * paused until a certain readiness stage has been reached or preserved at all times.
191 * Default is {@link DiscoveryPolicy#PAUSE_CONNECTED_UNTIL_READY}.
192 *
193 * <pre>
194 * + --+-------+--------+-----------+----------------------------------------------------+
195 * | # | meta | native | keepAlive | Note
196 * +---+-------+--------+-----------+----------------------------------------------------+
197 * | 1 | true | true | false | -
198 * | 2 | false | false | false | -
199 * +---+-------+--------+-----------+----------------------------------------------------+
200 * | 3 | true | true | true | -
201 * | 4 | true | false | true | temporarily disabled -> startDiscoveryBackground()
202 * | 5 | false | false | true | [4] -> [5] requires manual DISCOVERING event
203 * +---+-------+--------+-----------+----------------------------------------------------+
204 * </pre>
205 *
206 * Default parameter values are chosen for using public address resolution
207 * and usual discovery intervals etc.
208 *
209 * Method will always clear previous discovered devices via {@link #removeDiscoveredDevices()}.
210 *
211 * Method fails if isAdvertising().
212 *
213 * If successful, method also changes [this adapter's role](@ref BTAdapterRoles) to {@link BTRole#Master}.
214 *
215 * @param gattServerData_ the {@link DBGattServer} data to be offered via GattHandler as ::GATTRole::Client.
216 * Its handles will be setup via DBGattServer::setServicesHandles().
217 * Reference is held until next startDiscovery.
218 * @param policy defaults to {@link DiscoveryPolicy#PAUSE_CONNECTED_UNTIL_READY}, see {@link DiscoveryPolicy}
219 * @param le_scan_active true enables delivery of active scanning PDUs like EIR w/ device name (default), otherwise no scanning PDUs shall be sent
220 * @param le_scan_interval in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]
221 * @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
222 * @param filter_policy 0x00 accepts all PDUs (default), 0x01 only of whitelisted, ...
223 * @param filter_dup true to filter out duplicate AD PDUs (default), otherwise all will be reported.
224 * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
225 * @throws BTException
226 * @since 3.2.0
227 * @see #startDiscovery(DiscoveryPolicy, boolean)
228 * @see #isDiscovering()
229 * @see isAdvertising()
230 * @see DiscoveryPolicy
231 * @see @ref BTAdapterRoles
232 */
234 final DiscoveryPolicy policy, final boolean le_scan_active,
235 final short le_scan_interval, final short le_scan_window,
236 final byte filter_policy,
237 final boolean filter_dup) throws BTException;
238
239 /**
240 * Turns off device discovery if it is enabled.
241 * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
242 * @apiNote return {@link HCIStatusCode} since 2.0.0
243 * @since 2.0.0
244 */
246
247 /**
248 * Return the current {@link DiscoveryPolicy}, set via {@link #startDiscovery(DiscoveryPolicy, boolean, short, short, byte, boolean)}.
249 * @since 2.5.1
250 */
252
253 /**
254 * Manual {@link DiscoveryPolicy} intervention point, allowing user to remove the ready device from
255 * the queue of pausing-discovery devices.
256 *
257 * Manual intervention might be desired, if using {@link DiscoveryPolicy#PAUSE_CONNECTED_UNTIL_DISCONNECTED},
258 * but allowing discovery at an earlier processing step from {@link AdapterStatusListener#deviceReady(BTDevice, long)}.
259 *
260 * Re-enabling discovery is performed on the current thread.
261 *
262 * @param device the {@link BTDevice} to remove from the pausing-discovery queue
263 * @return true if this was the last {@link BTDevice}, re-enabling discovery. Otherwise false.
264 * @since 2.5.1
265 */
267
268 /** Returns a list of discovered BluetoothDevices from this adapter.
269 * @return A list of discovered BluetoothDevices on this adapter,
270 * NULL if an error occurred
271 */
273
274 /**
275 * Remove all the discovered devices found on this adapter.
276 *
277 * @return The number of removed discovered devices on this adapter
278 * @throws BTException
279 * @since 2.2.0
280 * @implNote Changed from 'removeDiscoveredDevices()' for clarity since version 2.2.0
281 */
283
284 /**
285 * Discards matching discovered devices.
286 * @return {@code true} if found and removed, otherwise false.
287 * @since 2.2.0
288 */
289 boolean removeDiscoveredDevice(final BDAddressAndType addressAndType);
290
291
292 /**
293 * Starts advertising
294 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.53 LE Set Extended Advertising Parameters command (Bluetooth 5.0)
295 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.54 LE Set Extended Advertising Data command (Bluetooth 5.0)
296 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.55 LE Set Extended Scan Response Data command (Bluetooth 5.0)
297 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.56 LE Set Extended Advertising Enable command (Bluetooth 5.0)
298 *
299 * if available, otherwise using
300 *
301 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.5 LE Set Advertising Parameters command
302 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.7 LE Set Advertising Data command
303 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.8 LE Set Scan Response Data command
304 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.9 LE Set Advertising Enable command
305 *
306 * Method fails if isDiscovering() or has any open or pending connected remote {@link BTDevice}s.
307 *
308 * If successful, method also changes [this adapter's role](@ref BTAdapterRoles) to ::BTRole::Slave.
309 *
310 * Advertising is active until either disabled via {@link #stopAdvertising()} or a connection has been made, see {@link #isAdvertising()}.
311 *
312 * The given ADV EIR {@link EInfoReport} will be updated with {@link #getName()} and at least {@link GAPFlags.Bit#LE_Gen_Disc} set.
313 *
314 * The given adv_mask and scanrsp_mask will be updated to have at least {@link EIRDataTypeSet.DataType#FLAGS}
315 * and {@link EIRDataTypeSet.DataType#NAME} set in total.
316 *
317 * @param gattServerData_ the {@link DBGattServer} data to be advertised and offered via GattHandler as ::GATTRole::Server.
318 * Its handles will be setup via DBGattServer::setServicesHandles().
319 * Reference is held until next disconnect.
320 * @param eir Full ADV EIR {@link EInfoReport}, will be updated with {@link #getName()} and at least {@link GAPFlags.Bit#LE_Gen_Disc} set.
321 * @param adv_mask {@link EIRDataTypeSet} mask for {@link EInfoReport} to select advertisement EIR PDU data, defaults to {@link EIRDataTypeSet.DataType#FLAGS} | {@link EIRDataTypeSet.DataType#SERVICE_UUID}
322 * @param scanrsp_mask {@link EIRDataTypeSet} mask for {@link EInfoReport} to select scan-response (active scanning) EIR PDU data, defaults to {@link EIRDataTypeSet.DataType#NAME} | {@link EIRDataTypeSet.DataType#CONN_IVAL}
323 * @param adv_interval_min in units of 0.625ms, default value 160 for 100ms; Value range [0x0020 .. 0x4000] for [20ms .. 10.24s]
324 * @param adv_interval_max in units of 0.625ms, default value 480 for 300ms; Value range [0x0020 .. 0x4000] for [20ms .. 10.24s]
325 * @param adv_type see AD_PDU_Type, default 0x00, i.e. ::AD_PDU_Type::ADV_IND
326 * @param adv_chan_map bit 0: chan 37, bit 1: chan 38, bit 2: chan 39, default is 0x07 (all 3 channels enabled)
327 * @param filter_policy 0x00 accepts all PDUs (default), 0x01 only of whitelisted, ...
328 * @return HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state
329 * @see #startAdvertising(DBGattServer, short, short, byte, byte, byte)
330 * @see #stopAdvertising()
331 * @see #isAdvertising()
332 * @see isDiscovering()
333 * @see @ref BTAdapterRoles
334 * @since 2.5.3
335 */
337 final EInfoReport eir,
338 final EIRDataTypeSet adv_mask,
339 final EIRDataTypeSet scanrsp_mask,
340 final short adv_interval_min, final short adv_interval_max,
341 final byte adv_type, final byte adv_chan_map, final byte filter_policy);
342
343 /**
344 * Starts advertising
345 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.53 LE Set Extended Advertising Parameters command (Bluetooth 5.0)
346 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.54 LE Set Extended Advertising Data command (Bluetooth 5.0)
347 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.55 LE Set Extended Scan Response Data command (Bluetooth 5.0)
348 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.56 LE Set Extended Advertising Enable command (Bluetooth 5.0)
349 *
350 * if available, otherwise using
351 *
352 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.5 LE Set Advertising Parameters command
353 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.7 LE Set Advertising Data command
354 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.8 LE Set Scan Response Data command
355 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.9 LE Set Advertising Enable command
356 *
357 * Method fails if isDiscovering() or has any open or pending connected remote {@link BTDevice}s.
358 *
359 * If successful, method also changes [this adapter's role](@ref BTAdapterRoles) to ::BTRole::Slave.
360 *
361 * Advertising is active until either disabled via {@link #stopAdvertising()} or a connection has been made, see {@link #isAdvertising()}.
362 *
363 * The ADV EIR {@link EInfoReport} will be generated on the default
364 * {@link EIRDataTypeSet} adv_mask using {@link EIRDataTypeSet.DataType#FLAGS} | {@link EIRDataTypeSet.DataType#SERVICE_UUID}
365 * and {@link EIRDataTypeSet} scanrsp_mask using scan-response (active scanning) {@link EIRDataTypeSet.DataType#NAME} | {@link EIRDataTypeSet.DataType#CONN_IVAL}.
366 *
367 * @param gattServerData_ the {@link DBGattServer} data to be advertised and offered via GattHandler as ::GATTRole::Server.
368 * Its handles will be setup via DBGattServer::setServicesHandles().
369 * Reference is held until next disconnect.
370 * @param adv_interval_min in units of 0.625ms, default value 160 for 100ms; Value range [0x0020 .. 0x4000] for [20ms .. 10.24s]
371 * @param adv_interval_max in units of 0.625ms, default value 480 for 300ms; Value range [0x0020 .. 0x4000] for [20ms .. 10.24s]
372 * @param adv_type see AD_PDU_Type, default 0x00, i.e. ::AD_PDU_Type::ADV_IND
373 * @param adv_chan_map bit 0: chan 37, bit 1: chan 38, bit 2: chan 39, default is 0x07 (all 3 channels enabled)
374 * @param filter_policy 0x00 accepts all PDUs (default), 0x01 only of whitelisted, ...
375 * @return HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state
376 * @see #startAdvertising(DBGattServer, EInfoReport, EIRDataTypeSet, EIRDataTypeSet, short, short, byte, byte, byte)
377 * @see #startAdvertising(DBGattServer)
378 * @see #stopAdvertising()
379 * @see #isAdvertising()
380 * @see isDiscovering()
381 * @see @ref BTAdapterRoles
382 * @since 2.4.0
383 */
385 final short adv_interval_min, final short adv_interval_max,
386 final byte adv_type, final byte adv_chan_map, final byte filter_policy);
387
388 /**
389 * Starts advertising using all default arguments, see {@link #startAdvertising(short, short, byte, byte, byte)} for details.
390 *
391 * Advertising is active until either disabled via {@link #stopAdvertising()} or a connection has been made, see {@link #isAdvertising()}.
392 *
393 * @param gattServerData_ the {@link DBGattServer} data to be advertised and offered via GattHandler as ::GATTRole::Server.
394 * Its handles will be setup via DBGattServer::setServicesHandles().
395 * Reference is held until next disconnect.
396 * @return HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state
397 * @see #startAdvertising(DBGattServer, EInfoReport, EIRDataTypeSet, EIRDataTypeSet, short, short, byte, byte, byte)
398 * @see #stopAdvertising()
399 * @see #isAdvertising()
400 * @see isDiscovering()
401 * @see @ref BTAdapterRoles
402 * @since 2.4.0
403 */
404 HCIStatusCode startAdvertising(final DBGattServer gattServerData); /* {
405 return startAdvertising(gattServerData, (short)640, (short)640, (byte)0, // AD_PDU_Type::ADV_IND,
406 (byte)0x07, (byte)0x00);
407 } */
408
409 /**
410 * Ends advertising.
411 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.56 LE Set Extended Advertising Enable command (Bluetooth 5.0)
412 *
413 * if available, otherwise using
414 *
415 * - BT Core Spec v5.2: Vol 4 HCI, Part E HCI Functional: 7.8.9 LE Set Advertising Enable command
416 *
417 * Advertising is active until either disabled via {@link #stopAdvertising()} or a connection has been made, see {@link #isAdvertising()}.
418 *
419 * @return HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state
420 * @see #startAdvertising(short, short, byte, byte, byte)
421 * @see #isAdvertising()
422 * @since 2.4.0
423 */
425
426 /**
427 * Returns the adapter's current advertising state. It can be modified through startAdvertising(..) and stopAdvertising().
428 *
429 * Advertising is active until either disabled via {@link #stopAdvertising()} or a connection has been made, see {@link #isAdvertising()}.
430 *
431 * @see #startAdvertising(short, short, byte, byte, byte)
432 * @see #stopAdvertising()
433 * @since 2.4.0
434 */
435 boolean isAdvertising();
436
437 /**
438 * If both types are of {@link BTAdapter}, it compares their {@link BDAddressAndType}, see {@link #getAddressAndType()}.
439 * <p>
440 * {@inheritDoc}
441 * </p>
442 */
443 @Override
444 boolean equals(final Object obj);
445
446 /**
447 * Returns the adapter's public BDAddressAndType.
448 * <p>
449 * The adapter's address as initially reported by the system is always its public address, i.e. {@link BDAddressType#BDADDR_LE_PUBLIC}.
450 * </p>
451 * @since 2.2.8
452 * @see #getVisibleAddressAndType()
453 */
455
456 /**
457 * Returns the adapter's currently visible BDAddressAndType.
458 * <p>
459 * The adapter's address as initially reported by the system is always its public address, i.e. {@link BDAddressType#BDADDR_LE_PUBLIC}.
460 * </p>
461 * <p>
462 * The adapter's visible BDAddressAndType might be set to {@link BDAddressType#BDADDR_LE_RANDOM} before scanning / discovery mode (TODO).
463 * </p>
464 * @since 2.2.8
465 * @see #getAddressAndType()
466 */
468
469 /**
470 * Returns the BluetoothAdapter's internal temporary device id
471 * <p>
472 * The internal device id is constant across the adapter lifecycle,
473 * but may change after its destruction.
474 * </p>
475 * @since 2.0.0
476 */
477 int getDevID();
478
479 /** Returns the Bluetooth major version of this adapter. Currently either `4` or `5`. */
481
482 /**
483 * Returns the name.
484 * <p>
485 * Can be changed via {@link #setName(String, String)} while powered-off.
486 * </p>
487 * @see #setName(String, String)
488 */
489 String getName();
490
491 /**
492 * Returns the short name.
493 * <p>
494 * Can be changed via {@link #setName(String, String)} while powered-off.
495 * </p>
496 * @see #setName(String, String)
497 * @since 2.4.0
498 */
499 String getShortName();
500
501 /**
502 * Sets the name and short-name.
503 *
504 * The corresponding management event will change the name and short-name.
505 *
506 * Shall be called while adapter is powered off, see {@link #setPowered(boolean)}.
507 * If adapter is powered, method returns {@link HCIStatusCode#COMMAND_DISALLOWED}.
508 *
509 * @param name
510 * @param short_name
511 * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
512 * @see #getName()
513 * @see #getShortName()
514 * @see #setPowered(boolean)
515 * @since 2.4.0
516 */
517 HCIStatusCode setName(String name, String short_name);
518
519 /**
520 * Returns whether the adapter is valid, plugged in and powered.
521 * @return true if {@link #isValid()}, HCI channel open and {@link AdapterSettings.SettingType#POWERED POWERED} state is set.
522 * @see #isSuspended()
523 * @see #isValid()
524 * @since 2.0.0
525 */
526 boolean isPowered();
527
528 /**
529 * Returns whether the adapter is suspended, i.e. valid and plugged in, but not powered.
530 * @return true if {@link #isValid()}, HCI channel open and {@link AdapterSettings.SettingType#POWERED POWERED} state is not set.
531 * @see #isPowered()
532 * @see #isValid()
533 */
534 boolean isSuspended();
535
536 /**
537 * Returns whether the adapter is valid, i.e. reference is valid, plugged in and generally operational,
538 * but not necessarily {@link #isPowered()}.
539 * @return true if this adapter references are valid and hadn't been {@link #close()}'ed
540 * @see #isPowered()
541 * @see #isSuspended()
542 * @since 2.0.0
543 */
544 boolean isValid();
545
546 /**
547 * Return {@link LE_Features} for this controller.
548 * <pre>
549 * BT Core Spec v5.2: Vol 6, Part B, 4.6 (LE LL) Feature Support
550 * </pre>
551 * @since 2.4.0
552 */
554
555 /**
556 * Returns the power state the adapter.
557 * <p>
558 * Consider using {@link #isPowered()}
559 * </p>
560 * @return The power state of the adapter.
561 * @since 2.0.0 Renamed from getPowered() to getPoweredState()
562 * @see #isPowered()
563 * @see #isSuspended()
564 * @see #isValid()
565 */
567
568 /**
569 * Sets the power state the adapter.
570 *
571 * In case current power state is already as desired, method will not change the power state.
572 *
573 * @param power_on true will power on this adapter if it is powered-off and vice versa.
574 * @return true if successfully powered-on, -off or unchanged, false on failure
575 *
576 * @apiNote return value boolean since 2.0.0
577 * @see #close()
578 * @see #initialize(BTMode)
579 * @see #isInitialized()
580 * @since 2.0.0
581 */
582 boolean setPowered(final boolean power_on);
583
584 /**
585 * Toggle adapter privacy address mode, i.e. resolvable random address including IRK.
586 * @param enable toggle to enable or disable (default)
587 * @return HCIStatusCode::SUCCESS or an error state on failure
588 * @since 3.2.0
589 */
590 HCIStatusCode setPrivacy(boolean enable);
591
592 /**
593 * Returns whether Secure Connections (SC) is enabled.
594 * @see #setSecureConnections(boolean)
595 * @since 2.4.0
596 */
598
599 /**
600 * Enable or disable Secure Connections (SC) of the adapter.
601 *
602 * By default, Secure Connections (SC) is enabled if supported.
603 *
604 * Shall be called while adapter is powered off, see {@link #setPowered(boolean)}.
605 * If adapter is powered, method returns {@link HCIStatusCode#COMMAND_DISALLOWED}.
606 *
607 * @param enable
608 * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
609 * @see #getSecureConnectionsEnabled()
610 * @see #setPowered(boolean)
611 * @since 2.4.0
612 */
613 HCIStatusCode setSecureConnections(final boolean enable);
614
615 /**
616 * Set default connection parameter of incoming connections for this adapter when in server mode, i.e. BTRole::Slave.
617 *
618 * In case the incoming connection's parameter don't lie within the given default values,
619 * a reconnect is being requested.
620 *
621 * Shall be called while adapter is powered off, see setPowered().
622 * If adapter is powered, method returns HCIStatusCode::COMMAND_DISALLOWED.
623 *
624 * BlueZ/Linux LE connection defaults are
625 * - conn_interval_min 24 -> 30ms
626 * - conn_interval_max 40 -> 50ms
627 * - conn_latency 0
628 * - supervision_timeout 42 -> 420ms
629 *
630 * Supported on GNU/Linux since kernel 5.9.
631 *
632 * @param dev_id
633 * @param conn_interval_min in units of 1.25ms, default value 8 for 10ms; Value range [6 .. 3200] for [7.5ms .. 4000ms].
634 * @param conn_interval_max in units of 1.25ms, default value 40 for 50ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
635 * @param conn_latency slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3].
636 * @param supervision_timeout in units of 10ms, default value 500ms >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s].
637 * @return HCIStatusCode::SUCCESS or an error state on failure
638 * @see setPowered()
639 * @since 2.5.3
640 */
641 HCIStatusCode setDefaultConnParam(final short conn_interval_min, final short conn_interval_max,
642 final short conn_latency, final short supervision_timeout);
643
644
645 /**
646 * Sets the given ::BTSecurityLevel and ::SMPIOCapability for connecting device when in server (peripheral) mode, see [adapter's role](@ref BTAdapterRoles).
647 * <p>
648 * Method either changes both parameter for the upcoming connection or none at all.
649 * </p>
650 * @param[in] sec_level ::BTSecurityLevel to be applied.
651 * @param[in] io_cap ::SMPIOCapability to be applied.
652 * @see BTSecurityLevel
653 * @see SMPIOCapability
654 * @see BTDevice#setConnSecurity(BTSecurityLevel, SMPIOCapability)
655 * @see #startAdvertising(DBGattServer, EInfoReport, EIRDataTypeSet, EIRDataTypeSet, short, short, byte, byte, byte)
656 */
657 void setServerConnSecurity(final BTSecurityLevel sec_level, final SMPIOCapability io_cap);
658
659 /**
660 * Set the adapter's persistent storage directory for {@link SMPKeyBin} files.
661 * - if set, all {@link SMPKeyBin} instances will be managed and persistent.
662 * - if not set, all {@link SMPKeyBin} instances will be transient only.
663 *
664 * When called, all keys within the path will be loaded,
665 * i.e. issuing {@link BTDevice#uploadKeys(SMPKeyBin, BTSecurityLevel) for all keys belonging to this BTAdapter.
666 *
667 * Persistent {@link SMPKeyBin} management is only functional when {@link BTAdapter} is in {@link BTRole#Slave} peripheral mode.
668 *
669 * For each {@link SMPKeyBin} file one shared {@link BTDevice} in {@link BTRole#Master} will be instantiated
670 * when uploadKeys() is called.
671 *
672 * @param path persistent storage path to {@link SMPKeyBin} files
673 * @see BTDevice#uploadKeys(SMPKeyBin, BTSecurityLevel)
674 */
675 void setSMPKeyPath(final String path);
676
677 /**
678 * Initialize the adapter with default values, including power-on.
679 * <p>
680 * Method shall be issued on the desired adapter found via {@link BTManager.ChangedAdapterSetListener}.
681 * </p>
682 * <p>
683 * While initialization, the adapter is first powered-off, setup and then powered-on.
684 * </p>
685 * <p>
686 * Calling the method will allow close() to power-off the adapter,
687 * if not powered on before.
688 * </p>
689 * @param btMode the desired adapter's {@link BTMode}, default shall be {@link BTMode#DUAL}
690 * @param powerOn true to leave adapter powered-on, otherwise leave it off *
691 * @return {@link HCIStatusCode#SUCCESS} or an error state on failure (e.g. power-on)
692 * @see #isInitialized()
693 * @see #initialize()
694 * @see #close()
695 * @see #setPowered(boolean)
696 * @since 3.2.0
697 */
698 HCIStatusCode initialize(final BTMode btMode, boolean powerOn);
699
700 /**
701 * Returns true, if {@link #initialize(BTMode)} has already been called for this adapter, otherwise false.
702 *
703 * @see #initialize(BTMode)
704 * @since 2.4.0
705 */
706 boolean isInitialized();
707
708 /**
709 * Reset the adapter.
710 * <p>
711 * The semantics are specific to the HCI host implementation,
712 * however, it shall comply at least with the HCI Reset command
713 * and bring up the device from standby into a POWERED functional state afterwards.
714 * </p>
715 * <pre>
716 * BT Core Spec v5.2: Vol 4, Part E HCI: 7.3.2 Reset command
717 * </pre>
718 * @since 2.0.0
719 */
721
722 /**
723 * Sets default preference of LE_PHYs.
724 *
725 * BT Core Spec v5.2: Vol 4, Part E, 7.8.49 LE Set PHY command
726 *
727 * @param Tx transmitter LE_PHYs bit mask of preference if not set to zero {@link LE_PHYs#mask} (ignored).
728 * @param Rx receiver LE_PHYs bit mask of preference if not set to zero {@link LE_PHYs#mask} (ignored).
729 * @return
730 * @see BTDevice#getTxPhys()
731 * @see BTDevice#getRxPhys()
732 * @see BTDevice#getConnectedLE_PHY(LE_PHYs[], LE_PHYs[])
733 * @see BTDevice#setConnectedLE_PHY(LE_PHYs, LE_PHYs)
734 * @since 2.4.0
735 */
737
738 /**
739 * This method connects to device without need of
740 * performing General Discovery. Connection mechanism is
741 * similar to Connect method from Device1 interface with
742 * exception that this method returns success when physical
743 * connection is established. After this method returns,
744 * services discovery will continue and any supported
745 * profile will be connected. There is no need for calling
746 * Connect on Device1 after this call. If connection was
747 * successful this method returns object path to created
748 * device object.
749 *
750 * @param address The Bluetooth device address of the remote
751 * device. This parameter is mandatory.
752 * @param addressType The Bluetooth device Address Type. This is
753 * address type that should be used for initial
754 * connection. If this parameter is not present
755 * BR/EDR device is created.
756 * Possible values:
757 * <ul>
758 * <li>{@code public} - Public address</li>
759 * <li>{@code random} - Random address</li>
760 * </ul>
761 */
763
764 /**
765 * Returns the current meta discovering {@link ScanType}.
766 * It can be modified through {@link #startDiscovery(DiscoveryPolicy, boolean)} and {@link #stopDiscovery()}.
767 * <p>
768 * Note that if {@link #startDiscovery(DiscoveryPolicy, boolean)} has been issued with keepAlive==true,
769 * the meta {@link ScanType} will still keep the desired {@link ScanType} enabled
770 * even if it has been temporarily disabled.
771 * </p>
772 * @see #startDiscovery(boolean)
773 * @see #stopDiscovery()
774 * @since 2.0.0
775 */
777
778 /**
779 * Returns true if the meta discovering state is not {@link ScanType#NONE}.
780 * It can be modified through {@link #startDiscovery(DiscoveryPolicy, boolean)} and {@link #stopDiscovery()}.
781 * @see startDiscovery()
782 * @see stopDiscovery()
783 * @since 2.4.0
784 */
785 boolean isDiscovering();
786
787 /**
788 * Add the given {@link AdapterStatusListener} to the list if not already present.
789 * <p>
790 * In case the {@link AdapterStatusListener}'s lifecycle and event delivery
791 * shall be constrained to this device, please use
792 * {@link BTDevice#addStatusListener(AdapterStatusListener)}.
793 * </p>
794 * <p>
795 * The newly added {@link AdapterStatusListener} will receive an initial
796 * {@link AdapterStatusListener#adapterSettingsChanged(BTAdapter, AdapterSettings, AdapterSettings, AdapterSettings, long) adapterSettingsChanged}
797 * event, passing an {@link AdapterSettings empty oldMask and changedMask}, as well as {@link AdapterSettings current newMask}. <br>
798 * This allows the receiver to be aware of this adapter's current settings.
799 * </p>
800 * @param listener A {@link AdapterStatusListener} instance
801 * @return true if the given listener is not element of the list and has been newly added, otherwise false.
802 * @since 2.3.0
803 * @see {@link BTDevice#addStatusListener(AdapterStatusListener)}
804 * @see {@link #removeStatusListener(AdapterStatusListener)}
805 * @see {@link #removeAllStatusListener()}
806 */
808
809 /**
810 * Remove the given {@link AdapterStatusListener} from the list.
811 * @param listener A {@link AdapterStatusListener} instance
812 * @return true if the given listener is an element of the list and has been removed, otherwise false.
813 * @since 2.0.0
814 */
816
817 /**
818 * Remove all {@link AdapterStatusListener} from the list.
819 * @return number of removed listener.
820 * @since 2.0.0
821 */
823
824 /**
825 * Return the user's DBGattServer shared reference if in {@link BTRole#Slave} mode
826 * as set via {@link #startAdvertising(DBGattServer) and valid until subsequent disconnect.
827 *
828 * Returns nullptr if in {@link BTRole#Master} mode.
829 */
831
832 /**
833 * Print the internally maintained BTDevice lists to stderr:
834 * - sharedDevices
835 * - connectedDevice
836 * - discoveredDevices
837 * - StatusListener
838 *
839 * This is intended as a debug facility.
840 * @since 2.3.0
841 */
843
844 @Override
845 String toString();
846}
BTAdapter status listener for remote BTDevice discovery events: Added, updated and removed; as well a...
Unique Bluetooth EUI48 address and BDAddressType tuple.
Representing a complete list of Gatt Service objects from the GATT server perspective,...
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
Collection of 'Extended Advertising Data' (EAD), 'Advertising Data' (AD) or 'Extended Inquiry Respons...
LE Link Layer Feature Set (bitmask)
LE Transport PHY bit values (bitmask)
Definition: LE_PHYs.java:36
Bluetooth adapter operating mode.
Definition: BTMode.java:34
Bluetooth roles from the perspective of the link layer (connection initiator).
Definition: BTRole.java:36
Bluetooth Security Level.
Discovery policy defines the BTAdapter discovery mode after connecting a remote BTDevice:
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
SMP IO Capability value.
Meta ScanType as derived from BTMode with defined value mask consisting of BDAddressType bits.
Definition: ScanType.java:36
BTAdapter represents one local Bluetooth Controller.
Definition: BTAdapter.java:48
boolean addDeviceToWhitelist(final BDAddressAndType addressAndType, final HCIWhitelistConnectType ctype, final short conn_interval_min, final short conn_interval_max, final short conn_latency, final short timeout)
Add the given device to the adapter's autoconnect whitelist.
boolean addDeviceToWhitelist(final BDAddressAndType addressAndType, final HCIWhitelistConnectType ctype)
Add the given device to the adapter's autoconnect whitelist.
HCIStatusCode stopDiscovery()
Turns off device discovery if it is enabled.
HCIStatusCode startDiscovery()
Starts discovery using all default arguments, see startDiscovery(DiscoveryPolicy, boolean,...
BTManager getManager()
Returns the used singleton BTManager instance, used to create this adapter.
int removeDiscoveredDevices()
Remove all the discovered devices found on this adapter.
boolean isInitialized()
Returns true, if initialize(BTMode) has already been called for this adapter, otherwise false.
HCIStatusCode reset()
Reset the adapter.
boolean removeDiscoveredDevice(final BDAddressAndType addressAndType)
Discards matching discovered devices.
LE_Features getLEFeatures()
Return LE_Features for this controller.
boolean setPowered(final boolean power_on)
Sets the power state the adapter.
DBGattServer getGATTServerData()
Return the user's DBGattServer shared reference if in BTRole#Slave mode as set via and valid until su...
BTDevice find(String name, BDAddressAndType addressAndType, long timeoutMS)
Find a BluetoothDevice.
BTMode getBTMode()
Returns the current BTMode of this adapter.
boolean isDeviceWhitelisted(final BDAddressAndType addressAndType)
Returns true, if the adapter's device is already whitelisted.
boolean equals(final Object obj)
If both types are of BTAdapter, it compares their BDAddressAndType, see getAddressAndType().
HCIStatusCode startAdvertising(final DBGattServer gattServerData, final EInfoReport eir, final EIRDataTypeSet adv_mask, final EIRDataTypeSet scanrsp_mask, final short adv_interval_min, final short adv_interval_max, final byte adv_type, final byte adv_chan_map, final byte filter_policy)
Starts advertising.
HCIStatusCode setSecureConnections(final boolean enable)
Enable or disable Secure Connections (SC) of the adapter.
boolean removeDeviceFromWhitelist(final BDAddressAndType addressAndType)
Remove the given device from the adapter's autoconnect whitelist.
String getName()
Returns the name.
int removeAllStatusListener()
Remove all AdapterStatusListener from the list.
void setSMPKeyPath(final String path)
Set the adapter's persistent storage directory for SMPKeyBin files.
void setServerConnSecurity(final BTSecurityLevel sec_level, final SMPIOCapability io_cap)
Sets the given ::BTSecurityLevel and ::SMPIOCapability for connecting device when in server (peripher...
int getDevID()
Returns the BluetoothAdapter's internal temporary device id.
boolean isSuspended()
Returns whether the adapter is suspended, i.e.
boolean isValid()
Returns whether the adapter is valid, i.e.
List< BTDevice > getDiscoveredDevices()
Returns a list of discovered BluetoothDevices from this adapter.
boolean getPoweredState()
Returns the power state the adapter.
BDAddressAndType getVisibleAddressAndType()
Returns the adapter's currently visible BDAddressAndType.
boolean addStatusListener(final AdapterStatusListener listener)
Add the given AdapterStatusListener to the list if not already present.
ScanType getCurrentScanType()
Returns the current meta discovering ScanType.
HCIStatusCode setDefaultConnParam(final short conn_interval_min, final short conn_interval_max, final short conn_latency, final short supervision_timeout)
Set default connection parameter of incoming connections for this adapter when in server mode,...
HCIStatusCode setDefaultLE_PHY(final LE_PHYs Tx, final LE_PHYs Rx)
Sets default preference of LE_PHYs.
BDAddressAndType getAddressAndType()
Returns the adapter's public BDAddressAndType.
HCIStatusCode setPrivacy(boolean enable)
Toggle adapter privacy address mode, i.e.
boolean removeDevicePausingDiscovery(final BTDevice device)
Manual DiscoveryPolicy intervention point, allowing user to remove the ready device from the queue of...
BTDevice connectDevice(BDAddressAndType addressAndType)
This method connects to device without need of performing General Discovery.
HCIStatusCode setName(String name, String short_name)
Sets the name and short-name.
boolean removeStatusListener(final AdapterStatusListener l)
Remove the given AdapterStatusListener from the list.
boolean isAdvertising()
Returns the adapter's current advertising state.
DiscoveryPolicy getCurrentDiscoveryPolicy()
Return the current DiscoveryPolicy, set via startDiscovery(DiscoveryPolicy, boolean,...
boolean isPowered()
Returns whether the adapter is valid, plugged in and powered.
boolean isDiscovering()
Returns true if the meta discovering state is not ScanType#NONE.
BTRole getRole()
Return the current BTRole of this adapter.
void printDeviceLists()
Print the internally maintained BTDevice lists to stderr:
HCIStatusCode stopAdvertising()
Ends advertising.
BTDevice find(String name, BDAddressAndType addressAndType)
Find a BluetoothDevice.
boolean getSecureConnectionsEnabled()
Returns whether Secure Connections (SC) is enabled.
int getBTMajorVersion()
Returns the Bluetooth major version of this adapter.
HCIStatusCode initialize(final BTMode btMode, boolean powerOn)
Initialize the adapter with default values, including power-on.
String getShortName()
Returns the short name.
BTDevice represents one remote Bluetooth device.
Definition: BTDevice.java:47
A thread safe singleton handler of the BTAdapter manager, e.g.
Definition: BTManager.java:36