Android : MQTT Unable to connect to server when using 3G

on Friday, August 29, 2014


When I try to run my app on a 3G connection it tries to connect for about 1 or 2 minutes and then it throws an exception, it works perfectly fine on wifi but not on 3G. I have tried with different peoples phone on different networks. Why wont it connect?


The exception it throws i an MQTT exception



Unable to connect to server


This is the MQTT class that i called it creates a new thread:



public class MQTTService extends Service {

ContextWrapper c = this;
private MqttClient mqttClient;

public IBinder onBind(Intent intent) {
return null;
}


@Override
public void onStart(Intent intent, int startId) {

Thread background = new Thread(new Runnable() {
final String BROKER_URL = "tcp://gateway.thebroker.com:1883";
final String clientId = "android-client90";

public void run() {
Looper.prepare();
try{


MqttClient mqttClient = new MqttClient(BROKER_URL, clientId, new MemoryPersistence());
MqttConnectOptions opts = new MqttConnectOptions();
opts.setKeepAliveInterval(600);
opts.setUserName("nabin");
opts.setPassword("M4rk3r".toCharArray());



Log.e("Before Callback", "Connecting..."); // Code works till here if callback is set then errors galore
//mqttClient.setCallback(handler.obtainMessage());
mqttClient.setCallback(new PushCallback(c));

mqttClient.connect(opts);

mqttClient.subscribe("house/pill/notification/bad");
mqttClient.subscribe("house/pill/notification/good");
mqttClient.subscribe("house/pill/notification/skip");
mqttClient.subscribe("house/pill/notification/alert");
mqttClient.subscribe("house/pill/notification/snooze");
mqttClient.subscribe("house/pill/schedule/response");
mqttClient.subscribe("house/pill/nextPill/response");

Log.e("CONNECTED!", "COOOONNECTED!!!!!");
}catch(MqttException e){
Log.e("Internet Error", "No connection available " + e.getMessage() );
}catch(Exception e){
Log.e("ERRRRRORRRRR ", e.getMessage());
}
}
});
background.start();
}
}


Thank you for any help!


0 comments:

Post a Comment