-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
60 changed files
with
1,626 additions
and
164 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
samples/src/main/java/org/sheedon/sample/OkMqttActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.sheedon.sample; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.databinding.DataBindingUtil; | ||
|
||
import android.os.Bundle; | ||
import android.view.View; | ||
|
||
import org.sheedon.app.R; | ||
|
||
public class OkMqttActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_ok_mqtt); | ||
|
||
// 连接mqtt | ||
OkMqttContributors.getInstance().loadOkMqttClient(this); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
samples/src/main/java/org/sheedon/sample/OkMqttContributors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.sheedon.sample; | ||
|
||
import android.content.Context; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import org.sheedon.app.factory.CallbackNameConverter; | ||
import org.sheedon.mqtt.OkMqttClient; | ||
import org.sheedon.mqtt.Topics; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
/** | ||
* OkMqttClient java code Contributor | ||
* | ||
* @Author: sheedon | ||
* @Email: [email protected] | ||
* @Date: 2022/4/25 22:35 | ||
*/ | ||
public class OkMqttContributors { | ||
|
||
// current client id | ||
private static final String clientId = UUID.randomUUID().toString(); | ||
// It is recommended that developers change to their own mqtt-Broker | ||
// private static final String serverUri = "tcp://test.mosquitto.org:1883"; | ||
private static final String serverUri = "tcp://broker-cn.emqx.io:1883"; | ||
// Topic to subscribe to by default. | ||
private static final List<Topics> topicsBodies = new ArrayList<Topics>() { | ||
{ | ||
// Contents in order: topic, message quality, attachRecord | ||
// attachRecord: whether additional records are required, | ||
// if necessary, the subscription will be automatically added after reconnection | ||
add(Topics.build("mq/clouds/cmd/test", 0, true)); | ||
} | ||
}; | ||
|
||
private static final OkMqttContributors instance = new OkMqttContributors(); | ||
|
||
private volatile OkMqttClient okMqttClient; | ||
|
||
public static OkMqttContributors getInstance() { | ||
return instance; | ||
} | ||
|
||
private OkMqttContributors() { | ||
|
||
} | ||
|
||
/** | ||
* 创建OkMqttClient | ||
*/ | ||
public OkMqttClient loadOkMqttClient(Context context) { | ||
if (okMqttClient != null) { | ||
return okMqttClient; | ||
} | ||
|
||
Context clientContext = context.getApplicationContext(); | ||
okMqttClient = new OkMqttClient.Builder() | ||
// Configure the basic parameters of mqtt connection | ||
.clientInfo(clientContext, serverUri, clientId) | ||
.subscribeBodies(topicsBodies.toArray(new Topics[0])) | ||
// 作用于关键字关联的响应信息解析器 | ||
.keywordConverter(new CallbackNameConverter(new Gson())) | ||
.build(); | ||
|
||
return okMqttClient; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
samples/src/main/java/org/sheedon/sample/fragment/OkMqttFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.sheedon.sample.fragment; | ||
|
||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.databinding.DataBindingUtil; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.navigation.Navigation; | ||
import androidx.navigation.fragment.NavHostFragment; | ||
|
||
import org.sheedon.app.R; | ||
import org.sheedon.app.databinding.FragmentOkMqttBinding; | ||
|
||
/** | ||
* okMqtt使用 | ||
* | ||
* @Author: sheedon | ||
* @Email: [email protected] | ||
* @Date: 2022/4/26 22:12 | ||
*/ | ||
public class OkMqttFragment extends Fragment { | ||
|
||
@Nullable | ||
@Override | ||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
FragmentOkMqttBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_ok_mqtt, container, false); | ||
|
||
// 到发送mqtt消息页面 | ||
binding.btnPublishMessage.setOnClickListener(v -> NavHostFragment.findNavController(OkMqttFragment.this).navigate(R.id.action_to_publish_message)); | ||
// 到订阅消息页面 | ||
binding.btnSubscribeTopic.setOnClickListener(v -> NavHostFragment.findNavController(OkMqttFragment.this).navigate(R.id.action_to_subscribe_topic)); | ||
// 到请求响应页面 | ||
binding.btnRr.setOnClickListener(v -> NavHostFragment.findNavController(OkMqttFragment.this).navigate(R.id.action_to_request_and_response)); | ||
// 到订阅一个组页面 | ||
binding.btnSubscribeArray.setOnClickListener(v -> NavHostFragment.findNavController(OkMqttFragment.this).navigate(R.id.action_to_subscribe_array)); | ||
|
||
return binding.getRoot(); | ||
} | ||
} |
Oops, something went wrong.