use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.
the class ApplozicMqttService method connectPublish.
public synchronized void connectPublish(final String userKeyString, final String deviceKeyString, final String status) {
try {
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
MqttMessage message = new MqttMessage();
message.setRetained(false);
message.setPayload((userKeyString + "," + deviceKeyString + "," + status).getBytes());
Utils.printLog(context, TAG, "UserKeyString,DeviceKeyString,status:" + userKeyString + "," + deviceKeyString + "," + status);
message.setQos(0);
client.publish(STATUS, message);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.
the class ApplozicMqttService method subscribe.
public synchronized void subscribe() {
if (!Utils.isInternetAvailable(context)) {
return;
}
final String deviceKeyString = MobiComUserPreference.getInstance(context).getDeviceKeyString();
final String userKeyString = MobiComUserPreference.getInstance(context).getSuUserKeyString();
if (TextUtils.isEmpty(deviceKeyString) || TextUtils.isEmpty(userKeyString)) {
return;
}
try {
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
connectPublish(userKeyString, deviceKeyString, "1");
subscribeToConversation();
if (client != null) {
client.setCallback(ApplozicMqttService.this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.
the class ApplozicMqttService method subscribeToOpenGroupTopic.
public synchronized void subscribeToOpenGroupTopic(Channel channel) {
try {
String currentId = null;
if (channel != null) {
currentId = String.valueOf(channel.getKey());
}
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
client.subscribe(OPEN_GROUP + getApplicationKey(context) + "-" + currentId, 0);
Utils.printLog(context, TAG, "Subscribed to Open group: " + OPEN_GROUP + getApplicationKey(context) + "-" + currentId);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.
the class ApplozicMqttService method connect.
private MqttClient connect() {
String userId = MobiComUserPreference.getInstance(context).getUserId();
try {
if (TextUtils.isEmpty(userId)) {
return client;
}
if (client == null) {
client = new MqttClient(getMqttBaseUrl(), userId + "-" + new Date().getTime(), memoryPersistence);
}
if (!client.isConnected()) {
Utils.printLog(context, TAG, "Connecting to mqtt...");
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(60);
options.setWill(STATUS, (MobiComUserPreference.getInstance(context).getSuUserKeyString() + "," + MobiComUserPreference.getInstance(context).getDeviceKeyString() + "," + "0").getBytes(), 0, true);
client.setCallback(ApplozicMqttService.this);
client.connect(options);
}
} catch (MqttException e) {
Utils.printLog(context, TAG, "Connecting already in progress.");
} catch (Exception e) {
e.printStackTrace();
}
return client;
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.
the class ApplozicMqttService method unSubscribeToOpenGroupTopic.
public synchronized void unSubscribeToOpenGroupTopic(Channel channel) {
try {
String currentId = null;
if (channel != null) {
currentId = String.valueOf(channel.getKey());
}
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
client.unsubscribe(OPEN_GROUP + getApplicationKey(context) + "-" + currentId);
Utils.printLog(context, TAG, "UnSubscribed to topic: " + OPEN_GROUP + getApplicationKey(context) + "-" + currentId);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations