问题
I want to update the toggle switch in adafruit from android app using MQTT, i want to publish as well as subscribe to toggles but after connection through mqtt protocols during publish it shows a error " AlarmPingSender: Unregister alarmreceiver to MqttServicepaho196825347016729 " Android App is properly connecting to adafruit server but when i am trying to publish or subscribing its showing error
public class MainActivity extends AppCompatActivity {
String TAG="MQTT";
TextView tvview;
Button btpublish;
Button btsubscribe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvview=(TextView)findViewById(R.id.tvview);
btpublish=(Button)findViewById(R.id.btpublish);
btsubscribe=(Button)findViewById(R.id.btsubscribe);
System.out.println("1111111");
String clientId = MqttClient.generateClientId();
final MqttAndroidClient client =
new MqttAndroidClient(this.getApplicationContext(), "tcp://io.adafruit.com:1883",
clientId);
System.out.println("22222");
try {
MqttConnectOptions options = new MqttConnectOptions();
System.out.println("33333");
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
options.setUserName("USERNAME");
options.setPassword("AIO KEY".toCharArray());
System.out.println("444444");
IMqttToken token = client.connect(options);
System.out.println("5555555");
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// We are connected
System.out.println("Connect");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
// Something went wrong e.g. connection timeout or firewall problems
System.out.println("C Failed");
}
});
} catch (MqttException e) {
e.printStackTrace();
}
btpublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String topic = "SalilPEkka∕feeds/check";
String payload = "00";
byte[] encodedPayload = new byte[0];
try {
if(client.isConnected())
System.out.println("Connected baba");
encodedPayload = payload.getBytes("UTF-8");
MqttMessage message = new MqttMessage(encodedPayload);
message.setRetained(false);
client.publish(topic,message);
} catch (MqttException e) {
System.out.println(e);
} catch (UnsupportedEncodingException e) {
System.out.println(e);
}
}
});
btsubscribe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String topic = "SalilPEkka∕feeds/check";
int qos = 1;
try {
IMqttToken subToken = client.subscribe(topic, qos);
subToken.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// The message was published
Toast.makeText(MainActivity.this, (asyncActionToken.getMessageId()), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(IMqttToken asyncActionToken,
Throwable exception) {
// The subscription could not be performed, maybe the user was not
// authorized to subscribe on the specified topic e.g. using wildcards
Toast.makeText(MainActivity.this, "Sorry", Toast.LENGTH_SHORT).show();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
});
}
来源:https://stackoverflow.com/questions/56526367/how-to-publish-data-in-adafruit-using-mqtt-android