Skip to content

Commit 2ecb6c3

Browse files
committed
stabilised bt connection establishment
1 parent c78934c commit 2ecb6c3

14 files changed

Lines changed: 136 additions & 56 deletions

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ https://developer.android.com/studio/projects/android-library
33
*/
44

55
// choose one line either for an app or a lib
6-
//apply plugin: 'com.android.application'
7-
apply plugin: 'com.android.library'
6+
apply plugin: 'com.android.application'
7+
//apply plugin: 'com.android.library'
88

99
android {
1010
compileSdkVersion 28
1111
defaultConfig {
1212
// we produce a library by commenting out that line:
13-
// applicationId "net.sharksystem.asap.example"
13+
applicationId "net.sharksystem.asap.example"
1414
minSdkVersion 23
1515
targetSdkVersion 28
1616
versionCode 1

app/libs/ASAP_Engine_0.5.0.jar

1.32 KB
Binary file not shown.

app/src/main/java/net/sharksystem/asap/android/apps/ASAPOnlineMessageSenderUserSide.java renamed to app/src/main/java/net/sharksystem/asap/android/apps/ASAPOnlineMessageSenderAndroidUserSide.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
import java.io.IOException;
1414
import java.util.List;
1515

16-
public class ASAPOnlineMessageSenderUserSide extends ASAPAbstractOnlineMessageSender {
16+
public class ASAPOnlineMessageSenderAndroidUserSide extends ASAPAbstractOnlineMessageSender {
1717

1818
private final ASAPApplication asapApplication;
1919

20-
public ASAPOnlineMessageSenderUserSide(ASAPApplication asapApplication) {
20+
public ASAPOnlineMessageSenderAndroidUserSide(ASAPApplication asapApplication) {
2121
this.asapApplication = asapApplication;
2222
}
2323

app/src/main/java/net/sharksystem/asap/android/bluetooth/BluetoothClientSocketThread.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public void run() {
4040
}
4141
}
4242

43+
private String logStartString = null;
4344
private String getLogStart() {
44-
return "BTClientSocketThread";
45+
if(this.logStartString == null) {
46+
this.logStartString = "BTClientSocketThread (to " + btDevice.getAddress() + "): ";
47+
}
48+
return this.logStartString;
4549
}
4650
}

app/src/main/java/net/sharksystem/asap/android/bluetooth/BluetoothEngine.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
1818

1919
import java.io.IOException;
20+
import java.util.HashMap;
21+
import java.util.Map;
2022

2123
import static android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_FINISHED;
2224
import static android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_STARTED;
@@ -68,7 +70,11 @@ public void start() {
6870

6971
public void stop() {
7072
Log.d(this.getLogStart(), "stop bluetooth");
71-
this.shutdown();
73+
try {
74+
this.shutdown();
75+
} catch (ASAPException e) {
76+
Log.e(this.getLogStart(), "could not shutdown bt: " + e.getLocalizedMessage());
77+
}
7278
}
7379

7480
/////////////////////////////////////////////////////////////////////////////////
@@ -95,7 +101,7 @@ private void setup() {
95101
Log.d(this.getLogStart(), "check if BT is enabled");
96102
if (!mBluetoothAdapter.isEnabled()) {
97103
Log.i(this.getLogStart(),
98-
"Bluetooth disabled - ask application for help - stop setting up bt");
104+
"Bluetooth disabled - ask application to start BT - stop setting up so far");
99105

100106
Intent requestIntent = new ASAPServiceRequestNotifyIntent(
101107
ASAPServiceRequestNotifyIntent.ASAP_RQ_ASK_USER_TO_ENABLE_BLUETOOTH);
@@ -185,8 +191,8 @@ public void startDiscoverable(int time) {
185191
* Start a BT scanning sweep of the area. According to android manual, each
186192
* sweep takes 12 seconds - thus that method could be called frequently.
187193
*/
188-
public boolean startDiscovery() {
189-
if(this.mBluetoothAdapter.startDiscovery()) {
194+
public boolean startDiscovery() throws ASAPException {
195+
if(this.getBTAdapter().startDiscovery()) {
190196
Log.d(this.getLogStart(), "successfully started Bluetooth discovery");
191197
return true;
192198
} else {
@@ -195,7 +201,7 @@ public boolean startDiscovery() {
195201
}
196202
}
197203

198-
private void shutdown() {
204+
private void shutdown() throws ASAPException {
199205
// unregister broadcast receiver
200206
if (this.foundBTDeviceBC != null) {
201207
Util.unregisterBCR(this.getLogStart(), this.getContext(), this.foundBTDeviceBC);
@@ -219,8 +225,8 @@ private void shutdown() {
219225
}
220226

221227
// stop BT adapter
222-
this.mBluetoothAdapter.cancelDiscovery();
223-
this.mBluetoothAdapter.disable();
228+
this.getBTAdapter().cancelDiscovery();
229+
this.getBTAdapter().disable();
224230

225231
this.btEnvironmentOn = false;
226232

@@ -234,14 +240,15 @@ BluetoothAdapter getBTAdapter() throws ASAPException {
234240
if(this.mBluetoothAdapter == null)
235241
throw new ASAPException("bluetooth not yet initialized");
236242

243+
Log.d(this.getLogStart(), "my mac address: " + this.mBluetoothAdapter.getAddress());
237244
return this.mBluetoothAdapter;
238245
}
239246

240247
/////////////////////////////////////////////////////////////////////////////////////////
241248
// handle connections and connection attempts //
242249
/////////////////////////////////////////////////////////////////////////////////////////
243250

244-
void deviceFound(BluetoothDevice btDevice, BluetoothClass btClass) {
251+
void deviceFound(BluetoothDevice btDevice, BluetoothClass btClass) throws ASAPException {
245252
String macAddress = btDevice.getAddress();// MAC address
246253

247254
StringBuilder sb = new StringBuilder();
@@ -250,7 +257,7 @@ void deviceFound(BluetoothDevice btDevice, BluetoothClass btClass) {
250257
sb.append(" | ");
251258
sb.append(btDevice.getName());
252259
sb.append("my address: ");
253-
sb.append(this.mBluetoothAdapter.getAddress());
260+
sb.append(this.getBTAdapter().getAddress());
254261
Log.d(this.getLogStart(), sb.toString());
255262

256263
// strongly recommended to stop discovery
@@ -265,37 +272,46 @@ void deviceFound(BluetoothDevice btDevice, BluetoothClass btClass) {
265272
}
266273
}
267274

275+
private Map<String, BluetoothSocket> openSockets = new HashMap<>();
276+
268277
/**
269278
* Both client and server sockets
270279
* @param socket
271280
* @throws IOException
272281
*/
273-
void handleBTSocket(BluetoothSocket socket) throws IOException {
274-
Log.d(this.getLogStart(), "new BT connection established");
275-
/* don't check here with shouldConnectToMACPeer if to talk
276-
with remote peer.
277-
278-
It must be checked before establishing a connection as client!
279-
Checked again here would fail - due to missing waiting period.
280-
281-
Thus, this must be check in server socket as well. See comments there.
282+
synchronized void handleBTSocket(BluetoothSocket socket) throws IOException {
283+
String address = socket.getRemoteDevice().getAddress();
284+
Log.d(this.getLogStart(), "going to handle new BT connection to " + address);
285+
286+
// already an open connection?
287+
BluetoothSocket bluetoothSocket = this.openSockets.get(address);
288+
if(bluetoothSocket != null) {
289+
// still active
290+
if(bluetoothSocket.isConnected()) {
291+
// already connected - connection is active
292+
Log.d(this.getLogStart(),
293+
"we already have an open and active connection to " + address);
294+
socket.close();
295+
return;
296+
} else {
297+
Log.d(this.getLogStart(), "we had a connection but it is gone " + address);
298+
}
299+
}
282300

283-
more:
284-
There is also a race condition between both peers - who will create connection
285-
earlier - that mechanism drops the later connection attempt.
286-
*/
301+
// remember that new connection
302+
this.openSockets.put(address, socket);
287303

288-
// set up new ASAP Session and we are done here.
304+
// set up new ASAP Session on that connection
289305
new ASAPConnectionLauncher(socket.getInputStream(), socket.getOutputStream(),
290306
this.getAsapService().getASAPEngine()).start();
291307
}
292308

293-
public void propagateStatus(Context ctx) {
309+
public void propagateStatus(Context ctx) throws ASAPException {
294310
Log.d(this.getLogStart(), "going to send status broadcast messages");
295311
ASAPServiceRequestNotifyIntent notifyIntent = null;
296312

297313
// Bluetooth running?
298-
if(this.mBluetoothAdapter.isEnabled()) {
314+
if(this.getBTAdapter().isEnabled()) {
299315
notifyIntent = new ASAPServiceRequestNotifyIntent(
300316
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_ENVIRONMENT_STARTED);
301317
} else {
@@ -305,7 +321,7 @@ public void propagateStatus(Context ctx) {
305321

306322
ctx.sendBroadcast(notifyIntent);
307323

308-
if(this.mBluetoothAdapter.isDiscovering()) {
324+
if(this.getBTAdapter().isDiscovering()) {
309325
notifyIntent = new ASAPServiceRequestNotifyIntent(
310326
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_DISCOVERY_STARTED);
311327
} else {

app/src/main/java/net/sharksystem/asap/android/bluetooth/BluetoothServerSocketThread.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,12 @@ public void run() {
4343
Log.d(this.getLogStart(), "entered wait loop - going to block in accept()");
4444
try {
4545
socket = mmServerSocket.accept();
46-
Log.d(this.getLogStart(), "new BT connection established to "
47-
+ socket.getRemoteDevice().getAddress());
46+
Log.d(this.getLogStart(),
47+
"new BT connection established to "
48+
+ socket.getRemoteDevice().getAddress());
4849

49-
// this must be handled here - see comments in BluetoothEngine
50-
if(this.btEngine.shouldConnectToMACPeer(socket.getRemoteDevice().getAddress())) {
51-
this.btEngine.handleBTSocket(socket);
52-
} else {
53-
Log.d(this.getLogStart(), "shall not talk to this peer - close");
54-
try {
55-
socket.close();
56-
}
57-
catch(IOException e) {
58-
Log.d(this.getLogStart(), "could not close - ignore: "
59-
+ e.getLocalizedMessage());
60-
}
61-
}
50+
// possible parallel connection are handle by BT engine
51+
this.btEngine.handleBTSocket(socket);
6252
} catch (IOException e) {
6353
Log.d(this.getLogStart(), "Socket's accept() method failed", e);
6454
break;

app/src/main/java/net/sharksystem/asap/android/bluetooth/FoundBTDevicesBroadcastReceiver.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.content.Intent;
88
import android.util.Log;
99

10+
import net.sharksystem.asap.ASAPException;
11+
1012
class FoundBTDevicesBroadcastReceiver extends BroadcastReceiver {
1113

1214
private final BluetoothEngine bluetoothEngine;
@@ -35,7 +37,12 @@ public void onReceive(Context context, Intent intent) {
3537
BluetoothClass btClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
3638
Log.d(this.getLogStart(), "found BT class: " + btClass.toString());
3739

38-
this.bluetoothEngine.deviceFound(device, btClass);
40+
try {
41+
this.bluetoothEngine.deviceFound(device, btClass);
42+
} catch (ASAPException e) {
43+
Log.e(this.getLogStart(),
44+
"could not handle device found: " + e.getLocalizedMessage());
45+
}
3946
}
4047
}
4148
}

app/src/main/java/net/sharksystem/asap/android/example/ASAPServiceTestActivity.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import net.sharksystem.asap.android.apps.ASAPActivity;
1414
import net.sharksystem.asap.android.R;
1515
import net.sharksystem.asap.android.apps.ASAPApplication;
16-
import net.sharksystem.asap.android.apps.ASAPOnlineMessageSenderUserSide;
16+
import net.sharksystem.asap.android.apps.ASAPOnlineMessageSenderAndroidUserSide;
1717

1818
import java.io.IOException;
1919

2020
public class ASAPServiceTestActivity extends ASAPActivity {
2121
private static final CharSequence TESTURI ="asap://testuri";
2222
private static final CharSequence TESTMESSAGE = "Hi there from asap writing activity";
23-
private ASAPOnlineMessageSenderUserSide asapOnlineSender;
23+
private ASAPOnlineMessageSenderAndroidUserSide asapOnlineSender;
2424

2525
public ASAPServiceTestActivity() {
2626
super(ASAPApplication.getASAPApplication());
@@ -68,6 +68,12 @@ else if(view == findViewById(R.id.startDiscovery)) {
6868
Log.d(this.getLogStart(), "start discover button pressed - send message");
6969
this.startBluetoothDiscovery();
7070
}
71+
else if(view == findViewById(R.id.startDiscoverableAndDiscovery)) {
72+
Log.d(this.getLogStart(),
73+
"start disoverable and discover button pressed - send messages");
74+
this.startBluetoothDiscovery();
75+
this.startBluetoothDiscoverable();
76+
}
7177
}
7278

7379
///////////////////////////////////////////////////////////////////////////////////////////
@@ -180,7 +186,7 @@ public void onAddOnlineSenderClick(View view) {
180186
return;
181187
}
182188

183-
this.asapOnlineSender = new ASAPOnlineMessageSenderUserSide(this.getASAPApplication());
189+
this.asapOnlineSender = new ASAPOnlineMessageSenderAndroidUserSide(this.getASAPApplication());
184190
this.asapStorage.attachASAPMessageAddListener(this.asapOnlineSender);
185191
} catch (IOException | ASAPException e) {
186192
Log.d(this.getLogStart(), "exception: " + e.getLocalizedMessage());

app/src/main/java/net/sharksystem/asap/android/service/ASAPConnectionLauncher.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public void run() {
6262
}
6363

6464
Log.d(this.getLogStart(), "call asapMultiEngine to handle connection");
65+
// TestConnectionHandler testConnectionHandler = new TestConnectionHandler(this.is, this.os);
66+
// testConnectionHandler.start();
6567
this.asapEngine.handleConnection(this.is, this.os);
6668
} catch (IOException | ASAPException e) {
6769
Log.d(this.getLogStart(), "while laucnhing asap connection: " + e.getLocalizedMessage());

app/src/main/java/net/sharksystem/asap/android/service/ASAPService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void startBluetoothDiscoverable() {
210210
}
211211

212212

213-
public void startBluetoothDiscovery() {
213+
public void startBluetoothDiscovery() throws ASAPException {
214214
Log.d(this.getLogStart(), "start bluetooth discovery");
215215

216216
BluetoothEngine asapBluetoothEngine =
@@ -232,7 +232,7 @@ private String getLogStart() {
232232
// status management //
233233
//////////////////////////////////////////////////////////////////////////////////////
234234

235-
public void propagateProtocolStatus() {
235+
public void propagateProtocolStatus() throws ASAPException {
236236
BluetoothEngine.getASAPBluetoothEngine(this, this)
237237
.propagateStatus(this);
238238
// Wifi.propagateStatus();
@@ -309,6 +309,7 @@ public void onlinePeersChanged(MultiASAPEngineFS multiASAPEngineFS) {
309309

310310
// broadcast
311311
String serializedOnlinePeers = Helper.collection2String(multiASAPEngineFS.getOnlinePeers());
312+
Log.d(this.getLogStart(), "online peers serialized: " + serializedOnlinePeers);
312313

313314
ASAPServiceRequestNotifyIntent intent =
314315
new ASAPServiceRequestNotifyIntent(

0 commit comments

Comments
 (0)