Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTManager.java
Go to the documentation of this file.
1 /**
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  * Copyright (c) 2020 ZAFENA AB
5  *
6  * Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
7  * Copyright (c) 2016 Intel Corporation.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 package org.direct_bt;
30 
31 import java.util.List;
32 
33 public interface BTManager
34 {
35  /**
36  * Interface allowing to retrieve certain settings
37  * of the implementation.
38  * <p>
39  * In case this interface will have to add a multitude of settings in the future,
40  * we will revise the API using a more efficient bitfield approach.
41  * </p>
42  */
43  public static interface Settings {
44  /**
45  * Returns true if underlying implementation is Direct-BT.
46  */
47  boolean isDirectBT();
48 
49  @Override
50  String toString();
51  }
52 
53 
54  /**
55  * Event listener to receive change events regarding the system's {@link BTAdapter} set,
56  * e.g. a removed or added {@link BTAdapter} due to user interaction or 'cold reset'.
57  * <p>
58  * When a new callback is added, all available adapter's will be reported as added,
59  * this allows a fully event driven workflow.
60  * </p>
61  * <p>
62  * The callback is performed on a dedicated thread,
63  * allowing the user to perform complex operations.
64  * </p>
65  * @since 2.0.0
66  * @see BTManager#addChangedAdapterSetListener(ChangedAdapterSetListener)
67  * @see BTManager#removeChangedAdapterSetListener(ChangedAdapterSetListener)
68  */
69  public static interface ChangedAdapterSetListener {
70  /**
71  * {@link BTAdapter} was added to the system.
72  * @param adapter the newly added {@link BTAdapter} to the system
73  */
74  void adapterAdded(final BTAdapter adapter);
75 
76  /**
77  * {@link BTAdapter} was removed from the system.
78  * <p>
79  * {@link BTAdapter#close()} is being called by the native manager after issuing all
80  * {@link #adapterRemoved(BTAdapter)} calls.
81  * </p>
82  * @param adapter the removed {@link BTAdapter} from the system
83  */
84  void adapterRemoved(final BTAdapter adapter);
85  }
86 
87  /**
88  * Returns this implmentation's {@link Settings}.
89  */
91 
92  /** Find a BluetoothObject of a type matching type. If parameters name,
93  * identifier and parent are not null, the returned object will have to
94  * match them.
95  * It will first check for existing objects. It will not turn on discovery
96  * or connect to devices.
97  * @parameter type specify the type of the object you are
98  * waiting for, NONE means anything.
99  * @parameter name optionally specify the name of the object you are
100  * waiting for (for Adapter or Device)
101  * @parameter identifier optionally specify the identifier of the object you are
102  * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address
103  * for Adapter or Device)
104  * @parameter parent optionally specify the parent of the object you are
105  * waiting for
106  * @parameter timeoutMS the function will return after timeout time in milliseconds, a
107  * value of zero means wait forever. If object is not found during this time null will be returned.
108  * @return An object matching the name, identifier, parent or null if not found before
109  * timeout expires or event is canceled.
110  */
111  public BTObject find(BTType type, String name, String identifier, BTObject parent, long timeoutMS);
112 
113 
114  /** Find a BluetoothObject of a type matching type. If parameters name,
115  * identifier and parent are not null, the returned object will have to
116  * match them.
117  * It will first check for existing objects. It will not turn on discovery
118  * or connect to devices.
119  * @parameter type specify the type of the object you are
120  * waiting for, NONE means anything.
121  * @parameter name optionally specify the name of the object you are
122  * waiting for (for Adapter or Device)
123  * @parameter identifier optionally specify the identifier of the object you are
124  * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address
125  * for Adapter or Device)
126  * @parameter parent optionally specify the parent of the object you are
127  * waiting for
128  * @return An object matching the name, identifier and parent.
129  */
130  public BTObject find(BTType type, String name, String identifier, BTObject parent);
131 
132  /** Returns a list of BluetoothAdapters available in the system
133  * @return A list of BluetoothAdapters available in the system
134  */
135  public List<BTAdapter> getAdapters();
136 
137  /**
138  * Returns the BluetoothAdapter matching the given dev_id or null if not found.
139  * <p>
140  * The adapters internal device id is constant across the adapter lifecycle,
141  * but may change after its destruction.
142  * </p>
143  * @param dev_id the internal temporary adapter device id
144  * @since 2.0.0
145  */
146  public BTAdapter getAdapter(final int dev_id);
147 
148  /** Returns a list of discovered BluetoothDevices
149  * @return A list of discovered BluetoothDevices
150  */
151  public List<BTDevice> getDevices();
152 
153  /** Returns a list of available BluetoothGattServices
154  * @return A list of available BluetoothGattServices
155  */
156  public List<BTGattService> getServices();
157 
158  /**
159  * Sets a default adapter to use for discovery.
160  * @return TRUE if the device was set
161  * @implNote not implemented for jau.direct_bt
162  */
163  public boolean setDefaultAdapter(BTAdapter adapter);
164 
165  /**
166  * Gets the default adapter to use for discovery.
167  * <p>
168  * The default adapter is either the first {@link BTAdapter#isPowered() powered} {@link BTAdapter},
169  * or function returns nullptr if none is enabled.
170  * </p>
171  * @return the used default adapter
172  */
174 
175  /**
176  * Turns on device discovery on the default adapter if it is disabled.
177  * @param keepAlive if {@code true}, indicates that discovery shall be restarted
178  * if stopped by the underlying Bluetooth implementation (BlueZ, ..).
179  * Using {@link #startDiscovery(boolean) startDiscovery}({@code keepAlive=true})
180  * and {@link #stopDiscovery()} is the recommended workflow
181  * for a reliable discovery process.
182  * @param le_scan_active true enables delivery of active scanning PDUs, otherwise no scanning PDUs shall be sent (default)
183  * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state
184  * @throws BTException
185  * @since 2.0.0
186  * @since 2.2.8
187  */
188  public HCIStatusCode startDiscovery(final boolean keepAlive, final boolean le_scan_active) throws BTException;
189 
190  /**
191  * Turns off device discovery on the default adapter 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  /**
199  * Returns the current meta discovering {@link ScanType} of the {@link #getDefaultAdapter()}
200  * via {@link BTAdapter#getCurrentScanType()}.
201  * @see BTAdapter#getCurrentScanType()
202  * @see #getDefaultAdapter()
203  * @since 2.3
204  */
206 
207  /**
208  * Add the given {@link ChangedAdapterSetListener} to this manager.
209  * <p>
210  * When a new callback is added, all available adapter's will be reported as added,
211  * this allows a fully event driven workflow.
212  * </p>
213  * <p>
214  * The callback is performed on a dedicated thread,
215  * allowing the user to perform complex operations.
216  * </p>
217  * @since 2.0.0
218  */
220 
221  /**
222  * Remove the given {@link ChangedAdapterSetListener} from this manager.
223  * @param l the to be removed element
224  * @return the number of removed elements
225  * @since 2.0.0
226  */
228 
229  /**
230  * Release the native memory associated with this object and all related Bluetooth resources.
231  * The object should not be used following a call to close
232  * <p>
233  * Shutdown method is intended to allow a clean Bluetooth state at program exist.
234  * </p>
235  */
236  public void shutdown();
237 }
org.direct_bt.BTManager.setDefaultAdapter
boolean setDefaultAdapter(BTAdapter adapter)
Sets a default adapter to use for discovery.
org.direct_bt.BTAdapter
Provides access to Bluetooth adapters.
Definition: BTAdapter.java:41
org.direct_bt.BTManager.removeChangedAdapterSetListener
int removeChangedAdapterSetListener(final ChangedAdapterSetListener l)
Remove the given ChangedAdapterSetListener from this manager.
org.direct_bt.BTManager.getDevices
List< BTDevice > getDevices()
Returns a list of discovered BluetoothDevices.
org.direct_bt.BTManager.ChangedAdapterSetListener.adapterAdded
void adapterAdded(final BTAdapter adapter)
BTAdapter was added to the system.
org.direct_bt.BTManager.stopDiscovery
HCIStatusCode stopDiscovery()
Turns off device discovery on the default adapter if it is enabled.
org.direct_bt.BTManager
Definition: BTManager.java:34
org.direct_bt.BTManager.find
BTObject find(BTType type, String name, String identifier, BTObject parent, long timeoutMS)
Find a BluetoothObject of a type matching type.
org.direct_bt.BTManager.Settings.toString
String toString()
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.BTManager.getSettings
Settings getSettings()
Returns this implmentation's Settings.
org.direct_bt.BTManager.getAdapters
List< BTAdapter > getAdapters()
Returns a list of BluetoothAdapters available in the system.
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.BTManager.Settings.isDirectBT
boolean isDirectBT()
Returns true if underlying implementation is Direct-BT.
org.direct_bt.BTManager.getAdapter
BTAdapter getAdapter(final int dev_id)
Returns the BluetoothAdapter matching the given dev_id or null if not found.
org.direct_bt.BTManager.ChangedAdapterSetListener.adapterRemoved
void adapterRemoved(final BTAdapter adapter)
BTAdapter was removed from the system.
org.direct_bt.BTType
Definition: BTType.java:28
org.direct_bt.BTException
Definition: BTException.java:32
org.direct_bt.BTManager.getDefaultAdapter
BTAdapter getDefaultAdapter()
Gets the default adapter to use for discovery.
org.direct_bt.BTManager.Settings
Interface allowing to retrieve certain settings of the implementation.
Definition: BTManager.java:43
org.direct_bt.BTManager.getCurrentScanType
ScanType getCurrentScanType()
Returns the current meta discovering ScanType of the getDefaultAdapter() via BTAdapter#getCurrentScan...
org.direct_bt.BTObject
Definition: BTObject.java:31
org.direct_bt.BTManager.find
BTObject find(BTType type, String name, String identifier, BTObject parent)
Find a BluetoothObject of a type matching type.
org.direct_bt.BTManager.shutdown
void shutdown()
Release the native memory associated with this object and all related Bluetooth resources.
org.direct_bt.BTManager.startDiscovery
HCIStatusCode startDiscovery(final boolean keepAlive, final boolean le_scan_active)
Turns on device discovery on the default adapter if it is disabled.
org.direct_bt.BTManager.getServices
List< BTGattService > getServices()
Returns a list of available BluetoothGattServices.
org.direct_bt.BTManager.addChangedAdapterSetListener
void addChangedAdapterSetListener(final ChangedAdapterSetListener l)
Add the given ChangedAdapterSetListener to this manager.
org.direct_bt.BTManager.ChangedAdapterSetListener
Event listener to receive change events regarding the system's BTAdapter set, e.g.
Definition: BTManager.java:69