Skip to content

Commit 6847efc

Browse files
unknownunknown
authored andcommitted
Fixed up the warnings and some bugs
1 parent 8862f21 commit 6847efc

File tree

1 file changed

+19
-48
lines changed

1 file changed

+19
-48
lines changed

src/com/tokudu/demo/PushService.java

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package com.tokudu.demo;
22

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-
103
import com.ibm.mqtt.IMqttClient;
114
import com.ibm.mqtt.MqttClient;
125
import com.ibm.mqtt.MqttException;
13-
import com.ibm.mqtt.MqttNotConnectedException;
146
import com.ibm.mqtt.MqttPersistence;
157
import com.ibm.mqtt.MqttPersistenceException;
168
import com.ibm.mqtt.MqttSimpleCallback;
@@ -25,13 +17,9 @@
2517
import android.content.Intent;
2618
import android.content.IntentFilter;
2719
import android.content.SharedPreferences;
28-
import android.content.SharedPreferences.Editor;
2920
import android.net.ConnectivityManager;
3021
import android.net.NetworkInfo;
31-
import android.os.Bundle;
3222
import android.os.IBinder;
33-
import android.os.Message;
34-
import android.provider.Settings.Secure;
3523
import android.util.Log;
3624

3725
/*
@@ -265,8 +253,13 @@ private synchronized void keepAlive() {
265253
// Send a keep alive, if there is a connection.
266254
if (mStarted == true && mConnection != null) {
267255
mConnection.sendKeepAlive();
256+
} else {
257+
reconnectIfNecessary();
268258
}
269-
} catch (IOException e) {
259+
} catch (MqttException e) {
260+
mConnection.disconnect();
261+
mConnection = null;
262+
scheduleReconnect(System.currentTimeMillis());
270263
Log.e(TAG, "Failed to send a keepalive", e);
271264
}
272265
}
@@ -437,49 +430,31 @@ public void disconnect() {
437430
* Send a request to the message broker to be sent messages published with
438431
* the specified topic name. Wildcards are allowed.
439432
*/
440-
private void subscribeToTopic(String topicName) {
433+
private void subscribeToTopic(String topicName) throws MqttException {
441434

442435
if ((mqttClient == null) || (mqttClient.isConnected() == false)) {
443436
// quick sanity check - don't try and subscribe if we don't have
444437
// a connection
445438
log("Connection error" + "No connection");
446439
} 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);
457442
}
458443
}
459444
/*
460445
* Sends a message to the message broker, requesting that it be published
461446
* to the specified topic.
462447
*/
463-
private void publishToTopic(String topicName, String message) {
448+
private void publishToTopic(String topicName, String message) throws MqttException {
464449
if ((mqttClient == null) || (mqttClient.isConnected() == false)) {
465450
// quick sanity check - don't try and publish if we don't have
466451
// a connection
467452
Log.e(TAG, "no connection publish");
468453
} 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);
483458
}
484459
}
485460

@@ -500,17 +475,13 @@ public void connectionLost() throws Exception {
500475
* Called when we receive a message from the message broker.
501476
*/
502477
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);
511482
}
512483

513-
public void sendKeepAlive() throws IOException{
484+
public void sendKeepAlive() throws MqttException {
514485
// publish to a keep-alive topic
515486
publishToTopic(MQTT_CLIENT_ID + "/keepalive", mPrefs.getString(PREF_DEVICE_ID, ""));
516487
}

0 commit comments

Comments
 (0)