使用百度平台的MQTT的Android的Demo
唔,不多说直接上代码
本demo源文件在:https://download.youkuaiyun.com/download/qq_42626565/11706227
public class MainActivity extends AppCompatActivity {
private String string_IMEI_ID = "client001";
private String host = "tcp://sgk0trr.mqtt.iot.bj.baidubce.com:1883";//代理ip
private TextView textView_text1,textView_text2,textView_text3;
private Button button_1,button_2,button_3,button_4;
private String userName = "sgk0trr/mqtt_iot";
private String passWord = "gddHAxOEKqujw56G";
private MqttClient Customer;//客户端
private MqttConnectOptions options;//配置 保存控制客户端连接到服务器的方式的选项集。\
MqttConnectThread mqttConnectThread = new MqttConnectThread();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Initialization();
click();
Initialization_MQTT();//初始化配置MQTT客户端
//mqttConnectThread.start();//执行连接服务器任务
}
private void Initialization(){
textView_text1 = findViewById(R.id.textView1);
textView_text2 = findViewById(R.id.textView2);
textView_text3 = findViewById(R.id.textView3);
button_1 = findViewById(R.id.button1);
button_2 = findViewById(R.id.button2);
button_3 = findViewById(R.id.button3);
button_4 = findViewById(R.id.button4);
}
private void click(){
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//连接
mqttConnectThread.start();
}
});
button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//关闭
try {
Customer.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
});
button_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//订阅主题
try {
Customer.subscribe("sw_led",0);//设置(订阅)接收的主题,主题的级别是0
} catch (MqttException e) {
e.printStackTrace();
}
}
});
button_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//发送主题
MqttMessage mqttMessage = null;
mqttMessage = new MqttMessage("gun!".getBytes());
try {
Customer.publish("sw_led",mqttMessage);
Toast.makeText(getApplicationContext(),"主题text发送了:"+mqttMessage, Toast.LENGTH_SHORT).show();
} catch (MqttException e) {
e.printStackTrace();
}
}
});
}
/* 初始化配置Mqtt */
private void Initialization_MQTT(){
try {
//(1)主机地址(2)客户端ID,一般以客户端唯一标识符(不能够和其它客户端重名)(3)最后一个参数是指数据保存在内存(具体保存什么数据,以后再说,其实现在我也不是很确定)
Customer = new MqttClient(host,string_IMEI_ID,new MemoryPersistence());
} catch (MqttException e) {
e.printStackTrace();
}
options = new MqttConnectOptions();//MQTT的连接设置
options.setCleanSession(true);//设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
options.setUserName(userName);//设置连接的用户名(自己的服务器没有设置用户名)
options.setPassword(passWord.toCharArray());//设置连接的密码(自己的服务器没有设置密码)
options.setConnectionTimeout(10);// 设置连接超时时间 单位为秒
options.setKeepAliveInterval(20);// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
Customer.setCallback(new MqttCallback() {
@Override//连接丢失后,会执行这里
public void connectionLost(Throwable throwable) {
}
@Override//获取的消息会执行这里--arg0是主题,arg1是消息
public void messageArrived(final String ssr, MqttMessage mqttMessage) throws Exception {
final String mqtt_zhuti = ssr;//主题
final String mqtt_message = mqttMessage.toString();//消息
runOnUiThread(new Runnable() {//
public void run() {
textView_text2.setText(mqtt_message);
textView_text1.setText(mqtt_zhuti);
Toast.makeText(getApplicationContext(),"主题:"+ssr+"消息:"+mqtt_message, Toast.LENGTH_SHORT).show();
}
});
}
@Override//订阅主题后会执行到这里
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
/*连接服务器任务*/
class MqttConnectThread extends Thread
{
public void run()
{
try
{
Customer.connect(options);//连接服务器,连接不上会阻塞在这
Log.e("是否在循环","1111111111111111");
runOnUiThread(new Runnable() {//
public void run() {
Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
Log.e("只走一遍","2222222222222222222");
}
});
}
catch (MqttSecurityException e)
{
//安全问题连接失败
Log.e("安全问题连接失败","e");
}
catch (MqttException e)
{
//连接失败原因
Log.e("连接失败原因",""+e);
}
}
}
xml的代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:text="TextView1" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:text="TextView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:text="TextView3" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开连接" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭连接" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="订阅消息" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发布消息" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>