Olá Pessoal, boa tarde.
Estou desenvolvendo uma aplicação com Mqtt no android, mas estou tendo problemas agora.
Tinha feito a mesma aplicação alguns meses atrás, estava salva. Funcionava perfeitamente. Agora quando a executo, tenho problemas: um erro com Broadcast e acabo implementando:
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0’
Isso resolve problemas do método connect()
da classe MqttAndroidClient
, porém, quando tento utilizar o método subscribe()
, aparece o seguinte erro:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jgabriel.mqtt, PID: 11264
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jgabriel.mqtt/com.jgabriel.mqtt.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void org.eclipse.paho.android.service.MqttService.subscribe(java.lang.String, java.lang.String[], int[], java.lang.String, java.lang.String, org.eclipse.paho.client.mqttv3.IMqttMessageListener[])’ on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3488)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.di
Cade o código?
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t = findViewById(R.id.tv1);
try{
MqttAndroidClient client = new MqttAndroidClient(this, "tcp://test.mosquitto.org:1883", MqttClient.generateClientId());
client.connect().setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(MainActivity.this, "Success!", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(MainActivity.this, "Failure!", Toast.LENGTH_SHORT).show();
}
});
client.subscribe("E_32tmdl", 1, new IMqttMessageListener() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
}
});
Log.d("Status", "Subscribed!");
}catch (MqttException ex) {
ex.printStackTrace();
}
}
}
Esse aqui é o código, fiz esse aqui para testar. Vou mostrar também o manifest e o gradle
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<service android:name="org.eclipse.paho.android.service.MqttService"/>
No gradle:
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
Repositórios:
maven {
url “https://repo.eclipse.org/content/repositories/paho-snapshots/”
}
Debugue teu código. Aparentemente, o objeto client tá nulo