Skip to content

Commit

Permalink
补充samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheedon committed May 2, 2022
1 parent de44c7b commit 72799fa
Show file tree
Hide file tree
Showing 60 changed files with 1,626 additions and 164 deletions.
76 changes: 0 additions & 76 deletions app/src/main/java/org/sheedon/app/MainActivity.kt

This file was deleted.

61 changes: 0 additions & 61 deletions app/src/main/java/org/sheedon/app/factory/MqttClient.kt

This file was deleted.

19 changes: 0 additions & 19 deletions app/src/main/res/layout/activity_main.xml

This file was deleted.

3 changes: 0 additions & 3 deletions app/src/main/res/values/strings.xml

This file was deleted.

File renamed without changes.
24 changes: 24 additions & 0 deletions app/build.gradle → samples/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -29,6 +30,19 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main {
java {
java.srcDirs += 'src/main/kotlin'
}
}
}


buildFeatures {
dataBinding true
}

}

dependencies {
Expand All @@ -41,6 +55,16 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

def nav_version = "2.4.2"

// Java language implementation
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

implementation project(':mqtt')
// gson
implementation 'com.google.code.gson:gson:2.8.6'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.sheedon.app">
package="org.sheedon.app" >

<application
android:name=".App"
Expand All @@ -10,16 +10,25 @@
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MqttDispatcher">
android:theme="@style/Theme.MqttDispatcher" >
<activity
android:name=".MainActivity"
android:exported="true">
android:name="org.sheedon.sample.OkMqttActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true" >
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->

<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>
</application>

</manifest>
21 changes: 21 additions & 0 deletions samples/src/main/java/org/sheedon/sample/OkMqttActivity.java
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 samples/src/main/java/org/sheedon/sample/OkMqttContributors.java
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;
}

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

0 comments on commit 72799fa

Please sign in to comment.