Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestDBTClientServer00.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.reflect.InvocationTargetException;
28import java.util.Arrays;
29import java.util.List;
30
31import org.direct_bt.BTMode;
32import org.direct_bt.BTRole;
33import org.direct_bt.BTSecurityLevel;
34import org.direct_bt.BTAdapter;
35import org.direct_bt.BTException;
36import org.direct_bt.BTFactory;
37import org.direct_bt.BTManager;
38import org.direct_bt.DirectBTVersion;
39import org.jau.io.PrintUtil;
40import org.jau.net.EUI48;
41import org.junit.Assert;
42import org.junit.FixMethodOrder;
43import org.junit.Test;
44import org.junit.runners.MethodSorters;
45
46/**
47 * Basic client and server Bluetooth tests using non-fat `Direct-BT Jar`, requiring one BT adapter:
48 * - test loading native libraries
49 * - start server advertising
50 * - server stop advertising
51 * - reuse server-adapter for client-mode discovery (just toggle on/off)
52 */
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55 static final boolean DEBUG = false;
56
57 /**
58 * Testing BTManager bring up using non-fat `Direct-BT Jar` and
59 * - test loading native libraries
60 * - test that at least one adapter are present
61 * - validating basic default adapter status
62 */
63 @Test(timeout = 5000)
64 public final void test01_ManagerBringup() {
66
67 BTManager manager = null;
68 try {
69 manager = BTFactory.getDirectBTManager();
70 } catch (BTException | NoSuchMethodException | SecurityException
71 | IllegalAccessException | IllegalArgumentException
72 | InvocationTargetException | ClassNotFoundException e) {
73 e.printStackTrace();
74 Assert.assertNull("Unable to instantiate Direct-BT BluetoothManager: "+e.getMessage(), e);
75 }
76 if( null == manager ) {
77 return;
78 }
79 final List<BTAdapter> adapters = manager.getAdapters();
80 PrintUtil.println(System.err, "Adapter: Count "+adapters.size());
81 for(int i=0; i<adapters.size(); i++) {
82 PrintUtil.println(System.err, i+": "+adapters.get(i).toString());
83 }
84 Assert.assertTrue("Adapter count not >= 1 but "+adapters.size(), adapters.size() >= 1);
85
86 for(final BTAdapter a : adapters) {
87 Assert.assertFalse( a.isInitialized() );
88 Assert.assertFalse( a.isPowered() );
89 Assert.assertEquals( BTRole.Master, a.getRole() ); // default role
90 Assert.assertTrue( 4 <= a.getBTMajorVersion() );
91 }
92 }
93
94 @Test(timeout = 5000)
95 public final void test10_ServerStartStop_and_ToggleDiscovery() {
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
109 final String serverName = "TestDBTCS00-S-T10";
110 final DBTServer01 server = new DBTServer01(serverName, EUI48.ALL_DEVICE, BTMode.DUAL, true /* SC */, BTSecurityLevel.NONE);
111 server.setProtocolSessionsLeft(1);
112
113 final DBTEndpoint.ChangedAdapterSetListener myChangedAdapterSetListener =
114 DBTEndpoint.initChangedAdapterSetListener(manager, Arrays.asList(server));
115
116 //
117 // Server start
118 //
120 DBTServerTest.startAdvertising(server, false /* current_exp_advertising_state */, "test10_startAdvertising");
121
122 //
123 // Server stop
124 //
125 DBTServerTest.stop(server, "test10_stopAdvertising");
126
127 //
128 // Now reuse adapter for client mode -> Start discovery + Stop Discovery
129 //
130 {
131 final BTAdapter adapter = server.getAdapter();
132 { // if( false ) {
133 adapter.removeAllStatusListener();
134 }
135
136 DBTEndpoint.startDiscovery(adapter, false /* current_exp_discovering_state */);
137
138 DBTEndpoint.stopDiscovery(adapter, true /* current_exp_discovering_state */);
139 }
140
141 final int count = manager.removeChangedAdapterSetListener(myChangedAdapterSetListener);
142 PrintUtil.println(System.err, "****** EOL Removed ChangedAdapterSetCallback " + count);
143 }
144
145 public static void main(final String args[]) {
146 org.junit.runner.JUnitCore.main(TestDBTClientServer00.class.getName());
147 }
148}
One stop BTManager API entry point.
Definition: BTFactory.java:52
static synchronized BTManager getDirectBTManager()
Returns an initialized BluetoothManager instance using the DirectBT implementation.
Definition: BTFactory.java:472
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.
This peripheral BTRole::Slave test participant works with DBTClient00.
BTAdapter getAdapter()
Return the adapter for this endpoint.
void setProtocolSessionsLeft(final int v)
Basic client and server Bluetooth tests using non-fat Direct-BT Jar, requiring one BT adapter:
Bluetooth adapter operating mode.
Definition: BTMode.java:34
DUAL
Dual Bluetooth mode, i.e.
Definition: BTMode.java:38
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
Bluetooth Security Level.
NONE
No encryption and no authentication.
BTAdapter represents one local Bluetooth Controller.
Definition: BTAdapter.java:48
int removeAllStatusListener()
Remove all AdapterStatusListener from the list.
A thread safe singleton handler of the BTAdapter manager, e.g.
Definition: BTManager.java:36
List< BTAdapter > getAdapters()
Returns a list of BluetoothAdapters available in the system.
int removeChangedAdapterSetListener(final ChangedAdapterSetListener l)
Remove the given ChangedAdapterSetListener from this manager.
static void startDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state)
static void checkInitializedState(final DBTEndpoint endp)
static void stopDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state)
static ChangedAdapterSetListener initChangedAdapterSetListener(final BTManager manager, final List< DBTEndpoint > endpts)
HCIStatusCode startAdvertising(String msg)
static void stop(final DBTServerTest server, final String msg)