1.build.gradle
allprojects {
repositories {
google()
jcenter()
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
}
2.pubsub client
public class Mqtt1Activity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
private EditText mTopicPub;
private EditText mTopicSub;
private EditText mMessageSub;
private EditText mMessagePub;
private EditText mQoSPub;
private EditText mQoSub;
private Button mPublish;
private Button mSubscribe;
private Switch mRetain;
private String TAG = "hanchd";
public static final String HOST = "tcp://test.mosquitto.org:1883";
private static final String clientid = "hancclientid";
private MqttClient mMqttClient;
private boolean isRetain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mqtt1);
mTopicPub=findViewById(R.id.et_topic_pub);
mTopicSub=findViewById(R.id.et_topic_sub);
mMessageSub=findViewById(R.id.et_message_sub);
mMessagePub=findViewById(R.id.et_message_pub);
mQoSPub=findViewById(R.id.et_QoS_pub);
mQoSub=findViewById(R.id.et_QoS_sub);
mPublish=findViewById(R.id.btn_pub);
mSubscribe=findViewById(R.id.btn_sub);
mRetain=findViewById(R.id.switch_retain);
mPublish.setOnClickListener(this);
mSubscribe.setOnClickListener(this);
mRetain.setOnCheckedChangeListener(this);
try {
mMqttClient = new MqttClient(HOST, clientid, new MemoryPersistence());
} catch (MqttException e) {
e.printStackTrace();
}
connect();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
isRetain = isChecked;
} else {
isRetain = isChecked;
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_pub:
publish();
break;
case R.id.btn_sub:
subscribe();
break;
default:
break;
}
}
private void publish() {
MqttMessage mqttMessage= new MqttMessage();
mqttMessage.setQos( Integer.parseInt(mQoSPub.getText().toString()));//
//messagePub.setQos(1);//
mqttMessage.setRetained(isRetain);//
mqttMessage.setPayload(mMessagePub.getText().toString().getBytes());
try {
mMqttClient.publish(mTopicPub.getText().toString(),mqttMessage);
} catch (MqttException e) {
e.printStackTrace();
}
}
private void subscribe() {
try {
mMqttClient.subscribe(mTopicSub.getText().toString(),Integer.parseInt(mQoSub.getText().toString()));
} catch (MqttException e) {
e.printStackTrace();
}
}
private void connect() {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setConnectionTimeout(10);
// 设置会话心跳时间
options.setKeepAliveInterval(20);
try {
mMqttClient.setCallback(new ConnectCallback());
mMqttClient.connect(options);
} catch (Exception e) {
e.printStackTrace();
}
}
public class ConnectCallback implements MqttCallback {
public void connectionLost(Throwable cause) {
// 连接丢失后,一般在这里面进行重连
Log.w(TAG, "连接断开,可以做重连");
}
public void deliveryComplete(IMqttDeliveryToken token) {
Log.w(TAG, "deliveryComplete---------" + token.isComplete());
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
if(topic.equals(mTopicSub.getText().toString()))
{
mMessageSub.setText(topic+": "+new String(message.getPayload()));
}
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:id="@+id/subscription_dialog">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TopicPub:" />
<EditText
android:id="@+id/et_topic_pub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:inputType="text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MessagePub:" />
<EditText
android:id="@+id/et_message_pub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:inputType="text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QoS:" />
<EditText
android:id="@+id/et_QoS_pub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:inputType="number"/>
<Switch
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Retain"
android:id="@+id/switch_retain" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_pub"
android:layout_gravity="center"
android:text="publish"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TopicSub:" />
<EditText
android:id="@+id/et_topic_sub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:inputType="text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QoS:" />
<EditText
android:id="@+id/et_QoS_sub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:inputType="number"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_sub"
android:layout_gravity="center"
android:text="subscribe"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MessageSub:" />
<EditText
android:id="@+id/et_message_sub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:inputType="text"/>
</LinearLayout>
|