Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BaseDBTClientServer.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package trial.org.direct_bt;
26
27import java.lang.invoke.MethodHandles;
28import java.lang.reflect.InvocationTargetException;
29import java.util.List;
30
31import org.direct_bt.BTAdapter;
32import org.direct_bt.BTDeviceRegistry;
33import org.direct_bt.BTException;
34import org.direct_bt.BTFactory;
35import org.direct_bt.BTManager;
36import org.direct_bt.BTSecurityRegistry;
37import org.jau.io.PrintUtil;
38import org.junit.After;
39import org.junit.AfterClass;
40import org.junit.Assert;
41import org.junit.Before;
42import org.junit.BeforeClass;
43import org.junit.FixMethodOrder;
44import org.junit.runners.MethodSorters;
45
46import jau.test.junit.util.SingletonJunitCase;
47
48@FixMethodOrder(MethodSorters.NAME_ASCENDING)
49public abstract class BaseDBTClientServer extends SingletonJunitCase {
50 static boolean DEBUG = false;
51
52 @BeforeClass
53 public static final void setupAll() {
54 if( DEBUG ) {
55 System.setProperty("direct_bt.debug", "true"); // native code
56 // System.setProperty("direct_bt.debug", "true,gatt.data"); // native code
57 // System.setProperty("org.direct_bt.debug", "true"); // java
58 // System.setProperty("jau.debug", "true"); // java
59 // System.setProperty("jau.verbose", "true"); // java
60 }
62
63 final Class<?> ThisClazz = MethodHandles.lookup().lookupClass();
64 PrintUtil.println(System.err, "++++ Test "+ThisClazz.getSimpleName()+".setupAll()");
65
66 Assert.assertTrue( DBTUtils.rmKeyFolder() );
67 Assert.assertTrue( DBTUtils.mkdirKeyFolder() );
68 }
69
70 /**
71 * Ensure
72 * - all adapter are powered off
73 * - manager being shutdown
74 */
75 @AfterClass
76 public static final void cleanupAll() {
77 final Class<?> ThisClazz = MethodHandles.lookup().lookupClass();
78 PrintUtil.println(System.err, "++++ Test "+ThisClazz.getSimpleName()+".cleanupAll()");
79
80 BTManager manager = null;
81 try {
82 manager = BTFactory.getDirectBTManager();
83 } catch (BTException | NoSuchMethodException | SecurityException
84 | IllegalAccessException | IllegalArgumentException
85 | InvocationTargetException | ClassNotFoundException e) {
86 e.printStackTrace();
87 PrintUtil.println(System.err, "Unable to instantiate Direct-BT BluetoothManager: "+e.getMessage());
88 e.printStackTrace();
89 }
90 if( null != manager ) {
91 final List<BTAdapter> adapters = manager.getAdapters();
92 for(final BTAdapter a : adapters) {
93 a.stopAdvertising();
94 a.stopDiscovery();
95 a.setPowered(false);
96 }
97 // All implicit via destructor or shutdown hook!
98 manager.shutdown(); /* implies: adapter.close(); */
99 }
100 }
101
102 private void resetStates() {
103 BTManager manager = null;
104 try {
105 manager = BTFactory.getDirectBTManager();
106 } catch (BTException | NoSuchMethodException | SecurityException
107 | IllegalAccessException | IllegalArgumentException
108 | InvocationTargetException | ClassNotFoundException e) {
109 e.printStackTrace();
110 PrintUtil.println(System.err, "Unable to instantiate Direct-BT BluetoothManager: "+e.getMessage());
111 e.printStackTrace();
112 }
113 if( null != manager ) {
114 final List<BTAdapter> adapters = manager.getAdapters();
115 for(final BTAdapter a : adapters) {
116 a.removeAllStatusListener();
117 a.stopAdvertising();
118 a.stopDiscovery();
119 Assert.assertTrue( a.setPowered(false) );
120 }
121 }
123 BTDeviceRegistry.clearWaitForDevices();
124 BTDeviceRegistry.clearProcessedDevices();
125 BTSecurityRegistry.clear();
126 }
127
128 /**
129 * Ensure
130 * - all adapter are powered off
131 */
132 @Before
133 public final void setupTest() {
134 final Class<?> ThisClazz = MethodHandles.lookup().lookupClass();
135 PrintUtil.println(System.err, "++++ Test "+ThisClazz.getSimpleName()+".setupTest()");
136
137 resetStates();
138 }
139
140 /**
141 * Ensure
142 * - remove all status listener from all adapter
143 * - all adapter are powered off
144 * - clear BTDeviceRegistry
145 * - clear BTSecurityRegistry
146 */
147 @After
148 public final void cleanupTest() {
149 final Class<?> ThisClazz = MethodHandles.lookup().lookupClass();
150 PrintUtil.println(System.err, "++++ Test "+ThisClazz.getSimpleName()+".cleanupTest()");
151
152 resetStates();
153 }
154}
One stop BTManager API entry point.
Definition: BTFactory.java:52
static synchronized void initDirectBTLibrary()
Preloads the DirectBT native library w/o instantiating BTManager.
Definition: BTFactory.java:482
static synchronized BTManager getDirectBTManager()
Returns an initialized BluetoothManager instance using the DirectBT implementation.
Definition: BTFactory.java:472
static final boolean mkdirKeyFolder()
Definition: DBTUtils.java:34
static final boolean rmKeyFolder()
Definition: DBTUtils.java:106
BTAdapter represents one local Bluetooth Controller.
Definition: BTAdapter.java:48
A thread safe singleton handler of the BTAdapter manager, e.g.
Definition: BTManager.java:36
int removeAllChangedAdapterSetListener()
Remove all added ChangedAdapterSetListener entries from this manager.
List< BTAdapter > getAdapters()
Returns a list of BluetoothAdapters available in the system.
void shutdown()
Release the native memory associated with this object and all related Bluetooth resources.