如何使用MQTT Android在adafruit中发布数据?

时间:2019-06-10 12:23:57

标签: java android mqtt adafruit

我想使用MQTT从android应用更新adafruit中的切换开关, 我想发布以及订阅切换,但是在发布过程中通过mqtt协议连接后,它显示错误“ AlarmPingSender:向MqttServicepaho196825347016729注销Alarmreceiver” Android App正确连接到adafruit服务器,但是当我尝试发布或订阅其显示错误时

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();
                }
            }
        });
    }

0 个答案:

没有答案