Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBTEndpoint.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 */
24package trial.org.direct_bt;
25
26import java.util.ArrayList;
27import java.util.List;
28import org.direct_bt.BTAdapter;
29import org.direct_bt.BTManager;
30import org.direct_bt.BTRole;
31import org.direct_bt.HCIStatusCode;
32import org.jau.io.PrintUtil;
33import org.junit.Assert;
34
35public interface DBTEndpoint {
36
37 /**
38 * Return name of this endpoint,
39 * which becomes the adapter's name.
40 */
41 String getName();
42
43 /**
44 * Set the server adapter for this endpoint.
45 *
46 * This is done in {@link ChangedAdapterSetListener#adapterAdded(BTAdapter)}
47 * if {@link #initAdapter(BTAdapter)} returned true.
48 *
49 * @param a the associate adapter for this endpoint.
50 */
52
53 /**
54 * Return the adapter for this endpoint.
55 */
57
58 void close(final String msg);
59
60 void setProtocolSessionsLeft(final int v);
65
66 /**
67 * Initialize the given adapter for this endpoint.
68 *
69 * The matching and successfully initialized adapter
70 * will become this endpoint's associated adapter via {@link #setAdapter(BTAdapter)},
71 * as performed in in {@link ChangedAdapterSetListener#adapterAdded(BTAdapter)}.
72 *
73 * @param adapter the potential associated adapter for this endpoint.
74 * @return true if successful and associated
75 */
76 boolean initAdapter(BTAdapter adapter);
77
78 public static void checkInitializedState(final DBTEndpoint endp) {
79 final BTAdapter adapter = endp.getAdapter();
80 Assert.assertTrue( adapter.isInitialized() );
81 Assert.assertTrue( adapter.isPowered() );
82 Assert.assertEquals( BTRole.Master, adapter.getRole() );
83 Assert.assertTrue( 4 <= adapter.getBTMajorVersion() );
84 }
85
86 public static ChangedAdapterSetListener initChangedAdapterSetListener(final BTManager manager, final List<DBTEndpoint> endpts) {
88 manager.addChangedAdapterSetListener(casl);
89 for(final DBTEndpoint endpt : endpts ) {
90 Assert.assertNotNull("No adapter found for "+endpt.getClass().getSimpleName(), endpt.getAdapter());
91 }
92 return casl;
93 }
95 List<DBTEndpoint> endpts = new ArrayList<DBTEndpoint>();
96
98 public ChangedAdapterSetListener(final List<DBTEndpoint> el) {
99 endpts.addAll(el);
100 }
101 public boolean add(final DBTEndpoint e) { return endpts.add(e); }
102
103 @Override
104 public void adapterAdded(final BTAdapter adapter) {
105 for(final DBTEndpoint endpt : endpts ) {
106 if( null == endpt.getAdapter() ) {
107 if( endpt.initAdapter( adapter ) ) {
108 endpt.setAdapter(adapter);
109 PrintUtil.println(System.err, "****** Adapter-"+endpt.getClass().getSimpleName()+" ADDED__: InitOK: " + adapter);
110 return;
111 }
112 }
113 }
114 PrintUtil.println(System.err, "****** Adapter ADDED__: Ignored: " + adapter);
115 }
116
117 @Override
118 public void adapterRemoved(final BTAdapter adapter) {
119 for(final DBTEndpoint endpt : endpts ) {
120 if( null != endpt.getAdapter() && adapter.equals( endpt.getAdapter() ) ) {
121 endpt.setAdapter(null);
122 PrintUtil.println(System.err, "****** Adapter-"+endpt.getClass().getSimpleName()+" REMOVED: " + adapter);
123 return;
124 }
125 }
126 PrintUtil.println(System.err, "****** Adapter REMOVED: Ignored " + adapter);
127 }
128 };
129
130 public static void startDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state) {
131 Assert.assertFalse(adapter.isAdvertising());
132 Assert.assertEquals(current_exp_discovering_state, adapter.isDiscovering());
133
134 Assert.assertEquals( HCIStatusCode.SUCCESS, adapter.startDiscovery() );
135 while( !adapter.isDiscovering() ) { // pending action
136 try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); }
137 }
138 Assert.assertFalse(adapter.isAdvertising());
139 Assert.assertTrue(adapter.isDiscovering());
140 Assert.assertEquals( BTRole.Master, adapter.getRole() );
141 }
142
143 public static void stopDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state) {
144 Assert.assertFalse(adapter.isAdvertising());
145 Assert.assertEquals(current_exp_discovering_state, adapter.isDiscovering());
146 Assert.assertEquals( BTRole.Master, adapter.getRole() );
147
148 Assert.assertEquals( HCIStatusCode.SUCCESS, adapter.stopDiscovery() ); // pending action
149 while( adapter.isDiscovering() ) { // pending action
150 try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); }
151 }
152 Assert.assertFalse(adapter.isAdvertising());
153 Assert.assertFalse(adapter.isDiscovering());
154 Assert.assertEquals( BTRole.Master, adapter.getRole() );
155 }
156
157}
void adapterRemoved(final BTAdapter adapter)
BTAdapter was removed from the system.
void adapterAdded(final BTAdapter adapter)
BTAdapter was added to the system.
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
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
BTAdapter represents one local Bluetooth Controller.
Definition: BTAdapter.java:48
HCIStatusCode stopDiscovery()
Turns off device discovery if it is enabled.
HCIStatusCode startDiscovery()
Starts discovery using all default arguments, see startDiscovery(DiscoveryPolicy, boolean,...
boolean isInitialized()
Returns true, if initialize(BTMode) has already been called for this adapter, otherwise false.
boolean equals(final Object obj)
If both types are of BTAdapter, it compares their BDAddressAndType, see getAddressAndType().
boolean isAdvertising()
Returns the adapter's current advertising state.
boolean isPowered()
Returns whether the adapter is valid, plugged in and powered.
boolean isDiscovering()
Returns true if the meta discovering state is not ScanType#NONE.
BTRole getRole()
Return the current BTRole of this adapter.
int getBTMajorVersion()
Returns the Bluetooth major version of this adapter.
A thread safe singleton handler of the BTAdapter manager, e.g.
Definition: BTManager.java:36
void addChangedAdapterSetListener(final ChangedAdapterSetListener l)
Add the given ChangedAdapterSetListener to this manager.
static void startDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state)
String getName()
Return name of this endpoint, which becomes the adapter's name.
static void checkInitializedState(final DBTEndpoint endp)
void setProtocolSessionsLeft(final int v)
void close(final String msg)
boolean initAdapter(BTAdapter adapter)
Initialize the given adapter for this endpoint.
void setAdapter(BTAdapter a)
Set the server adapter for this endpoint.
static void stopDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state)
BTAdapter getAdapter()
Return the adapter for this endpoint.
static ChangedAdapterSetListener initChangedAdapterSetListener(final BTManager manager, final List< DBTEndpoint > endpts)