1 package com.example.bee_honeyt;
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.database.sqlite.SQLiteDatabase;
6 import android.database.sqlite.SQLiteOpenHelper;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.Message;
10 import android.util.Log;
12 import org.eclipse.paho.android.service.MqttAndroidClient;
13 import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
14 import org.eclipse.paho.client.mqttv3.IMqttActionListener;
15 import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
16 import org.eclipse.paho.client.mqttv3.IMqttToken;
17 import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
18 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
19 import org.eclipse.paho.client.mqttv3.MqttException;
20 import org.eclipse.paho.client.mqttv3.MqttMessage;
40 private static final String
TAG =
"_Communication";
69 Log.v(
TAG,
"[Communication()] clientId = " + clientId);
111 public void connectComplete(
boolean b, String s)
113 Log.w(
TAG,
"[connectComplete()] serverUri = " + s +
" connecte = " +
mqttAndroidClient.isConnected());
114 Message msg = Message.obtain();
115 Bundle bundle =
new Bundle();
122 public void connectionLost(Throwable throwable)
124 Log.w(
TAG,
"[connectionLost()]");
125 Message msg = Message.obtain();
126 Bundle b =
new Bundle();
133 public void messageArrived(String topic, MqttMessage mqttMessage)
throws Exception
135 Log.w(
TAG,
"[messageArrived()] topic = " + topic +
" message = " + mqttMessage.toString());
136 Message msg = Message.obtain();
137 Bundle b =
new Bundle();
139 b.putString(
"topic", topic);
140 b.putString(
"message", mqttMessage.toString());
146 public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)
148 Log.w(
TAG,
"[deliveryComplete()]");
171 MqttConnectOptions mqttConnectOptions =
new MqttConnectOptions();
172 mqttConnectOptions.setAutomaticReconnect(
true);
173 mqttConnectOptions.setCleanSession(
false);
174 mqttConnectOptions.setUserName(
username);
175 mqttConnectOptions.setPassword(
password.toCharArray());
179 Log.d(
TAG,
"[connecter()] serverUri = " + serverUri +
" clientId = " + clientId);
183 public void onSuccess(IMqttToken asyncActionToken)
185 DisconnectedBufferOptions disconnectedBufferOptions =
new DisconnectedBufferOptions();
186 disconnectedBufferOptions.setBufferEnabled(
true);
187 disconnectedBufferOptions.setBufferSize(100);
188 disconnectedBufferOptions.setPersistBuffer(
false);
189 disconnectedBufferOptions.setDeleteOldestMessages(
false);
191 Log.d(
TAG,
"[onSuccess()] serverUri = " + serverUri +
" clientId = " + clientId);
195 public void onFailure(IMqttToken asyncActionToken, Throwable exception)
197 Log.d(
TAG,
"[onFailure()] serverUri = " + serverUri +
" clientId = " + clientId +
" exception = " + exception.toString());
201 catch (MqttException e)
216 Log.d(
TAG,
"[deconnecter()] serverUri = " + serverUri +
" clientId = " + clientId);
220 disconToken.setActionCallback(
new IMqttActionListener()
223 public void onSuccess(IMqttToken asyncActionToken)
225 Log.d(
TAG,
"[onSuccess()] serverUri = " + serverUri +
" clientId = " + clientId);
229 public void onFailure(IMqttToken asyncActionToken, Throwable exception)
231 Log.d(
TAG,
"[onFailure()] serverUri = " + serverUri +
" clientId = " + clientId +
" exception = " + exception.toString());
235 catch (MqttException e)
268 final String topicTTN = clientId +
"/devices/" + deviceID +
"/up";
269 Log.w(
TAG,
"[souscrireTopic()] topic = " + topicTTN);
272 final boolean[] retour = {
false};
276 public void onSuccess(IMqttToken asyncActionToken)
278 Log.w(
TAG,
"[onSuccess()] topic = " + topicTTN);
283 public void onFailure(IMqttToken asyncActionToken, Throwable exception)
285 Log.w(
TAG,
"[onFailure()] topic = " + topicTTN);
291 catch (MqttException e)
293 Log.w(
TAG,
"Erreur topic = " + topicTTN);
311 final String topicTTN = clientId +
"/devices/" + deviceID +
"/up";
312 Log.w(
TAG,
"[unsouscrireTopic()] topic = " + topicTTN);
315 final boolean[] retour = {
false};
320 catch (MqttException e)
322 Log.w(
TAG,
"Erreur topic = " + topicTTN);