1
1
package com .tokudu .demo ;
2
2
3
- import java .io .IOException ;
4
- import java .io .InputStream ;
5
- import java .io .OutputStream ;
6
- import java .net .InetSocketAddress ;
7
- import java .net .Socket ;
8
- import java .util .Date ;
9
-
10
3
import com .ibm .mqtt .IMqttClient ;
11
4
import com .ibm .mqtt .MqttClient ;
12
5
import com .ibm .mqtt .MqttException ;
13
- import com .ibm .mqtt .MqttNotConnectedException ;
14
6
import com .ibm .mqtt .MqttPersistence ;
15
7
import com .ibm .mqtt .MqttPersistenceException ;
16
8
import com .ibm .mqtt .MqttSimpleCallback ;
25
17
import android .content .Intent ;
26
18
import android .content .IntentFilter ;
27
19
import android .content .SharedPreferences ;
28
- import android .content .SharedPreferences .Editor ;
29
20
import android .net .ConnectivityManager ;
30
21
import android .net .NetworkInfo ;
31
- import android .os .Bundle ;
32
22
import android .os .IBinder ;
33
- import android .os .Message ;
34
- import android .provider .Settings .Secure ;
35
23
import android .util .Log ;
36
24
37
25
/*
@@ -265,8 +253,13 @@ private synchronized void keepAlive() {
265
253
// Send a keep alive, if there is a connection.
266
254
if (mStarted == true && mConnection != null ) {
267
255
mConnection .sendKeepAlive ();
256
+ } else {
257
+ reconnectIfNecessary ();
268
258
}
269
- } catch (IOException e ) {
259
+ } catch (MqttException e ) {
260
+ mConnection .disconnect ();
261
+ mConnection = null ;
262
+ scheduleReconnect (System .currentTimeMillis ());
270
263
Log .e (TAG , "Failed to send a keepalive" , e );
271
264
}
272
265
}
@@ -437,49 +430,31 @@ public void disconnect() {
437
430
* Send a request to the message broker to be sent messages published with
438
431
* the specified topic name. Wildcards are allowed.
439
432
*/
440
- private void subscribeToTopic (String topicName ) {
433
+ private void subscribeToTopic (String topicName ) throws MqttException {
441
434
442
435
if ((mqttClient == null ) || (mqttClient .isConnected () == false )) {
443
436
// quick sanity check - don't try and subscribe if we don't have
444
437
// a connection
445
438
log ("Connection error" + "No connection" );
446
439
} else {
447
- try {
448
- String [] topics = { topicName };
449
- mqttClient .subscribe (topics , MQTT_QUALITIES_OF_SERVICE );
450
- } catch (MqttNotConnectedException e ) {
451
- Log .e (TAG , "Connection error" + e .getMessage (), e );
452
- } catch (IllegalArgumentException e ) {
453
- Log .e (TAG , "Connection error" + e .getMessage (), e );
454
- } catch (MqttException e ) {
455
- Log .e (TAG , "MQQTEXCEPTION" + (e .getMessage () == null ? e .getMessage ():" NULL" ), e );
456
- }
440
+ String [] topics = { topicName };
441
+ mqttClient .subscribe (topics , MQTT_QUALITIES_OF_SERVICE );
457
442
}
458
443
}
459
444
/*
460
445
* Sends a message to the message broker, requesting that it be published
461
446
* to the specified topic.
462
447
*/
463
- private void publishToTopic (String topicName , String message ) {
448
+ private void publishToTopic (String topicName , String message ) throws MqttException {
464
449
if ((mqttClient == null ) || (mqttClient .isConnected () == false )) {
465
450
// quick sanity check - don't try and publish if we don't have
466
451
// a connection
467
452
Log .e (TAG , "no connection publish" );
468
453
} else {
469
- try {
470
- mqttClient .publish (topicName ,
471
- message .getBytes (),
472
- MQTT_QUALITY_OF_SERVICE ,
473
- MQTT_RETAINED_PUBLISH );
474
- } catch (MqttPersistenceException e ) {
475
- Log .e (TAG , "Connection error" + e .getMessage (), e );
476
- } catch (MqttNotConnectedException e ) {
477
- Log .e (TAG , "Connection error" + e .getMessage (), e );
478
- } catch (IllegalArgumentException e ){
479
- Log .e (TAG , "Connection error" + e .getMessage (), e );
480
- } catch (MqttException e ) {
481
- log ("Connection error" + e .getMessage ());
482
- }
454
+ mqttClient .publish (topicName ,
455
+ message .getBytes (),
456
+ MQTT_QUALITY_OF_SERVICE ,
457
+ MQTT_RETAINED_PUBLISH );
483
458
}
484
459
}
485
460
@@ -500,17 +475,13 @@ public void connectionLost() throws Exception {
500
475
* Called when we receive a message from the message broker.
501
476
*/
502
477
public void publishArrived (String topicName , byte [] payload , int qos , boolean retained ) {
503
- try {
504
- // Show a notification
505
- String s = new String (payload );
506
- showNotification (s );
507
- log ("Got message: " + s );
508
- } catch (Exception exc ) {
509
- Log .e ("recieving error:" ,exc .getMessage ());
510
- }
478
+ // Show a notification
479
+ String s = new String (payload );
480
+ showNotification (s );
481
+ log ("Got message: " + s );
511
482
}
512
483
513
- public void sendKeepAlive () throws IOException {
484
+ public void sendKeepAlive () throws MqttException {
514
485
// publish to a keep-alive topic
515
486
publishToTopic (MQTT_CLIENT_ID + "/keepalive" , mPrefs .getString (PREF_DEVICE_ID , "" ));
516
487
}
0 commit comments