├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── jflavio1 │ │ └── androidmqttexample │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── jflavio1 │ │ │ └── androidmqttexample │ │ │ ├── model │ │ │ └── CustomLightSensor.kt │ │ │ ├── mqtt │ │ │ ├── BaseMqttModel.kt │ │ │ ├── CustomMqttCallback.kt │ │ │ ├── CustomMqttClient.kt │ │ │ ├── CustomMqttLog.kt │ │ │ └── SensorsMqttService.kt │ │ │ ├── presenters │ │ │ ├── SensorsListPresenter.kt │ │ │ └── SensorsListPresenterImpl.kt │ │ │ ├── repository │ │ │ ├── SensorMapper.kt │ │ │ └── SensorsRepository.kt │ │ │ ├── viewmodel │ │ │ └── LightSensorViewModel.kt │ │ │ └── views │ │ │ ├── MainActivity.kt │ │ │ ├── SensorsAdapter.kt │ │ │ └── SensorsListView.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ ├── ic_light.xml │ │ └── ic_light_off.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── item_sensor.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── jflavio1 │ └── androidmqttexample │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/ 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MqttAndroidExample 2 | An example Android app using MQTT protocol 3 | 4 | #### What is MQTT? 5 | MQTT is a publish-subscribe-based messaging protocol, this means that clients must subscribe to a specific topic where messages are sent. The MQTT broker (or server) is in charge of managing of sending message to a specific (or specifics) topics and all clients subscribed to it will be receiving the data. 6 | ![MQTT architecture. Image from HiveMQ](https://miro.medium.com/max/1400/1*niJrX0DR9yzxUSb9TgQxgA.png) 7 | 8 | #### In Android? 9 | The implementation of this protocol was developed thanks to Eclipse Paho project. You can check an article I wrote for The Android Pub about this project here: [About the MQTT protocol for IoT on Android](https://android.jlelse.eu/about-the-mqtt-protocol-for-iot-on-android-efb4973577b) 10 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 27 9 | defaultConfig { 10 | applicationId "com.jflavio1.androidmqttexample" 11 | minSdkVersion 19 12 | targetSdkVersion 27 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 28 | implementation 'com.android.support:appcompat-v7:27.1.1' 29 | implementation 'com.android.support.constraint:constraint-layout:1.1.0' 30 | testImplementation 'junit:junit:4.12' 31 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 32 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 33 | 34 | implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0' 35 | implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' 36 | 37 | def lifecycle_version = "1.1.1" 38 | // ViewModel and LiveData 39 | implementation "android.arch.lifecycle:extensions:$lifecycle_version" 40 | 41 | implementation 'com.google.dagger:dagger-android:2.16' 42 | implementation 'com.google.dagger:dagger-android-support:2.16' // if you use the support libraries 43 | annotationProcessor 'com.google.dagger:dagger-android-processor:2.16' 44 | 45 | implementation libs.rxKotlin 46 | implementation libs.rxAndroid 47 | 48 | implementation 'com.android.support:recyclerview-v7:27.1.1' 49 | } 50 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/jflavio1/androidmqttexample/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.jflavio1.androidmqttexample", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/model/CustomLightSensor.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.model 2 | 3 | /** 4 | * CustomLightSensor 5 | * 6 | * @author Jose Flavio - jflavio90@gmail.com 7 | * @since 6/5/17 8 | */ 9 | class CustomLightSensor(var id: String, var name: String, var lightOn: Boolean) { 10 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/mqtt/BaseMqttModel.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.mqtt 2 | 3 | import org.eclipse.paho.client.mqttv3.IMqttActionListener 4 | import org.eclipse.paho.client.mqttv3.IMqttMessageListener 5 | import org.jetbrains.annotations.Nullable 6 | 7 | /** 8 | * BaseMqttModel 9 | * 10 | * @author Jose Flavio - jflavio90@gmail.com 11 | * @since 6/5/17 12 | */ 13 | interface BaseMqttModel { 14 | 15 | fun connectToServer() 16 | 17 | fun disconnectFromServer() 18 | 19 | fun subscribeToTopic(topicName: String, qos: Int, subscriptionListener: IMqttActionListener?, messageListener: IMqttMessageListener?) 20 | 21 | fun unsubscribeFromTopic(topicName: String) 22 | 23 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/mqtt/CustomMqttCallback.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.mqtt 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.support.v4.content.LocalBroadcastManager 6 | import android.util.Log 7 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken 8 | import org.eclipse.paho.client.mqttv3.MqttCallbackExtended 9 | import org.eclipse.paho.client.mqttv3.MqttMessage 10 | 11 | /** 12 | * CustomMqttCallback 13 | * 14 | * @author Jose Flavio - jflavio90@gmail.com 15 | * @since 10/5/17 16 | */ 17 | class CustomMqttCallback(val context: Context) : MqttCallbackExtended { 18 | 19 | override fun connectComplete(reconnect: Boolean, serverURI: String?) { 20 | } 21 | 22 | override fun messageArrived(topic: String?, message: MqttMessage?) { 23 | log("Arrived message on topic $topic") 24 | } 25 | 26 | override fun connectionLost(cause: Throwable?) { 27 | log("ConnectionLost ${cause.toString()}") 28 | LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(SensorsMqttService.CONNECTION_LOST)) 29 | } 30 | 31 | override fun deliveryComplete(token: IMqttDeliveryToken?) { 32 | } 33 | 34 | private fun log(text: String) { 35 | Log.d("MQTT", text) 36 | } 37 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/mqtt/CustomMqttClient.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.mqtt 2 | 3 | import android.content.Context 4 | import android.util.Log 5 | import org.eclipse.paho.android.service.MqttAndroidClient 6 | import org.eclipse.paho.client.mqttv3.IMqttActionListener 7 | import org.eclipse.paho.client.mqttv3.IMqttToken 8 | import org.eclipse.paho.client.mqttv3.MqttCallback 9 | 10 | /** 11 | * CustomMqttClient 12 | * 13 | * @author Jose Flavio - jflavio90@gmail.com 14 | * @since 6/5/17 15 | */ 16 | class CustomMqttClient(context: Context?, serverURI: String?, clientId: String?) : MqttAndroidClient(context, serverURI, clientId) { 17 | 18 | lateinit var mqttCallback: MqttCallback 19 | 20 | // Fixme: we should connect using mqttConnectOptions 21 | override fun connect(userContext: Any?, callback: IMqttActionListener?): IMqttToken { 22 | log("Connecting to Mqtt broker...") 23 | return super.connect(userContext, callback) 24 | } 25 | 26 | override fun subscribe(topic: String?, qos: Int, userContext: Any?, callback: IMqttActionListener?): IMqttToken { 27 | log("Subscribing to topic $topic") 28 | return super.subscribe(topic, qos, userContext, callback) 29 | } 30 | 31 | override fun setCallback(callback: MqttCallback?) { 32 | super.setCallback(callback) 33 | this.mqttCallback = callback!! 34 | } 35 | 36 | private fun log(text: String) { 37 | Log.d("MQTT", "Client: $text") 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/mqtt/CustomMqttLog.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.mqtt 2 | 3 | import android.util.Log 4 | import org.eclipse.paho.android.service.MqttTraceHandler 5 | import java.lang.Exception 6 | 7 | /** 8 | * CustomMqttLog 9 | * 10 | * @author Jose Flavio - jflavio90@gmail.com 11 | * @since 16/5/17 12 | */ 13 | class CustomMqttLog : MqttTraceHandler{ 14 | private val TAG = "MQTT" 15 | override fun traceDebug(tag: String?, message: String?) { 16 | Log.d(TAG, "$tag - $message") 17 | } 18 | 19 | override fun traceException(tag: String?, message: String?, e: Exception?) { 20 | Log.d(TAG, "$tag - $message \n${e.toString()}") 21 | } 22 | 23 | override fun traceError(tag: String?, message: String?) { 24 | Log.d(TAG, "$tag - $message") 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/mqtt/SensorsMqttService.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.mqtt 2 | 3 | import android.app.Notification 4 | import android.app.Service 5 | import android.content.Intent 6 | import android.os.Binder 7 | import android.os.Build 8 | import android.support.v4.content.LocalBroadcastManager 9 | import android.util.Log 10 | import org.eclipse.paho.client.mqttv3.* 11 | import org.json.JSONObject 12 | 13 | 14 | /** 15 | * SensorsMqttService 16 | * 17 | * @author Jose Flavio - jflavio90@gmail.com 18 | * @since 6/5/17 19 | */ 20 | class SensorsMqttService : Service(), BaseMqttModel { 21 | 22 | lateinit var mqttClient: CustomMqttClient 23 | lateinit var mqttCliendId: String 24 | 25 | companion object { 26 | val MQTT_CONNECT = "mqtt_connect" 27 | val MQTT_DISCONNECT = "mqtt_disconnect" 28 | 29 | val MQTT_SERVER_URL = "tcp://broker.mqttdashboard.com:1883" 30 | 31 | // connection state filters 32 | val CONNECTION_SUCCESS = "CONNECTION_SUCCESS" 33 | val CONNECTION_FAILURE = "CONNECTION_FAILURE" 34 | val CONNECTION_LOST = "CONNECTION_LOST" 35 | val DISCONNECT_SUCCESS = "DISCONNECT_SUCCESS" 36 | 37 | val MQTT_MESSAGE_TYPE = "type" 38 | val MQTT_MESSAGE_PAYLOAD = "payload" 39 | 40 | val TOPICS = arrayOf("home_sensors_info", "home_lights") 41 | } 42 | 43 | override fun onCreate() { 44 | super.onCreate() 45 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 46 | // TODO create notification for showing on Android Oreo: "You are listening to temperature changes on real time" 47 | startForeground(0, Notification()) 48 | } 49 | logMqtt("Created mqtt service...") 50 | } 51 | 52 | override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 53 | 54 | // TODO getting Build.SERIAL will no work on Android P 55 | this.mqttCliendId = Build.SERIAL 56 | 57 | if(intent == null) { 58 | logErrorMqtt("Null intent in onStartCommand, returning START_NOT_STICKY") 59 | stopSelf() 60 | return Service.START_NOT_STICKY 61 | } 62 | 63 | if (MQTT_CONNECT == intent.action!!) { 64 | connectToServer() 65 | } else if (MQTT_DISCONNECT == intent.action) { 66 | disconnectFromServer() 67 | } 68 | 69 | // this will return START_STICKY and will recreate the service (with saved start intent) when Android destroys it 70 | return super.onStartCommand(intent, flags, startId) 71 | } 72 | 73 | /** 74 | * Connects to the Mqtt Server. 75 | */ 76 | override fun connectToServer() { 77 | mqttClient = CustomMqttClient(this, MQTT_SERVER_URL, this.mqttCliendId) 78 | mqttClient.setCallback(CustomMqttCallback(this)) 79 | 80 | // enable logs from library 81 | mqttClient.setTraceEnabled(true) 82 | mqttClient.setTraceCallback(CustomMqttLog()) 83 | 84 | val options = MqttConnectOptions() 85 | options.apply { 86 | connectionTimeout = 30 87 | isAutomaticReconnect = true 88 | isCleanSession = true 89 | keepAliveInterval = 120 90 | } 91 | 92 | mqttClient.connect(options,this, object : IMqttActionListener { 93 | override fun onSuccess(asyncActionToken: IMqttToken?) { 94 | logMqtt("Success connecting to server...") 95 | LocalBroadcastManager.getInstance(this@SensorsMqttService).sendBroadcast(Intent(CONNECTION_SUCCESS)) 96 | } 97 | 98 | override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) { 99 | logErrorMqtt("Error on connecting to server... retry?: ${exception!!.toString()}") 100 | LocalBroadcastManager.getInstance(this@SensorsMqttService).sendBroadcast(Intent(CONNECTION_FAILURE)) 101 | disconnectFromServer() 102 | } 103 | }) 104 | } 105 | 106 | /** 107 | * Disconnect from the Mqtt Server. 108 | */ 109 | override fun disconnectFromServer() { 110 | this.mqttClient.disconnect(this, object : IMqttActionListener { 111 | override fun onSuccess(asyncActionToken: IMqttToken?) { 112 | logMqtt("Disconnected from server attempt success...") 113 | mqttClient.unregisterResources() 114 | SensorsMqttService@ mqttClient.close() 115 | LocalBroadcastManager.getInstance(this@SensorsMqttService).sendBroadcast(Intent(DISCONNECT_SUCCESS)) 116 | stopSelf() 117 | } 118 | 119 | override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) { 120 | logMqtt("Failure on triying to disconnect: ${exception.toString()}") 121 | } 122 | 123 | }) 124 | } 125 | 126 | override fun subscribeToTopic(topicName: String, qos: Int, subscriptionListener: IMqttActionListener?, messageListener: IMqttMessageListener?) { 127 | logMqtt("Subscribing to topic $topicName") 128 | this.mqttClient.subscribe(topicName, qos, this, subscriptionListener, messageListener) 129 | } 130 | 131 | override fun unsubscribeFromTopic(topicName: String) { 132 | this.mqttClient.unsubscribe(topicName) 133 | } 134 | 135 | /** 136 | * Publish a [MqttMessage] into the specified [topicName]. 137 | */ 138 | fun publish(topicName: String, message: MqttMessage){ 139 | this.mqttClient.publish(topicName, message, this, object: IMqttActionListener{ 140 | override fun onSuccess(asyncActionToken: IMqttToken?) { 141 | logMqtt("Message was sent to topic $topicName") 142 | } 143 | 144 | override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) { 145 | Log.d("MqttRepository", "Fail on sending message to topic $topicName") 146 | } 147 | 148 | }) 149 | } 150 | 151 | private fun logMqtt(text: String) { 152 | Log.d("MQTT", text) 153 | } 154 | 155 | private fun logErrorMqtt(text: String) { 156 | Log.w("MQTT", text) 157 | } 158 | 159 | override fun onDestroy() { 160 | super.onDestroy() 161 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 162 | stopForeground(true) 163 | } 164 | } 165 | 166 | override fun onBind(intent: Intent?) = mBinder 167 | 168 | private val mBinder = LocalBinder() 169 | 170 | inner class LocalBinder : Binder() { 171 | val service: SensorsMqttService 172 | get() = this@SensorsMqttService 173 | } 174 | 175 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/presenters/SensorsListPresenter.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.presenters 2 | 3 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 4 | 5 | /** 6 | * SensorsListPresenter 7 | * 8 | * @author Jose Flavio - jflavio90@gmail.com 9 | * @since 6/5/17 10 | */ 11 | interface SensorsListPresenter { 12 | 13 | fun initMqttService() 14 | 15 | fun stopMqttService() 16 | 17 | fun getTemperatures() 18 | 19 | fun changeLightState(sensor: CustomLightSensor, turnedOn: Boolean) 20 | 21 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/presenters/SensorsListPresenterImpl.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.presenters 2 | 3 | import android.arch.lifecycle.Observer 4 | import android.arch.lifecycle.ViewModelProviders 5 | import android.content.* 6 | import android.os.Build 7 | import android.os.IBinder 8 | import android.support.v4.app.FragmentActivity 9 | import android.support.v4.content.LocalBroadcastManager 10 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 11 | import com.jflavio1.androidmqttexample.mqtt.SensorsMqttService 12 | import com.jflavio1.androidmqttexample.repository.SensorsRepository 13 | import com.jflavio1.androidmqttexample.viewmodel.LightSensorViewModel 14 | import com.jflavio1.androidmqttexample.views.SensorsListView 15 | 16 | 17 | /** 18 | * SensorsListPresenterImpl 19 | * 20 | * @author Jose Flavio - jflavio90@gmail.com 21 | * @since 6/5/17 22 | */ 23 | class SensorsListPresenterImpl(val view: SensorsListView) : SensorsListPresenter { 24 | 25 | private lateinit var repository : SensorsRepository 26 | private var mqttService: SensorsMqttService? = null 27 | private var mqttBroadcast: MqttBroadcast 28 | 29 | val serviceConnection = object: ServiceConnection { 30 | override fun onServiceConnected(name: ComponentName?, service: IBinder?) { 31 | mqttService = (service as SensorsMqttService.LocalBinder).service 32 | repository = SensorsRepository(mqttService!!) 33 | } 34 | 35 | override fun onServiceDisconnected(name: ComponentName?) { 36 | mqttService = null 37 | } 38 | } 39 | 40 | init { 41 | mqttBroadcast = MqttBroadcast() 42 | this.view.setSensorPresenter(this) 43 | } 44 | 45 | override fun initMqttService() { 46 | 47 | LocalBroadcastManager.getInstance(this.view.getViewContext()).registerReceiver(mqttBroadcast, IntentFilter(SensorsMqttService.CONNECTION_SUCCESS)) 48 | 49 | val startServiceIntent = Intent(this.view.getViewContext(), SensorsMqttService::class.java) 50 | this.view.getViewContext().bindService(startServiceIntent, serviceConnection, 0) 51 | 52 | startServiceIntent.action = SensorsMqttService.MQTT_CONNECT 53 | 54 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 55 | this.view.getViewContext().startForegroundService(startServiceIntent) 56 | } else { 57 | this.view.getViewContext().startService(startServiceIntent) 58 | } 59 | 60 | } 61 | 62 | override fun stopMqttService() { 63 | this.view.getViewContext().unbindService(serviceConnection) 64 | LocalBroadcastManager.getInstance(this.view.getViewContext()).unregisterReceiver(mqttBroadcast) 65 | val startServiceIntent = Intent(this.view.getViewContext(), SensorsMqttService::class.java) 66 | startServiceIntent.action = SensorsMqttService.MQTT_DISCONNECT 67 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 68 | this.view.getViewContext().startForegroundService(startServiceIntent) 69 | } else { 70 | this.view.getViewContext().startService(startServiceIntent) 71 | } 72 | } 73 | 74 | override fun getTemperatures() { 75 | val vm = ViewModelProviders.of(this.view.getViewContext() as FragmentActivity).get(LightSensorViewModel::class.java) 76 | vm.getSensors().observe(this.view.getViewContext() as FragmentActivity, Observer> { 77 | this.view.setSensorsTemperature(it!!) 78 | }) 79 | this.repository.getAllSensors(vm) 80 | } 81 | 82 | override fun changeLightState(sensor:CustomLightSensor, turnedOn: Boolean) { 83 | this.repository.updateSensorState(sensor, turnedOn) 84 | } 85 | 86 | inner class MqttBroadcast: BroadcastReceiver() { 87 | 88 | override fun onReceive(context: Context?, intent: Intent?) { 89 | 90 | if(SensorsMqttService.CONNECTION_SUCCESS == intent!!.action){ 91 | view.onMqttConnected() 92 | } 93 | 94 | if(SensorsMqttService.CONNECTION_FAILURE == intent.action){ 95 | view.onMqttError("error on connecting") 96 | } 97 | 98 | if(SensorsMqttService.CONNECTION_LOST == intent.action){ 99 | view.onMqttError("connection lost") 100 | view.onMqttDisconnected() 101 | } 102 | 103 | if(SensorsMqttService.DISCONNECT_SUCCESS == intent.action){ 104 | view.onMqttStopped() 105 | } 106 | } 107 | 108 | } 109 | 110 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/repository/SensorMapper.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.repository 2 | 3 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 4 | import org.json.JSONArray 5 | import org.json.JSONObject 6 | 7 | /** 8 | * SensorMapper 9 | * 10 | * @author Jose Flavio - jflavio90@gmail.com 11 | * @since 10/5/17 12 | */ 13 | class SensorMapper() { 14 | 15 | fun mapJsonArray(array: JSONArray): ArrayList{ 16 | val list = arrayListOf() 17 | for(i in 0..(array.length() -1)){ 18 | list.add(mapJson(array.getJSONObject(i))) 19 | } 20 | return list 21 | } 22 | 23 | fun mapJson(obj: JSONObject): CustomLightSensor{ 24 | return CustomLightSensor(obj.getString("id"), obj.getString("name"), obj.getBoolean("turnedOn")) 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/repository/SensorsRepository.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.repository 2 | 3 | import android.util.Log 4 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 5 | import com.jflavio1.androidmqttexample.mqtt.SensorsMqttService 6 | import com.jflavio1.androidmqttexample.viewmodel.LightSensorViewModel 7 | import org.eclipse.paho.client.mqttv3.IMqttActionListener 8 | import org.eclipse.paho.client.mqttv3.IMqttMessageListener 9 | import org.eclipse.paho.client.mqttv3.IMqttToken 10 | import org.eclipse.paho.client.mqttv3.MqttMessage 11 | import org.json.JSONObject 12 | 13 | /** 14 | * SensorsRepository 15 | * 16 | * @author Jose Flavio - jflavio90@gmail.com 17 | * @since 10/5/17 18 | */ 19 | class SensorsRepository(val service: SensorsMqttService) { 20 | 21 | companion object { 22 | val GET_SENSORS = "GET_SENSORS" 23 | val UPDATE_LIGHT_STATE = "UPDATE_LIGHT_STATE" 24 | } 25 | 26 | /** 27 | * GetAllSensors is a method that will subscribe to a specific topic 28 | * "home_lights" on [SensorsMqttService.TOPICS] and when a message is received 29 | * on that topic it will classify the type of message getting the "type" 30 | * string from the MqttMessage. 31 | */ 32 | fun getAllSensors(vm: LightSensorViewModel) { 33 | 34 | // we subscribe to sensors topic 35 | service.subscribeToTopic(SensorsMqttService.TOPICS[1], 0, object : IMqttActionListener { 36 | override fun onSuccess(asyncActionToken: IMqttToken?) { 37 | Log.d("Mqtt", "Success subscribing to topic ${SensorsMqttService.TOPICS[0]}") 38 | } 39 | 40 | override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) { 41 | Log.d("Mqtt", "Failure on topic subscription") 42 | } 43 | }, IMqttMessageListener { topic, message -> 44 | 45 | val msgObj = JSONObject(message.toString()) 46 | val type = msgObj.getString(SensorsMqttService.MQTT_MESSAGE_TYPE) 47 | val payload = msgObj.getJSONObject(SensorsMqttService.MQTT_MESSAGE_PAYLOAD) 48 | 49 | when(type){ 50 | 51 | "sensors_info" -> { 52 | val array = payload.getJSONArray("sensors_info") 53 | vm.updateSensorsInfo(SensorMapper().mapJsonArray(array)) 54 | } 55 | 56 | "update_sensor" -> { 57 | val obj = JSONObject(message.toString()).getJSONObject(SensorsMqttService.MQTT_MESSAGE_PAYLOAD).getJSONObject("sensor_info") 58 | vm.updateSensor(SensorMapper().mapJson(obj)) 59 | } 60 | 61 | } 62 | 63 | }) 64 | 65 | val message = MqttMessage() 66 | val jsonMessage = JSONObject() 67 | jsonMessage.put(SensorsMqttService.MQTT_MESSAGE_TYPE, GET_SENSORS) 68 | message.qos = 0 69 | message.payload = jsonMessage.toString().toByteArray() 70 | 71 | // here we ask for home sensors information 72 | service.publish(SensorsMqttService.TOPICS[0], message) 73 | 74 | } 75 | 76 | /** 77 | * Used for turning on or turning off a light. When the MqttServer receive the message 78 | * and the light state is changed, we will be notified by the "update_sensor" 79 | * message type that is listened on [getAllSensors] method. 80 | */ 81 | fun updateSensorState(customLightSensor: CustomLightSensor, turnedOn: Boolean){ 82 | val message = MqttMessage() 83 | val jsonMessage = JSONObject() 84 | val sensorInfo = JSONObject() 85 | jsonMessage.put(SensorsMqttService.MQTT_MESSAGE_TYPE, UPDATE_LIGHT_STATE) 86 | 87 | sensorInfo.put("sensor_id", customLightSensor.id) 88 | sensorInfo.put("isTurnedOn", turnedOn) 89 | 90 | jsonMessage.put("sensor_info", sensorInfo) 91 | 92 | message.qos = 0 93 | message.payload = jsonMessage.toString().toByteArray() 94 | 95 | // here we ask for home sensors information 96 | service.publish(SensorsMqttService.TOPICS[0], message) 97 | 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/viewmodel/LightSensorViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.viewmodel 2 | 3 | import android.arch.lifecycle.LiveData 4 | import android.arch.lifecycle.MutableLiveData 5 | import android.arch.lifecycle.ViewModel 6 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 7 | 8 | /** 9 | * LightSensorViewModel 10 | * 11 | * @author Jose Flavio - jflavio90@gmail.com 12 | * @since 9/5/17 13 | */ 14 | class LightSensorViewModel : ViewModel() { 15 | 16 | private var tempSensorsList = MutableLiveData>() 17 | 18 | /** 19 | * UpdateSensor is a method that is going to receive a [CustomLightSensor] object 20 | * from [com.jflavio1.androidmqttexample.repository.SensorsRepository] and it's going 21 | * to search the sensor on the sensors list and update it. 22 | */ 23 | fun updateSensor(sensor: CustomLightSensor){ 24 | for (i in 0..(tempSensorsList.value!!.size - 1)){ 25 | if(tempSensorsList.value!![i].id == sensor.id){ 26 | tempSensorsList.value!![i] = sensor 27 | } 28 | } 29 | this.tempSensorsList.postValue(tempSensorsList.value) 30 | } 31 | 32 | /** 33 | * UpdateSensorsInfo is a method that given an [ArrayList] of [CustomLightSensor] 34 | * will update all list. 35 | */ 36 | fun updateSensorsInfo(list: ArrayList){ 37 | this.tempSensorsList.postValue(list) 38 | } 39 | 40 | /** 41 | * GetSensors will return the current [ArrayList] as a [LiveData] object. 42 | */ 43 | fun getSensors(): LiveData> { 44 | return tempSensorsList 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/views/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.views 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.support.v7.widget.LinearLayoutManager 6 | import android.widget.Toast 7 | import com.jflavio1.androidmqttexample.R 8 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 9 | import com.jflavio1.androidmqttexample.presenters.SensorsListPresenter 10 | import com.jflavio1.androidmqttexample.presenters.SensorsListPresenterImpl 11 | import kotlinx.android.synthetic.main.activity_main.* 12 | 13 | /** 14 | * MainActivity 15 | * 16 | * @author Jose Flavio - jflavio90@gmail.com 17 | * @since 6/5/17 18 | */ 19 | class MainActivity : AppCompatActivity(), SensorsListView { 20 | 21 | lateinit var presenter: SensorsListPresenter 22 | lateinit private var sensorsAdapter: SensorsAdapter 23 | 24 | override fun onCreate(savedInstanceState: Bundle?) { 25 | super.onCreate(savedInstanceState) 26 | setContentView(R.layout.activity_main) 27 | SensorsListPresenterImpl(this) 28 | 29 | sensorsAdapter = SensorsAdapter(object : SensorsAdapter.SensorsAdapterListener { 30 | override fun onSensorLightClick(sensor: CustomLightSensor) { 31 | presenter.changeLightState(sensor, !sensor.lightOn) 32 | } 33 | }) 34 | 35 | mainActivity_rv.apply { 36 | layoutManager = LinearLayoutManager(this@MainActivity) 37 | setHasFixedSize(true) 38 | adapter = sensorsAdapter 39 | } 40 | 41 | } 42 | 43 | override fun onDestroy() { 44 | this.presenter.stopMqttService() 45 | super.onDestroy() 46 | } 47 | 48 | override fun setSensorPresenter(presenter: SensorsListPresenter) { 49 | this.presenter = presenter 50 | this.presenter.initMqttService() 51 | } 52 | 53 | override fun onMqttConnected() { 54 | this.presenter.getTemperatures() 55 | } 56 | 57 | override fun onMqttError(errorMessage: String) { 58 | Toast.makeText(this, "Error on connection: $errorMessage", Toast.LENGTH_SHORT).show() 59 | } 60 | 61 | override fun onMqttDisconnected() { 62 | Toast.makeText(this, "Disconnected from server...", Toast.LENGTH_SHORT).show() 63 | } 64 | 65 | override fun onMqttStopped() { 66 | } 67 | 68 | override fun setSensorsTemperature(sensors: ArrayList) { 69 | sensorsAdapter.updateAllList(sensors) 70 | } 71 | 72 | override fun getViewContext() = this 73 | 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/views/SensorsAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.views 2 | 3 | import android.support.v7.widget.RecyclerView 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.ImageView 8 | import android.widget.TextView 9 | import com.jflavio1.androidmqttexample.R 10 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 11 | 12 | /** 13 | * SensorsAdapter 14 | * 15 | * @author Jose Flavio - jflavio90@gmail.com 16 | * @since 13/5/17 17 | */ 18 | class SensorsAdapter(val listener: SensorsAdapterListener) : RecyclerView.Adapter() { 19 | 20 | private var sensorsList = arrayListOf() 21 | 22 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder { 23 | return Holder(LayoutInflater.from(parent.context).inflate(R.layout.item_sensor, parent, false)) 24 | } 25 | 26 | fun updateAllList(list: ArrayList) { 27 | this.sensorsList = list 28 | notifyDataSetChanged() 29 | } 30 | 31 | override fun getItemCount() = sensorsList.size 32 | 33 | override fun onBindViewHolder(holder: Holder, position: Int) { 34 | holder.fillData(sensorsList[position]) 35 | holder.itemView.findViewById(R.id.itemSensor_iv_light).setOnClickListener { 36 | listener.onSensorLightClick(sensorsList[position]) 37 | } 38 | } 39 | 40 | class Holder(itemView: View?) : RecyclerView.ViewHolder(itemView) { 41 | 42 | fun fillData(sensor: CustomLightSensor) { 43 | itemView.findViewById(R.id.itemSensor_tv_name).text = sensor.name 44 | 45 | if (sensor.lightOn) { 46 | itemView.findViewById(R.id.itemSensor_iv_light).setBackgroundResource(R.drawable.ic_light) 47 | } else { 48 | itemView.findViewById(R.id.itemSensor_iv_light).setBackgroundResource(R.drawable.ic_light_off) 49 | } 50 | 51 | } 52 | } 53 | 54 | interface SensorsAdapterListener { 55 | fun onSensorLightClick(sensor: CustomLightSensor) 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jflavio1/androidmqttexample/views/SensorsListView.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample.views 2 | 3 | import android.content.Context 4 | import com.jflavio1.androidmqttexample.model.CustomLightSensor 5 | import com.jflavio1.androidmqttexample.presenters.SensorsListPresenter 6 | 7 | /** 8 | * SensorsListView 9 | * 10 | * @author Jose Flavio - jflavio90@gmail.com 11 | * @since 6/5/17 12 | */ 13 | interface SensorsListView { 14 | 15 | fun setSensorPresenter(presenter: SensorsListPresenter) 16 | 17 | fun onMqttConnected() 18 | 19 | fun onMqttError(errorMessage: String) 20 | 21 | fun onMqttDisconnected() 22 | 23 | fun onMqttStopped() 24 | 25 | fun setSensorsTemperature(sensors: ArrayList) 26 | 27 | fun getViewContext() : Context 28 | 29 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_light.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_light_off.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 20 | 21 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_sensor.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 18 | 19 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #F1EE33 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidMqttExample 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/jflavio1/androidmqttexample/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.jflavio1.androidmqttexample 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.2.41' 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.2' 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | ext { 17 | rxKotlinVersion = "2.2.0" 18 | rxAndroidVersion = "2.0.2" 19 | 20 | libs = [ 21 | rxKotlin: ('io.reactivex.rxjava2:rxkotlin:' + rxKotlinVersion), 22 | rxAndroid: ('io.reactivex.rxjava2:rxandroid:' + rxAndroidVersion), 23 | ] 24 | } 25 | } 26 | 27 | allprojects { 28 | repositories { 29 | google() 30 | jcenter() 31 | } 32 | } 33 | 34 | task clean(type: Delete) { 35 | delete rootProject.buildDir 36 | } 37 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jflavio11/MqttAndroidExample/4b01cdcc4cea47cd4b96b076c415506f341b0fb6/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri May 11 11:43:10 PET 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------