准备
- AndroidStudio
- MQTT支持包:org.eclipse.paho.client.mqttv3-1.2.0.jar
实操
public class MainActivity extends AppCompatActivity {
private MqttClient client;
private String userName = "设备名&a1YSkpQ02ky";
private String passWord = "7051FFEAE740FFAD48E93699D70518E761689AF1";
private String mqtt_id = "12345678|securemode=3,signmethod=hmacsha1|";
private String mqtt_sub_topic = "/sys/a1YSkpQ02ky/设备名/thing/service/property/set";
private String mqtt_pub_topic = "/sys/a1YSkpQ02ky/设备名/thing/event/property/post";
private String host = "tcp://a1YSkpQ02ky.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883";
private MqttConnectOptions options;
private ScheduledExecutorService scheduler;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_pub= (Button) findViewById(R.id.button_pub);
button_pub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"发布成功" ,Toast.LENGTH_SHORT).show();
publishmessageplus(mqtt_pub_topic,"OPEN LED");
}
});
Mqtt_init();
startReconnect();
handler = new Handler() {
@SuppressLint