Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MqttHelper {
- public MqttAndroidClient mqttAndroidClient;
- final String serverUri = "ws://10.3.186.12:9001";
- final String clientId = "ExampleAndroidClient";
- final String subscriptionTopic = "MQTT";
- final String username = "xxxxxxx";
- final String password = "yyyyyyyyyy";
- public MqttHelper(Context context){
- mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
- mqttAndroidClient.setCallback(new MqttCallbackExtended() {
- @Override
- public void connectComplete(boolean b, String s) {
- Log.w("MQTT", s);
- }
- @Override
- public void connectionLost(Throwable throwable) {
- }
- @Override
- public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
- Log.w("MQTT", mqttMessage.toString());
- }
- @Override
- public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
- }
- });
- connect();
- }
- public void setCallback(MqttCallbackExtended callback) {
- mqttAndroidClient.setCallback(callback);
- }
- private void connect(){
- MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
- mqttConnectOptions.setAutomaticReconnect(true);
- mqttConnectOptions.setCleanSession(false);
- mqttConnectOptions.setUserName(username);
- mqttConnectOptions.setPassword(password.toCharArray());
- try {
- mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
- disconnectedBufferOptions.setBufferEnabled(true);
- disconnectedBufferOptions.setBufferSize(100);
- disconnectedBufferOptions.setPersistBuffer(false);
- disconnectedBufferOptions.setDeleteOldestMessages(false);
- mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
- subscribeToTopic();
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- Log.w("MQTT", "Failed to connect to: " + serverUri + exception.toString());
- }
- });
- } catch (MqttException ex){
- ex.printStackTrace();
- }
- }
- private void subscribeToTopic() {
- try {
- mqttAndroidClient.subscribe(subscriptionTopic, 2, null, new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- Log.w("MQTT","Subscribed!");
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- Log.w("MQTT", "Subscribed fail!");
- }
- });
- } catch (MqttException ex) {
- System.err.println("Exceptionst subscribing");
- ex.printStackTrace();
- }
- }
- }
- MqttHelper mqttHelper;
- TextView dataReceived;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- dataReceived = (TextView) findViewById(R.id.dataReceived);
- startMqtt();
- }
- private void startMqtt() {
- mqttHelper = new MqttHelper(getApplicationContext());
- mqttHelper.setCallback(new MqttCallbackExtended() {
- @Override
- public void connectComplete(boolean b, String s) {
- }
- @Override
- public void connectionLost(Throwable throwable) {
- }
- @Override
- public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
- Log.w("Debug", mqttMessage.toString());
- dataReceived.setText(mqttMessage.toString());
- }
- @Override
- public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement