Skip to content

Commit 2b8e267

Browse files
committed
Add check for currently active connection to shouldConnect
1 parent 9a3838f commit 2b8e267

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

app/src/main/java/net/sharksystem/asap/android/lora/LoRaBTInputOutputStream.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ public LoRaASAPInputStream getASAPInputStream(String mac) {
113113
return this.getASAPInputStream(mac);
114114
}
115115

116+
/**
117+
* Checks if there is an active {@link LoRaASAPInputStream} for this mac
118+
* @param macAddress
119+
* @return
120+
*/
121+
public boolean hasASAPInputStream(String macAddress) {
122+
return this.loRaASAPInputStreams.containsKey(macAddress);
123+
}
124+
116125
public LoRaBTInputStream getInputStream() {
117126
return is;
118127
}

app/src/main/java/net/sharksystem/asap/android/lora/LoRaCommunicationManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class LoRaCommunicationManager extends Thread {
2525
private static final String CLASS_LOG_TAG = "ASAPLoRaCommManager";
2626
private static final long FLUSH_BUFFER_TIMEOUT = 250;
27-
private static final long DISCOVER_MESSAGE_TIMEOUT = 60 * 1000; //60s in ms
27+
private static final long DISCOVER_MESSAGE_TIMEOUT = 5 * 60 * 1000; //5 minutes in ms
2828
private static final long CONNECTION_ACTIVE_TIMEOUT = DISCOVER_MESSAGE_TIMEOUT * 10; //60s in ms
2929
private static LoRaBTInputOutputStream ioStream = null;
3030
private BluetoothDevice btDevice;
@@ -68,6 +68,10 @@ public OutputStream getASAPOutputStream(String mac) {
6868
return this.ioStream.getASAPOutputStream(mac);
6969
}
7070

71+
public boolean hasASAPInputStream(String macAddress) {
72+
return this.ioStream.hasASAPInputStream(macAddress);
73+
}
74+
7175
public InputStream getASAPInputStream(String mac) {
7276
return this.ioStream.getASAPInputStream(mac);
7377
}

app/src/main/java/net/sharksystem/asap/android/lora/LoRaEngine.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,24 @@ public void checkConnectionStatus() {
9999
* @param macAddress
100100
*/
101101
void tryConnect(String macAddress) {
102-
if (this.shouldConnectToMACPeer(macAddress)) {
102+
if (this.shouldConnectToMACPeer(macAddress) && !this.hasActiveConnectionToMACPeer(macAddress)) {
103+
Log.d(this.CLASS_LOG_TAG, "Connection to "
104+
+ macAddress + " not yet established. Trying to start an ASAPSession.");
103105
this.launchASAPConnection(macAddress, this.loRaCommunicationManager.getASAPInputStream(macAddress), this.loRaCommunicationManager.getASAPOutputStream(macAddress));
104106
} else {
105107
Log.d(this.CLASS_LOG_TAG, "Connection to "
106108
+ macAddress + " not needed.");
107109
}
108110
}
111+
112+
/**
113+
* Test if there is a currently active Connection to a macAddress.
114+
* @param macAddress
115+
* @return
116+
*/
117+
private boolean hasActiveConnectionToMACPeer(String macAddress) {
118+
if(this.loRaCommunicationManager != null && this.loRaCommunicationManager.isAlive())
119+
return this.loRaCommunicationManager.hasASAPInputStream(macAddress);
120+
return false;
121+
}
109122
}

0 commit comments

Comments
 (0)