Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestBringup00.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 test.org.direct_bt;
26
27import java.lang.reflect.InvocationTargetException;
28import java.util.List;
29
30import org.direct_bt.BTRole;
31import org.direct_bt.BTSecurityRegistry;
32import org.direct_bt.BTAdapter;
33import org.direct_bt.BTDeviceRegistry;
34import org.direct_bt.BTException;
35import org.direct_bt.BTFactory;
36import org.direct_bt.BTManager;
37import org.direct_bt.DirectBTVersion;
38import org.jau.io.PrintUtil;
39import org.junit.Assert;
40import org.junit.FixMethodOrder;
41import org.junit.Test;
42import org.junit.runners.MethodSorters;
43
44import jau.test.junit.util.SingletonJunitCase;
45
46/**
47 * Testing BTManager bring up using fat `Direct-BT Jaulib Fat Jar` and
48 * - test loading native libraries
49 * - show all installed adapter
50 * - no extra permissions required
51 */
52@FixMethodOrder(MethodSorters.NAME_ASCENDING)
53public class TestBringup00 extends SingletonJunitCase {
54 static final boolean DEBUG = false;
55
56 private void resetStates() {
57 BTManager manager = null;
58 try {
59 manager = BTFactory.getDirectBTManager();
60 } catch (BTException | NoSuchMethodException | SecurityException
61 | IllegalAccessException | IllegalArgumentException
62 | InvocationTargetException | ClassNotFoundException e) {
63 e.printStackTrace();
64 PrintUtil.println(System.err, "Unable to instantiate Direct-BT BluetoothManager: "+e.getMessage());
65 e.printStackTrace();
66 }
67 if( null != manager ) {
68 final List<BTAdapter> adapters = manager.getAdapters();
69 for(final BTAdapter a : adapters) {
70 // test runs w/o elevated permissions
71 a.removeAllStatusListener();
72 // a.stopAdvertising();
73 // a.stopDiscovery();
74 // Assert.assertTrue( a.setPowered(false) );
75 }
76 }
81 }
82
83 @Test(timeout = 40000) // long timeout for valgrind
84 public final void test01_ManagerBringup() {
85 {
86 // System.setProperty("direct_bt.debug", "true"); // native code
87 // System.setProperty("direct_bt.debug", "true,gatt.data"); // native code
88 // System.setProperty("org.direct_bt.debug", "true"); // java
89 // System.setProperty("jau.debug", "true"); // java
90 // System.setProperty("jau.verbose", "true"); // java
91 }
94 resetStates();
95
96 BTManager manager = null;
97 try {
98 manager = BTFactory.getDirectBTManager();
99 } catch (BTException | NoSuchMethodException | SecurityException
100 | IllegalAccessException | IllegalArgumentException
101 | InvocationTargetException | ClassNotFoundException e) {
102 e.printStackTrace();
103 Assert.assertNull("Unable to instantiate Direct-BT BluetoothManager: "+e.getMessage(), e);
104 }
105 if( null == manager ) {
106 return;
107 }
108 final List<BTAdapter> adapters = manager.getAdapters();
109 PrintUtil.println(System.err, "Adapter: Count "+adapters.size());
110 for(int i=0; i<adapters.size(); i++) {
111 PrintUtil.println(System.err, i+": "+adapters.get(i).toString());
112 }
113 for(final BTAdapter a : adapters) {
114 // test runs w/o elevated permissions
115 Assert.assertFalse( a.isInitialized() );
116 // Assert.assertFalse( a.isPowered() );
117 Assert.assertEquals( BTRole.Master, a.getRole() ); // default role
118 Assert.assertTrue( 4 <= a.getBTMajorVersion() );
119 }
120 // All implicit via destructor or shutdown hook!
121 adapters.clear();
122 resetStates();
123 manager.shutdown(); /* implies: adapter.close(); */
124 }
125
126 public static void main(final String args[]) {
127 org.junit.runner.JUnitCore.main(TestBringup00.class.getName());
128 }
129}
Application toolkit providing BT device registration of processed and awaited devices.
static void clearWaitForDevices()
Clears internal list.
static void clearProcessedDevices()
Clears internal list.
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
Application toolkit providing BT security setup and its device association on a pattern matching basi...
static void clear()
Clears internal list.
This jaulib derived version info class is only usable when having jaulib available,...
static final void printVersionInfo(final PrintStream out)
Print full Direct-BT version information.
Testing BTManager bring up using fat Direct-BT Jaulib Fat Jar and.
static void main(final String args[])
Bluetooth roles from the perspective of the link layer (connection initiator).
Definition: BTRole.java:36
Master
Master or central role, discovering remote devices and initiating connection.
Definition: BTRole.java:40
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.