Skip to content

Commit 58cc2c4

Browse files
committed
working on Bluetooth problem: Switching off BT doesn't kill BT sockets - quite weird
1 parent 053f66a commit 58cc2c4

5 files changed

Lines changed: 60 additions & 6 deletions

File tree

app/libs/ASAP_Engine_0.5.0.jar

75 Bytes
Binary file not shown.

app/src/main/java/net/sharksystem/asap/android/apps/ASAPApplication.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,11 @@ public void setOnlinePeersList(List<CharSequence> peerList) {
322322

323323
Log.d(this.getLogStart(), sb.toString());
324324
if(onlinePeerList.size() < peerList.size()) {
325-
Toast.makeText(this.getActivity(), "new online connections", Toast.LENGTH_SHORT);
325+
Toast.makeText(this.getActivity(),
326+
"new online connections", Toast.LENGTH_SHORT).show();
326327
} else {
327-
Toast.makeText(this.getActivity(), "online connections changed", Toast.LENGTH_SHORT);
328+
Toast.makeText(this.getActivity(),
329+
"online connections changed", Toast.LENGTH_SHORT).show();
328330
}
329331

330332
this.onlinePeerList = peerList;

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.util.Log;
1111

1212
import net.sharksystem.asap.ASAPException;
13-
import net.sharksystem.asap.android.service.ASAPConnectionLauncher;
1413
import net.sharksystem.asap.android.service.MacLayerEngine;
1514
import net.sharksystem.asap.android.service.ASAPService;
1615
import net.sharksystem.asap.android.Util;
@@ -165,6 +164,44 @@ private void setup() {
165164
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_ENVIRONMENT_STARTED));
166165
}
167166

167+
168+
void acceptServerSocketKilled() {
169+
Log.d(this.getLogStart(), "was told accept socket died - shutdown");
170+
// server socket was killed - that most probably because BT was switched off by users
171+
try {
172+
this.shutdown();
173+
} catch (ASAPException e) {
174+
Log.d(this.getLogStart(), "problems with shutdown: " + e.getLocalizedMessage());
175+
}
176+
177+
/* BT on Android shows that weired behaviour that process can write into a BT socket
178+
even when BT is off. That's impossible - but kill all sockets they are only zombies
179+
*/
180+
181+
for(BluetoothSocket socket : this.openSockets.values()) {
182+
String name = "no remote Device";
183+
String address = name;
184+
BluetoothDevice remoteDevice = socket.getRemoteDevice();
185+
if( remoteDevice != null) {
186+
name = remoteDevice.getName();
187+
address = remoteDevice.getAddress();
188+
}
189+
190+
Log.d(this.getLogStart(), "kill zombie socket "
191+
+ "name: " + name
192+
+ " | address: " + address
193+
+ " | isConnected: " + socket.isConnected());
194+
try {
195+
socket.getInputStream().close();
196+
socket.getOutputStream().close();
197+
} catch (IOException e) {
198+
Log.d(this.getLogStart(), "could not close");
199+
}
200+
}
201+
202+
this.openSockets = new HashMap<>();
203+
}
204+
168205
public void startDiscoverable() {
169206
this.startDiscoverable(BluetoothEngine.DEFAULT_VISIBILITY_TIME);
170207
}
@@ -301,9 +338,7 @@ synchronized void handleBTSocket(BluetoothSocket socket) throws IOException {
301338
// remember that new connection
302339
this.openSockets.put(address, socket);
303340

304-
// set up new ASAP Session on that connection
305-
new ASAPConnectionLauncher(socket.getInputStream(), socket.getOutputStream(),
306-
this.getAsapService().getASAPEngine()).start();
341+
this.launchASAPConnection(address, socket.getInputStream(), socket.getOutputStream());
307342
}
308343

309344
public void propagateStatus(Context ctx) throws ASAPException {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public void run() {
5151
this.btEngine.handleBTSocket(socket);
5252
} catch (IOException e) {
5353
Log.d(this.getLogStart(), "Socket's accept() method failed", e);
54+
// tell engine
55+
this.btEngine.acceptServerSocketKilled();
5456
break;
5557
}
5658
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import android.content.Context;
44
import android.util.Log;
55

6+
import net.sharksystem.asap.MultiASAPEngineFS;
7+
8+
import java.io.InputStream;
9+
import java.io.OutputStream;
610
import java.util.Date;
711
import java.util.HashMap;
812
import java.util.Map;
@@ -96,4 +100,15 @@ public boolean shouldConnectToMACPeer(String macAddress) {
96100
private String getLogStart() {
97101
return "ASAPMacLayerEngine";
98102
}
103+
104+
protected void launchASAPConnection(
105+
String address, InputStream inputStream, OutputStream outputStream) {
106+
107+
// set up new ASAP Session on that connection
108+
ASAPConnectionLauncher asapConnectionLauncher =
109+
new ASAPConnectionLauncher(
110+
inputStream, outputStream, this.getAsapService().getASAPEngine());
111+
112+
asapConnectionLauncher.start();
113+
}
99114
}

0 commit comments

Comments
 (0)