Direct-BT  2.3.1
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  * 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.UUID;
33 
34 /**
35  * Provides access to Bluetooth adapters.
36  *
37  * @see [Bluetooth Specification](https://www.bluetooth.com/specifications/bluetooth-core-specification/)
38  * @see [BlueZ adapter API](http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/adapter-api.txt)
39  */
40 public interface BTAdapter extends BTObject
41 {
42  /**
43  * Returns the used singleton {@link BTManager} instance, used to create this adapter.
44  */
46 
47  /** Find a BluetoothDevice. If parameters name and address are not null,
48  * the returned object will have to match them.
49  * It will first check for existing objects. It will not turn on discovery
50  * or connect to devices.
51  * @parameter name optionally specify the name of the object you are
52  * waiting for
53  * @parameter address optionally specify the MAC address of the device you are
54  * waiting for
55  * @parameter timeoutMS the function will return after timeout time in milliseconds, a
56  * value of zero means wait forever. If object is not found during this time null will be returned.
57  * @return An object matching the name and address or null if not found before
58  * timeout expires.
59  */
60  public BTDevice find(String name, BDAddressAndType addressAndType, long timeoutMS);
61 
62  /** Find a BluetoothDevice. If parameters name and address are not null,
63  * the returned object will have to match them.
64  * It will first check for existing objects. It will not turn on discovery
65  * or connect to devices.
66  * @parameter name optionally specify the name of the object you are
67  * waiting for
68  * @parameter address optionally specify the MAC address of the device you are
69  * waiting for
70  * @return An object matching the name and address.
71  */
72  public BTDevice find(String name, BDAddressAndType addressAndType);
73 
74  /* Bluetooth specific API */
75 
76  /**
77  * Returns true, if the adapter's device is already whitelisted.
78  * @since 2.0.0
79  */
80  public boolean isDeviceWhitelisted(final BDAddressAndType addressAndType);
81 
82  /**
83  * Add the given device to the adapter's autoconnect whitelist.
84  * <p>
85  * The given LE connection parameter will be uploaded to the kernel for the given device first,
86  * if the device is of type {@link BDAddressType#BDADDR_LE_PUBLIC} or {@link BDAddressType#BDADDR_LE_RANDOM}.
87  * </p>
88  * <p>
89  * Method will reject duplicate devices, in which case it should be removed first.
90  * </p>
91  *
92  * @param address
93  * @param address_type
94  * @param ctype
95  * @param conn_interval_min default value 0x000F
96  * @param conn_interval_max default value 0x000F
97  * @param conn_latency default value 0x0000
98  * @param timeout in units of 10ms, default value 1000 for 10000ms or 10s.
99  * @return {@code true} if successful, otherwise {@code false}.
100  *
101  * @see #addDeviceToWhitelist(String, BDAddressType, HCIWhitelistConnectType)
102  * @since 2.0.0
103  */
104  public boolean addDeviceToWhitelist(final BDAddressAndType addressAndType,
105  final HCIWhitelistConnectType ctype,
106  final short conn_interval_min, final short conn_interval_max,
107  final short conn_latency, final short timeout);
108 
109  /**
110  * Add the given device to the adapter's autoconnect whitelist.
111  * <p>
112  * This variant of {@link #addDeviceToWhitelist(String, BDAddressType, HCIWhitelistConnectType, short, short, short, short)}
113  * uses default connection parameter, which will be uploaded to the kernel for the given device first.
114  * </p>
115  * <p>
116  * Method will reject duplicate devices, in which case it should be removed first.
117  * </p>
118  *
119  * @param address
120  * @param address_type
121  * @param ctype
122  * @return {@code true} if successful, otherwise {@code false}.
123  *
124  * @see #addDeviceToWhitelist(String, BDAddressType, HCIWhitelistConnectType, short, short, short, short)
125  * @since 2.0.0
126  */
127  public boolean addDeviceToWhitelist(final BDAddressAndType addressAndType,
128  final HCIWhitelistConnectType ctype);
129 
130 
131  /**
132  * Remove the given device from the adapter's autoconnect whitelist.
133  * @since 2.0.0
134  */
135  public boolean removeDeviceFromWhitelist(final BDAddressAndType addressAndType);
136 
137 
138  /**
139  * Turns on device discovery if it is disabled.
140  * <p>
141  * {@code jau.direct_bt}'s implementation will always issue {@link #removeDiscoveredDevices()},
142  * ensuring all scanned devices will be found after calling this method. Regardless whether
143  * discovery is already running.
144  * </p>
145  * <pre>
146  * + --+-------+--------+-----------+----------------------------------------------------+
147  * | # | meta | native | keepAlive | Note
148  * +---+-------+--------+-----------+----------------------------------------------------+
149  * | 1 | true | true | false | -
150  * | 2 | false | false | false | -
151  * +---+-------+--------+-----------+----------------------------------------------------+
152  * | 3 | true | true | true | -
153  * | 4 | true | false | true | temporarily disabled -> startDiscoveryBackground()
154  * | 5 | false | false | true | [4] -> [5] requires manual DISCOVERING event
155  * +---+-------+--------+-----------+----------------------------------------------------+
156  * </pre>
157  *
158  * @param keepAlive if {@code true}, indicates that discovery shall be restarted
159  * if stopped by the underlying Bluetooth implementation (BlueZ, ..).
160  * Using {@link #startDiscovery(boolean) startDiscovery}({@code keepAlive=true})
161  * and {@link #stopDiscovery()} is the recommended workflow
162  * for a reliable discovery process.
163  * @param le_scan_active true enables delivery of active scanning PDUs, otherwise no scanning PDUs shall be sent (default)
164  * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
165  * @throws BTException
166  * @since 2.0.0
167  * @since 2.2.8
168  * @see #startDiscovery(boolean, boolean, int, int, byte)
169  * @see #getDiscovering()
170  */
171  public HCIStatusCode startDiscovery(final boolean keepAlive, final boolean le_scan_active) throws BTException;
172 
173  /**
174  * Shares same implementation as {@link #startDiscovery(boolean, boolean)}, but allows setting custom scan values.
175  * @param keepAlive
176  * @param le_scan_active true enables delivery of active scanning PDUs, otherwise no scanning PDUs shall be sent (default)
177  * @param le_scan_interval in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]
178  * @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
179  * @param filter_policy 0x00 accepts all PDUs (default), 0x01 only of whitelisted, ...
180  * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
181  * @throws BTException
182  * @since 2.2.8
183  * @see #startDiscovery(boolean, boolean)
184  * @see #getDiscovering()
185  */
186  public HCIStatusCode startDiscovery(final boolean keepAlive, final boolean le_scan_active,
187  final short le_scan_interval, final short le_scan_window,
188  final byte filter_policy) throws BTException;
189 
190  /**
191  * Turns off device discovery if it is enabled.
192  * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
193  * @apiNote return {@link HCIStatusCode} since 2.0.0
194  * @since 2.0.0
195  */
197 
198  /** Returns a list of discovered BluetoothDevices from this adapter.
199  * @return A list of discovered BluetoothDevices on this adapter,
200  * NULL if an error occurred
201  */
203 
204  /**
205  * Remove all the discovered devices found on this adapter.
206  *
207  * @return The number of removed discovered devices on this adapter
208  * @throws BTException
209  * @since 2.2.0
210  * @implNote Changed from 'removeDiscoveredDevices()' for clarity since version 2.2.0
211  */
213 
214  /**
215  * Discards matching discovered devices.
216  * @return {@code true} if found and removed, otherwise false.
217  * @since 2.2.0
218  */
219  public boolean removeDiscoveredDevice(final BDAddressAndType addressAndType);
220 
221  /* D-Bus property accessors: */
222 
223  /**
224  * Returns the adapter's public BDAddressAndType.
225  * <p>
226  * The adapter's address as initially reported by the system is always its public address, i.e. {@link BDAddressType#BDADDR_LE_PUBLIC}.
227  * </p>
228  * @since 2.2.8
229  * @see #getVisibleAddressAndType()
230  */
232 
233  /**
234  * Returns the adapter's currently visible BDAddressAndType.
235  * <p>
236  * The adapter's address as initially reported by the system is always its public address, i.e. {@link BDAddressType#BDADDR_LE_PUBLIC}.
237  * </p>
238  * <p>
239  * The adapter's visible BDAddressAndType might be set to {@link BDAddressType#BDADDR_LE_RANDOM} before scanning / discovery mode (TODO).
240  * </p>
241  * @since 2.2.8
242  * @see #getAddressAndType()
243  */
245 
246  /** Returns the system name of this adapter.
247  * @return The system name of this adapter.
248  */
249  public String getName();
250 
251  /**
252  * Returns the BluetoothAdapter's internal temporary device id
253  * <p>
254  * The internal device id is constant across the adapter lifecycle,
255  * but may change after its destruction.
256  * </p>
257  * @since 2.0.0
258  */
259  public int getDevID();
260 
261  /** Returns the friendly name of this adapter.
262  * @return The friendly name of this adapter, or NULL if not set.
263  */
264  public String getAlias();
265 
266  /** Sets the friendly name of this adapter.
267  */
268  public void setAlias(String value);
269 
270  /**
271  * Returns whether the adapter is valid, plugged in and powered.
272  * @return true if {@link #isValid()}, HCI channel open and {@link AdapterSettings.SettingType#POWERED POWERED} state is set.
273  * @see #isSuspended()
274  * @see #isValid()
275  * @since 2.0.0
276  */
277  public boolean isPowered();
278 
279  /**
280  * Returns whether the adapter is suspended, i.e. valid and plugged in, but not powered.
281  * @return true if {@link #isValid()}, HCI channel open and {@link AdapterSettings.SettingType#POWERED POWERED} state is not set.
282  * @see #isPowered()
283  * @see #isValid()
284  */
285  public boolean isSuspended();
286 
287  /**
288  * Returns whether the adapter is valid, i.e. reference is valid, plugged in and generally operational,
289  * but not necessarily {@link #isPowered()}.
290  * @return true if this adapter references are valid and hadn't been {@link #close()}'ed
291  * @see #isPowered()
292  * @see #isSuspended()
293  * @since 2.0.0
294  */
295  public boolean isValid();
296 
297  /**
298  * Returns the power state the adapter.
299  * <p>
300  * Consider using {@link #isPowered()}
301  * </p>
302  * @return The power state of the adapter.
303  * @since 2.0.0 Renamed from getPowered() to getPoweredState()
304  * @see #isPowered()
305  * @see #isSuspended()
306  * @see #isValid()
307  */
308  public boolean getPoweredState();
309 
310  /**
311  * Sets the power state the adapter.
312  * @apiNote return value boolean since 2.0.0
313  * @since 2.0.0
314  */
315  public boolean setPowered(boolean value);
316 
317  /**
318  * Reset the adapter.
319  * <p>
320  * The semantics are specific to the HCI host implementation,
321  * however, it shall comply at least with the HCI Reset command
322  * and bring up the device from standby into a POWERED functional state afterwards.
323  * </p>
324  * <pre>
325  * BT Core Spec v5.2: Vol 4, Part E HCI: 7.3.2 Reset command
326  * </pre>
327  * @since 2.0.0
328  */
330 
331  /** Returns the discoverable state the adapter.
332  * @return The discoverable state of the adapter.
333  */
334  public boolean getDiscoverable();
335 
336  /**
337  * Sets the discoverable state the adapter.
338  * @apiNote return value boolean since 2.0.0
339  * @since 2.0.0
340  */
341  public boolean setDiscoverable(boolean value);
342 
343  /**
344  * This method connects to device without need of
345  * performing General Discovery. Connection mechanism is
346  * similar to Connect method from Device1 interface with
347  * exception that this method returns success when physical
348  * connection is established. After this method returns,
349  * services discovery will continue and any supported
350  * profile will be connected. There is no need for calling
351  * Connect on Device1 after this call. If connection was
352  * successful this method returns object path to created
353  * device object.
354  *
355  * @param address The Bluetooth device address of the remote
356  * device. This parameter is mandatory.
357  * @param addressType The Bluetooth device Address Type. This is
358  * address type that should be used for initial
359  * connection. If this parameter is not present
360  * BR/EDR device is created.
361  * Possible values:
362  * <ul>
363  * <li>{@code public} - Public address</li>
364  * <li>{@code random} - Random address</li>
365  * </ul>
366  */
367  public BTDevice connectDevice(BDAddressAndType addressAndType);
368 
369  /**
370  * Sets the discoverable state the adapter.
371  * @apiNote return value boolean since 2.0.0
372  * @since 2.0.0
373  */
374  public boolean setPairable(boolean value);
375 
376  /**
377  * Returns the current meta discovering {@link ScanType}.
378  * It can be modified through {@link #startDiscovery(boolean, boolean)} and {@link #stopDiscovery()}.
379  * <p>
380  * Note that if {@link #startDiscovery(boolean, boolean)} has been issued with keepAlive==true,
381  * the meta {@link ScanType} will still keep the desired {@link ScanType} enabled
382  * even if it has been temporarily disabled.
383  * </p>
384  * @see #startDiscovery(boolean)
385  * @see #stopDiscovery()
386  * @since 2.0.0
387  */
389 
390  /**
391  * Add the given {@link AdapterStatusListener} to the list if not already present.
392  * <p>
393  * In case the {@link AdapterStatusListener}'s lifecycle and event delivery
394  * shall be constrained to this device, please use
395  * {@link BTDevice#addStatusListener(AdapterStatusListener)}.
396  * </p>
397  * <p>
398  * The newly added {@link AdapterStatusListener} will receive an initial
399  * {@link AdapterStatusListener#adapterSettingsChanged(BTAdapter, AdapterSettings, AdapterSettings, AdapterSettings, long) adapterSettingsChanged}
400  * event, passing an {@link AdapterSettings empty oldMask and changedMask}, as well as {@link AdapterSettings current newMask}. <br>
401  * This allows the receiver to be aware of this adapter's current settings.
402  * </p>
403  * @param listener A {@link AdapterStatusListener} instance
404  * @return true if the given listener is not element of the list and has been newly added, otherwise false.
405  * @since 2.3.0
406  * @see {@link BTDevice#addStatusListener(AdapterStatusListener)}
407  * @see {@link #removeStatusListener(AdapterStatusListener)}
408  * @see {@link #removeAllStatusListener()}
409  */
410  public boolean addStatusListener(final AdapterStatusListener listener);
411 
412  /**
413  * Remove the given {@link AdapterStatusListener} from the list.
414  * @param listener A {@link AdapterStatusListener} instance
415  * @return true if the given listener is an element of the list and has been removed, otherwise false.
416  * @since 2.0.0
417  */
419 
420  /**
421  * Remove all {@link AdapterStatusListener} from the list.
422  * @return number of removed listener.
423  * @since 2.0.0
424  */
426 
427  /**
428  * Print the internally maintained BTDevice lists to stderr:
429  * - sharedDevices
430  * - connectedDevice
431  * - discoveredDevices
432  * - StatusListener
433  *
434  * This is intended as a debug facility.
435  * @since 2.3.0
436  */
437  public void printDeviceLists();
438 
439 }
org.direct_bt.BTAdapter.setDiscoverable
boolean setDiscoverable(boolean value)
Sets the discoverable state the adapter.
org.direct_bt.BTAdapter.isDeviceWhitelisted
boolean isDeviceWhitelisted(final BDAddressAndType addressAndType)
Returns true, if the adapter's device is already whitelisted.
org.direct_bt.BTAdapter.connectDevice
BTDevice connectDevice(BDAddressAndType addressAndType)
This method connects to device without need of performing General Discovery.
org.direct_bt.BTAdapter
Provides access to Bluetooth adapters.
Definition: BTAdapter.java:41
org.direct_bt.BTAdapter.printDeviceLists
void printDeviceLists()
Print the internally maintained BTDevice lists to stderr:
org.direct_bt.BTAdapter.isPowered
boolean isPowered()
Returns whether the adapter is valid, plugged in and powered.
org.direct_bt.BTAdapter.reset
HCIStatusCode reset()
Reset the adapter.
org.direct_bt.BTAdapter.getAlias
String getAlias()
Returns the friendly name of this adapter.
org.direct_bt.BTAdapter.removeDeviceFromWhitelist
boolean removeDeviceFromWhitelist(final BDAddressAndType addressAndType)
Remove the given device from the adapter's autoconnect whitelist.
org.direct_bt.HCIWhitelistConnectType
HCI Whitelist connection type.
Definition: HCIWhitelistConnectType.java:32
org.direct_bt.BTAdapter.getName
String getName()
Returns the system name of this adapter.
org.direct_bt.BTAdapter.find
BTDevice find(String name, BDAddressAndType addressAndType)
Find a BluetoothDevice.
org.direct_bt.BTAdapter.startDiscovery
HCIStatusCode startDiscovery(final boolean keepAlive, final boolean le_scan_active, final short le_scan_interval, final short le_scan_window, final byte filter_policy)
Shares same implementation as startDiscovery(boolean, boolean), but allows setting custom scan values...
org.direct_bt.BTAdapter.find
BTDevice find(String name, BDAddressAndType addressAndType, long timeoutMS)
Find a BluetoothDevice.
org.direct_bt.BTAdapter.isValid
boolean isValid()
Returns whether the adapter is valid, i.e.
org.direct_bt.BTAdapter.removeDiscoveredDevices
int removeDiscoveredDevices()
Remove all the discovered devices found on this adapter.
org.direct_bt.BTAdapter.removeAllStatusListener
int removeAllStatusListener()
Remove all AdapterStatusListener from the list.
org.direct_bt.BTManager
Definition: BTManager.java:34
org.direct_bt.BTAdapter.getDevID
int getDevID()
Returns the BluetoothAdapter's internal temporary device id.
org.direct_bt.BTAdapter.getCurrentScanType
ScanType getCurrentScanType()
Returns the current meta discovering ScanType.
org.direct_bt.BTAdapter.startDiscovery
HCIStatusCode startDiscovery(final boolean keepAlive, final boolean le_scan_active)
Turns on device discovery if it is disabled.
org.direct_bt.BDAddressAndType
Unique Bluetooth EUI48 address and BDAddressType tuple.
Definition: BDAddressAndType.java:36
org.direct_bt.BTAdapter.setPairable
boolean setPairable(boolean value)
Sets the discoverable state the adapter.
org.direct_bt.BTAdapter.getVisibleAddressAndType
BDAddressAndType getVisibleAddressAndType()
Returns the adapter's currently visible BDAddressAndType.
org.direct_bt.ScanType
Meta ScanType as derived from BTMode with defined value mask consisting of BDAddressType bits.
Definition: ScanType.java:36
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.BTAdapter.addDeviceToWhitelist
boolean addDeviceToWhitelist(final BDAddressAndType addressAndType, final HCIWhitelistConnectType ctype)
Add the given device to the adapter's autoconnect whitelist.
org.direct_bt.BTDevice
Provides access to Bluetooth adapters.
Definition: BTDevice.java:42
org.direct_bt.BTAdapter.getDiscoverable
boolean getDiscoverable()
Returns the discoverable state the adapter.
org.direct_bt.BTAdapter.removeStatusListener
boolean removeStatusListener(final AdapterStatusListener l)
Remove the given AdapterStatusListener from the list.
org.direct_bt.BTAdapter.stopDiscovery
HCIStatusCode stopDiscovery()
Turns off device discovery if it is enabled.
org.direct_bt.BTException
Definition: BTException.java:32
org.direct_bt.BTAdapter.isSuspended
boolean isSuspended()
Returns whether the adapter is suspended, i.e.
org.direct_bt.BTAdapter.addStatusListener
boolean addStatusListener(final AdapterStatusListener listener)
Add the given AdapterStatusListener to the list if not already present.
org.direct_bt.BTAdapter.getDiscoveredDevices
List< BTDevice > getDiscoveredDevices()
Returns a list of discovered BluetoothDevices from this adapter.
org.direct_bt.BTObject
Definition: BTObject.java:31
org.direct_bt.BTAdapter.setAlias
void setAlias(String value)
Sets the friendly name of this adapter.
org.direct_bt.BTAdapter.addDeviceToWhitelist
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.
org.direct_bt.BTAdapter.getPoweredState
boolean getPoweredState()
Returns the power state the adapter.
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.BTAdapter.removeDiscoveredDevice
boolean removeDiscoveredDevice(final BDAddressAndType addressAndType)
Discards matching discovered devices.
org.direct_bt.BTAdapter.setPowered
boolean setPowered(boolean value)
Sets the power state the adapter.
org.direct_bt.BTAdapter.getManager
BTManager getManager()
Returns the used singleton BTManager instance, used to create this adapter.
org.direct_bt.BTAdapter.getAddressAndType
BDAddressAndType getAddressAndType()
Returns the adapter's public BDAddressAndType.