mqttjs / mqtt-elements

Polymer elements for MQTT
http://mqttjs.github.io/mqtt-elements/
MIT License
27 stars 6 forks source link

IMqttToken token = client.connect(options); error #18

Closed MRTUNC closed 7 months ago

MRTUNC commented 4 years ago

Hello Sir and friends ! I have seen your sharing at https://www.hivemq.com/blog/mqtt-client-library-enyclopedia-paho-android-service/ and then ı want to try your example of source code , But ı have got a trouble as below; "IMqttToken token = client.connect(options);" this code block gives error also connection() event. would you please help me that whats wrong ? I have tried too much stuff ...

package com.example.button2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;

import org.eclipse.paho.android.service.MqttAndroidClient; import org.eclipse.paho.client.mqttv3.IMqttActionListener; import org.eclipse.paho.client.mqttv3.IMqttToken; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.io.UnsupportedEncodingException;

public class MainActivity extends AppCompatActivity { TextView txt1,txt2;

Button btn1;
EditText girdi;
Integer sayac=0;
String d;
static  String MQTTHOST="tcp://farmer.cloudmqtt.com:15916";
static  String  USERNAME="yesman";
static  String PASSWORD="deneme";
String topicStr="tnc17";
MqttAndroidClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt1=(TextView)findViewById(R.id.txtview);
    btn1=(Button)findViewById(R.id.button);
    txt2=(TextView)findViewById(R.id.textView3);
    girdi=(EditText)findViewById(R.id.editTextTextPersonName);
    // client tanımlama
    String clientId = MqttClient.generateClientId();
    //mqttAndroidClient = new MqttAndroidClient(context, brokerUrl, clientId);
    client=new MqttAndroidClient(this.getApplicationContext(),MQTTHOST,clientId);
    MqttConnectOptions options=new MqttConnectOptions();
   // options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
    options.setUserName(USERNAME);
    options.setPassword(PASSWORD.toCharArray());
    options.setCleanSession(true);
    options.setConnectionTimeout(10);

    IMqttToken token = client.connect(options);

    token.setActionCallback(new IMqttActionListener() {
        @Override
        public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
               // Log.d(TAG, "onSuccess");
                Toast.makeText(getBaseContext(),"basarili",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
               // Log.d(TAG, "onFailure");
                Toast.makeText(getBaseContext(),"failg",Toast.LENGTH_LONG).show();

            }
        });
    /*} catch (MqttException e) {
        e.printStackTrace();
    } */
    // client

}
@SuppressLint("SetTextI18n")
public  void tikla1(View V){
    ++sayac;
    txt1.setText("aadd:"+sayac);
    d=girdi.getText().toString();
    txt2.setText(d);
    //mqtt
    String topic=topicStr;
    String message="hello tnc";
    try{
        client.publish(topic,message.getBytes(),0,false);

    }
    catch (Exception e)
    {
        e.printStackTrace();

    }

    //mqtt

}

}