Saturday, May 5, 2018

MqttConnectOptions members not recogized by Android Sudio 3.1.1

Leave a Comment

I am writing a straightforward mqtt client. For some strange reason Android studio 3.1.1 can't resolve any member function of MqttConnectOptions.

None of the members functions of MqttConnectOptions are recognized by the compiler.

All the examples I can find on the web use the MqttConnectOptions members. I'm baffled!

What simple thing am I missing?

package com.grayraven.garage;  import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log;  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;    public class MainActivity extends AppCompatActivity {      final String TAG = "Garage_Main";     private static Context mContext = GarageApp.getAppContext();      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         mContext = this.getApplicationContext();         setContentView(R.layout.activity_main);     }      final String broker = "10.211.1.127:1883";     final String password = "monkey123";     final String subscriptionTopic = "garage_door";       MqttConnectOptions options = new MqttConnectOptions();     options.setUserName("pi"); //cannot resolve setUserName!!!     options.setPassword(password.toCharArray()); //cannot resolve setPassword!!!!       String clientId = MqttClient.generateClientId();     MqttAndroidClient client = new MqttAndroidClient(mContext, broker,                     clientId);     /// rest of code works so omitted for clarity 

app build.gradle:

apply plugin: 'com.android.application'  android {     compileSdkVersion 26     defaultConfig {         applicationId "com.grayraven.garage"         minSdkVersion 22         targetSdkVersion 26         versionCode 1         versionName "1.0"         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     } }  repositories {     maven {         url "https://repo.eclipse.org/content/repositories/paho-releases/"     } }  dependencies {     implementation fileTree(dir: 'libs', include: ['*.jar'])     implementation 'com.android.support:appcompat-v7:26.1.0'     implementation 'com.android.support.constraint:constraint-layout:1.1.0'     testImplementation 'junit:junit:4.12'     androidTestImplementation 'com.android.support.test:runner:1.0.1'     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'     implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {         exclude module: 'support-v4'     } } 

2 Answers

Answers 1

Your code to import MQTT dependencies for Android are not correct. Please try this one.

Step 1: Remove these lines from your build.gradle file

repositories {     maven {         url "https://repo.eclipse.org/content/repositories/paho-releases/"     } }  implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {         exclude module: 'support-v4'     } 

Step 2: Add these lines to build.gradle file of your Android Studio project. This is top-level build.gradle file not the one in app forder where you declare all dependencies of your project.

repositories {     maven {         url "https://repo.eclipse.org/content/repositories/paho-snapshots/"     } } 

Step 3: Add these lines to build.gradle file of your Android app

dependencies {     compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'     compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' } 

Finally Clean, Sync then Build your project.

Answers 2

It was just a rookie mistake, I was trying to set the options outside the code section of the main activity.

Never mind.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment