Skip to content

Commit 05ba943

Browse files
committed
still as sync problem in settings activity with bt discoverable button - rest pretty stable
1 parent a77de04 commit 05ba943

5 files changed

Lines changed: 103 additions & 24 deletions

File tree

app/src/main/java/net/sharksystem/asap/android/ASAPServiceMethods.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public class ASAPServiceMethods {
77
public static final int ADD_MESSAGE = 0;
8+
public static final int ASK_PROTOCOL_STATUS = 9;
89

910
public static final int START_WIFI_DIRECT = 1;
1011
public static final int STOP_WIFI_DIRECT = 2;

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyBroadcastReceiver;
2424
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
2525

26+
import java.util.ArrayList;
27+
import java.util.List;
28+
2629
public class ASAPActivity extends AppCompatActivity implements
2730
ASAPServiceRequestListener, ASAPServiceNotificationListener {
2831

@@ -96,17 +99,26 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
9699
if(resultCode == this.visibilityTime) {
97100
Log.d(this.getLogStart(), "user allowed BT discoverability for seconds: "
98101
+ this.visibilityTime);
102+
103+
// notify
104+
Log.d(this.getLogStart(), "call aspNotifyBTDiscoverableStarted()");
105+
this.aspNotifyBTDiscoverableStarted();
99106
}
100107
}
101108

109+
private List<Message> messageStorage = null;
102110
protected void sendMessage2Service(int messageNumber) {
111+
Message msg = Message.obtain(null, messageNumber, 0, 0);
112+
103113
if(this.mService == null) {
104-
Log.d(this.getLogStart(), "service not yet available - cannot send message");
105-
return;
114+
Log.d(this.getLogStart(), "service not yet available - cannot send but store message");
115+
if(this.messageStorage == null) {
116+
this.messageStorage = new ArrayList<Message>();
117+
}
118+
this.messageStorage.add(msg);
119+
} else {
120+
this.sendMessage2Service(msg);
106121
}
107-
108-
Message msg = Message.obtain(null, messageNumber, 0, 0);
109-
this.sendMessage2Service(msg);
110122
}
111123

112124
public void sendMessage2Service(Message msg) {
@@ -165,6 +177,11 @@ public void stopASAPEngineBroadcasts() {
165177
this.sendMessage2Service(ASAPServiceMethods.STOP_BROADCASTS);
166178
}
167179

180+
public void refreshProtocolStatus() {
181+
Log.d(this.getLogStart(), "send message to ask for protocol status broadcasts");
182+
this.sendMessage2Service(ASAPServiceMethods.ASK_PROTOCOL_STATUS);
183+
}
184+
168185
/////////////////////////////////////////////////////////////////////////////////////
169186
// asap service notification broadcast receiver management //
170187
/////////////////////////////////////////////////////////////////////////////////////
@@ -276,6 +293,9 @@ protected void onStop() {
276293
this.shutdownASAPServiceNotificationBroadcastReceiver();
277294
this.shutdownASAPReceivedBroadcastReceiver();
278295
this.unbindServices();
296+
297+
// forget stored messages
298+
this.messageStorage = null;
279299
// stop protocols?
280300
}
281301

@@ -286,6 +306,9 @@ protected void onDestroy() {
286306
this.unbindServices();
287307
this.asapApplication.activityDestroyed(this);
288308

309+
// forget stored messages
310+
this.messageStorage = null;
311+
289312
/*
290313
this.sendMessage2Service(ASAPServiceMethods.STOP_WIFI_DIRECT);
291314
this.sendMessage2Service(ASAPServiceMethods.STOP_BLUETOOTH);
@@ -326,8 +349,15 @@ public void onServiceConnected(ComponentName className, IBinder service) {
326349
mService = new Messenger(service);
327350
mBound = true;
328351

329-
Log.d(getLogStart(), "connection established - asap engine can start broadcast");
330-
startASAPEngineBroadcasts();
352+
Log.d(getLogStart(), "connection established");
353+
if(messageStorage != null && messageStorage.size() > 0) {
354+
Log.d(getLogStart(), "send stored messages | #msg = " + messageStorage.size());
355+
for(Message msg : messageStorage) {
356+
sendMessage2Service(msg);
357+
}
358+
359+
messageStorage = null;
360+
}
331361
}
332362

333363
public void onServiceDisconnected(ComponentName className) {
@@ -365,7 +395,7 @@ public void aspNotifyBTDiscoveryStarted() {
365395

366396
@Override
367397
public void aspNotifyBTDiscoveryStopped() {
368-
this.asapApplication.setBTDiscovery(true);
398+
this.asapApplication.setBTDiscovery(false);
369399
}
370400

371401
////////////////////////////////////////////////////////////////////////////////

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.sharksystem.asap.android.service.MacLayerEngine;
1515
import net.sharksystem.asap.android.service.ASAPService;
1616
import net.sharksystem.asap.android.Util;
17+
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyBroadcastReceiver;
1718
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
1819

1920
import java.io.IOException;
@@ -50,6 +51,13 @@ public static BluetoothEngine getASAPBluetoothEngine() {
5051

5152
private BluetoothEngine(ASAPService ASAPService, Context context) {
5253
super(ASAPService, context);
54+
55+
// get default bt adapter
56+
this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
57+
if (mBluetoothAdapter == null) {
58+
// Device doesn't support Bluetooth
59+
Log.i(this.getLogStart(), "device does not support bluetooth - give up");
60+
}
5361
}
5462

5563
private String getLogStart() {
@@ -81,8 +89,7 @@ private void setup() {
8189
// setup bt environment //
8290
///////////////////////////////////////////////////////////////////////////////////////
8391

84-
// get default bt adapter
85-
this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
92+
// reference was set in constructor - or not
8693
if (mBluetoothAdapter == null) {
8794
// Device doesn't support Bluetooth
8895
Log.i(this.getLogStart(), "device does not support bluetooth - give up");
@@ -275,4 +282,30 @@ void handleBTSocket(BluetoothSocket socket) throws IOException {
275282
new ASAPSession(socket.getInputStream(), socket.getOutputStream(),
276283
this.getAsapService().getASAPEngine(), this).start();
277284
}
285+
286+
public void propagateStatus(Context ctx) {
287+
Log.d(this.getLogStart(), "going to send status broadcast messages");
288+
ASAPServiceRequestNotifyIntent notifyIntent = null;
289+
290+
// Bluetooth running?
291+
if(this.mBluetoothAdapter.isEnabled()) {
292+
notifyIntent = new ASAPServiceRequestNotifyIntent(
293+
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_ENVIRONMENT_STARTED);
294+
} else {
295+
notifyIntent = new ASAPServiceRequestNotifyIntent(
296+
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_ENVIRONMENT_STOPPED);
297+
}
298+
299+
ctx.sendBroadcast(notifyIntent);
300+
301+
if(this.mBluetoothAdapter.isDiscovering()) {
302+
notifyIntent = new ASAPServiceRequestNotifyIntent(
303+
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_DISCOVERY_STARTED);
304+
} else {
305+
notifyIntent = new ASAPServiceRequestNotifyIntent(
306+
ASAPServiceRequestNotifyIntent.ASAP_NOTIFY_BT_DISCOVERY_STOPPED);
307+
}
308+
309+
ctx.sendBroadcast(notifyIntent);
310+
}
278311
}

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
class ASAPMessageHandler extends Handler {
1616
private static final String LOGSTART = "AASPMessageHandler";
17-
private ASAPService ASAPService;
17+
private ASAPService asapService;
1818

1919
ASAPMessageHandler(ASAPService context) {
20-
this.ASAPService = context;
20+
this.asapService = context;
2121
}
2222

2323
@Override
@@ -51,7 +51,7 @@ public void handleMessage(Message msg) {
5151

5252
try {
5353
ASAPOnlineMessageSender onlineMessageSender =
54-
this.ASAPService.getASAPOnlineMessageSender();
54+
this.asapService.getASAPOnlineMessageSender();
5555

5656
if(onlineMessageSender == null) {
5757
Log.w(LOGSTART, "no online message sender in place");
@@ -61,7 +61,7 @@ public void handleMessage(Message msg) {
6161
}
6262

6363
/*
64-
MultiASAPEngineFS asapMulti = this.ASAPService.getASAPEngine();
64+
MultiASAPEngineFS asapMulti = this.asapService.getASAPEngine();
6565
ASAPEngine asapEngine = asapMulti.getEngineByFormat(format);
6666
6767
if (asapEngine == null) {
@@ -73,11 +73,11 @@ public void handleMessage(Message msg) {
7373
// simulate broadcast
7474
Intent intent = new ASAPBroadcastIntent(
7575
ASAP.UNKNOWN_USER,
76-
this.ASAPService.getASAPRootFolderName(),
76+
this.asapService.getASAPRootFolderName(),
7777
uri,
7878
asapEngine.getEra());
7979
80-
this.ASAPService.sendBroadcast(intent);
80+
this.asapService.sendBroadcast(intent);
8181
}
8282
*/
8383
} catch (IOException e) {
@@ -88,35 +88,39 @@ public void handleMessage(Message msg) {
8888
break;
8989

9090
case ASAPServiceMethods.START_WIFI_DIRECT:
91-
this.ASAPService.startWifiDirect();
91+
this.asapService.startWifiDirect();
92+
break;
93+
94+
case ASAPServiceMethods.ASK_PROTOCOL_STATUS:
95+
this.asapService.propagateProtocolStatus();
9296
break;
9397

9498
case ASAPServiceMethods.STOP_WIFI_DIRECT:
95-
this.ASAPService.stopWifiDirect();
99+
this.asapService.stopWifiDirect();
96100
break;
97101

98102
case ASAPServiceMethods.START_BLUETOOTH:
99-
this.ASAPService.startBluetooth();
103+
this.asapService.startBluetooth();
100104
break;
101105

102106
case ASAPServiceMethods.STOP_BLUETOOTH:
103-
this.ASAPService.stopBluetooth();
107+
this.asapService.stopBluetooth();
104108
break;
105109

106110
case ASAPServiceMethods.START_BLUETOOTH_DISCOVERABLE:
107-
this.ASAPService.startBluetoothDiscoverable();
111+
this.asapService.startBluetoothDiscoverable();
108112
break;
109113

110114
case ASAPServiceMethods.START_BLUETOOTH_DISCOVERY:
111-
this.ASAPService.startBluetoothDiscovery();
115+
this.asapService.startBluetoothDiscovery();
112116
break;
113117

114118
case ASAPServiceMethods.START_BROADCASTS:
115-
this.ASAPService.resumeBroadcasts();
119+
this.asapService.resumeBroadcasts();
116120
break;
117121

118122
case ASAPServiceMethods.STOP_BROADCASTS:
119-
this.ASAPService.pauseBroadcasts();
123+
this.asapService.pauseBroadcasts();
120124
break;
121125

122126
default:

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ public void startBluetoothDiscovery() {
207207
}
208208

209209

210+
//////////////////////////////////////////////////////////////////////////////////////
211+
// status management //
212+
//////////////////////////////////////////////////////////////////////////////////////
213+
214+
public void propagateProtocolStatus() {
215+
BluetoothEngine.getASAPBluetoothEngine(this, this)
216+
.propagateStatus(this);
217+
// Wifi.propagateStatus();
218+
}
219+
220+
210221
//////////////////////////////////////////////////////////////////////////////////////
211222
// chunk receiving management //
212223
//////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)