java - Having trouble connecting android with MQTT to broker -
i trying connect apollo broker, code works when use alone in normal java project, the same except in android project , try run when click button mainactivity.
i have text box gets updated "1" before try connect mqttclient second .sett("2") not run think problem client.connect(opts) if client.connect() text box gets updated "2" since need username , password opts rest not run
just started using mqtt learning go along. help.
package com.example.androidmqtt; import org.eclipse.paho.client.mqttv3.mqttclient; import org.eclipse.paho.client.mqttv3.mqttconnectoptions; import org.eclipse.paho.client.mqttv3.mqttdeliverytoken; import org.eclipse.paho.client.mqttv3.mqttexception; import org.eclipse.paho.client.mqttv3.mqttmessage; import org.eclipse.paho.client.mqttv3.mqtttopic; import org.eclipse.paho.client.mqttv3.internal.memorypersistence; public class service { mqttclient client; memorypersistence persistence = new memorypersistence(); public service()throws exception{} public static void main(string[] args) throws exception { new service().dodemo(); } public void dodemo() { try { client = new mqttclient("tcp://10.1.10.1:1883", "testingmymqtt", persistence); mainactivity.sett("2"); mqttconnectoptions opts = new mqttconnectoptions(); opts.setusername("nabi"); opts.setpassword("m4rk3".tochararray()); opts.setkeepaliveinterval(480); mainactivity.sett("1");//sets txt1 in main view 1 know whats going on client.connect(opts); mainactivity.sett("2"); mqttmessage msg = new mqttmessage("works".getbytes()); msg.setretained(true); msg.setqos(1); mainactivity.sett("its working"); mqtttopic topic = client.gettopic("android/test"); mqttdeliverytoken token = topic.publish(msg); } catch (mqttexception e) { e.printstacktrace(); } }
}
verify android can connect local network 10.1.10.1
, if so, check logs of apollo broker.
Comments
Post a Comment