├── .gitignore
├── AndroidManifest.xml
├── ant.properties
├── build.xml
├── libs
├── paho-mqtt-client-1.0.1-sources.jar
└── paho-mqtt-client-1.0.1.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ ├── ic_action_search.png
│ ├── ic_launcher.png
│ └── snow.png
├── drawable-ldpi
│ └── ic_launcher.png
├── drawable-mdpi
│ ├── ic_action_search.png
│ ├── ic_launcher.png
│ └── snow.png
├── drawable-xhdpi
│ ├── ic_action_search.png
│ ├── ic_launcher.png
│ └── snow.png
├── drawable
│ └── blackice.png
├── layout
│ └── main.xml
└── values
│ ├── colors.xml
│ └── strings.xml
└── src
└── de
└── eclipsemagazin
└── mqtt
├── publisher
└── Thermometer.java
└── push
├── BlackIceActivity.java
├── MQTTService.java
└── PushCallback.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea/
2 | /gen/
3 | /local.properties
4 | /out/
5 | /mqtt-android-push.iml
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ant.properties:
--------------------------------------------------------------------------------
1 | # This file is used to override default values used by the Ant build system.
2 | #
3 | # This file must be checked into Version Control Systems, as it is
4 | # integral to the build system of your project.
5 |
6 | # This file is only used by the Ant script.
7 |
8 | # You can use this to override default values such as
9 | # 'source.dir' for the location of your java source folder and
10 | # 'out.dir' for the location of your output folder.
11 |
12 | # You can also use it define how the release builds are signed by declaring
13 | # the following properties:
14 | # 'key.store' for the location of your keystore and
15 | # 'key.alias' for the name of the key to use.
16 | # The password will be asked during the build when you use the 'release' target.
17 |
18 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
29 |
30 |
31 |
35 |
36 |
37 |
38 |
39 |
40 |
49 |
50 |
51 |
52 |
56 |
57 |
69 |
70 |
71 |
89 |
90 |
91 |
92 |
93 |
--------------------------------------------------------------------------------
/libs/paho-mqtt-client-1.0.1-sources.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/libs/paho-mqtt-client-1.0.1-sources.jar
--------------------------------------------------------------------------------
/libs/paho-mqtt-client-1.0.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/libs/paho-mqtt-client-1.0.1.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-8
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-hdpi/ic_action_search.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/snow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-hdpi/snow.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-mdpi/ic_action_search.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/snow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-mdpi/snow.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-xhdpi/ic_action_search.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/snow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable-xhdpi/snow.png
--------------------------------------------------------------------------------
/res/drawable/blackice.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dobermai/android-mqtt-push/16b28097afdb45b08f061571467f95b78bd62b06/res/drawable/blackice.png
--------------------------------------------------------------------------------
/res/layout/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
15 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ffffff
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | mqtt-android-push
4 |
5 |
--------------------------------------------------------------------------------
/src/de/eclipsemagazin/mqtt/publisher/Thermometer.java:
--------------------------------------------------------------------------------
1 | package de.eclipsemagazin.mqtt.publisher;
2 |
3 | import org.eclipse.paho.client.mqttv3.MqttClient;
4 | import org.eclipse.paho.client.mqttv3.MqttException;
5 | import org.eclipse.paho.client.mqttv3.MqttMessage;
6 | import org.eclipse.paho.client.mqttv3.MqttTopic;
7 | import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;
8 |
9 | import java.util.Random;
10 |
11 | public class Thermometer {
12 |
13 | public static final String BROKER_URL = "tcp://broker.mqttdashboard.com:1883";
14 | //public static final String BROKER_URL = "tcp://test.mosquitto.org:1883";
15 |
16 | public static final String TOPIC = "de/eclipsemagazin/blackice/warnings";
17 |
18 | private MqttClient client;
19 |
20 |
21 | public Thermometer() {
22 | try {
23 | client = new MqttClient(BROKER_URL, MqttClient.generateClientId(), new MemoryPersistence());
24 | } catch (MqttException e) {
25 | e.printStackTrace();
26 | System.exit(1);
27 | }
28 | }
29 |
30 | private void start() {
31 |
32 | try {
33 | client.connect();
34 |
35 | publishTemperature();
36 |
37 | client.disconnect();
38 |
39 | } catch (MqttException e) {
40 | e.printStackTrace();
41 | System.exit(1);
42 | }
43 | }
44 |
45 | private void publishTemperature() throws MqttException {
46 | final MqttTopic temperatureTopic = client.getTopic(TOPIC);
47 |
48 | final int temperature = createRandomNumberBetween(-20, 4);
49 |
50 | final MqttMessage message = new MqttMessage(String.valueOf(temperature).getBytes());
51 | temperatureTopic.publish(message);
52 |
53 | System.out.println("Published data. Topic: " + temperatureTopic.getName() + " Message: " + temperature);
54 | }
55 |
56 | public static int createRandomNumberBetween(int min, int max) {
57 |
58 | return new Random().nextInt(max - min + 1) + min;
59 | }
60 |
61 | public static void main(String... args) {
62 | final Thermometer thermometer = new Thermometer();
63 | thermometer.start();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/de/eclipsemagazin/mqtt/push/BlackIceActivity.java:
--------------------------------------------------------------------------------
1 | package de.eclipsemagazin.mqtt.push;
2 |
3 | import android.app.Activity;
4 | import android.app.ActivityManager;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.os.Bundle;
8 | import android.view.View;
9 | import android.widget.Button;
10 |
11 | public class BlackIceActivity extends Activity {
12 |
13 | public static final String SERVICE_CLASSNAME = "de.eclipsemagazin.mqtt.push.MQTTService";
14 | private Button button;
15 |
16 | @Override
17 | public void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.main);
20 |
21 | button = (Button) findViewById(R.id.button1);
22 | updateButton();
23 |
24 | }
25 |
26 | @Override
27 | protected void onResume() {
28 | super.onResume();
29 | updateButton();
30 | }
31 |
32 | private void updateButton() {
33 | if (serviceIsRunning()) {
34 | button.setText("Stop Service");
35 | button.setOnClickListener(new View.OnClickListener() {
36 | @Override
37 | public void onClick(View view) {
38 | button.setText("Start Service");
39 | stopBlackIceService();
40 | updateButton();
41 | }
42 | });
43 |
44 | } else {
45 | button.setText("Start Service");
46 | button.setOnClickListener(new View.OnClickListener() {
47 | @Override
48 | public void onClick(View view) {
49 | button.setText("Stop Service");
50 | startBlackIceService();
51 | updateButton();
52 | }
53 | });
54 | }
55 | }
56 |
57 | private void startBlackIceService() {
58 |
59 | final Intent intent = new Intent(this, MQTTService.class);
60 | startService(intent);
61 | }
62 |
63 | private void stopBlackIceService() {
64 |
65 | final Intent intent = new Intent(this, MQTTService.class);
66 | stopService(intent);
67 | }
68 |
69 | private boolean serviceIsRunning() {
70 | ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
71 | for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
72 | if (SERVICE_CLASSNAME.equals(service.service.getClassName())) {
73 | return true;
74 | }
75 | }
76 | return false;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/de/eclipsemagazin/mqtt/push/MQTTService.java:
--------------------------------------------------------------------------------
1 | package de.eclipsemagazin.mqtt.push;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.os.IBinder;
6 | import android.widget.Toast;
7 | import org.eclipse.paho.client.mqttv3.MqttClient;
8 | import org.eclipse.paho.client.mqttv3.MqttException;
9 | import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;
10 |
11 | /**
12 | * @author Dominik Obermaier
13 | */
14 | public class MQTTService extends Service {
15 |
16 | public static final String BROKER_URL = "tcp://broker.mqttdashboard.com:1883";
17 | //public static final String BROKER_URL = "tcp://test.mosquitto.org:1883";
18 |
19 | /* In a real application, you should get an Unique Client ID of the device and use this, see
20 | http://android-developers.blogspot.de/2011/03/identifying-app-installations.html */
21 | public static final String clientId = "android-client";
22 |
23 | public static final String TOPIC = "de/eclipsemagazin/blackice/warnings";
24 | private MqttClient mqttClient;
25 |
26 |
27 | public IBinder onBind(Intent intent) {
28 | return null;
29 | }
30 |
31 | @Override
32 | public void onStart(Intent intent, int startId) {
33 |
34 | try {
35 | mqttClient = new MqttClient(BROKER_URL, clientId, new MemoryPersistence());
36 |
37 | mqttClient.setCallback(new PushCallback(this));
38 | mqttClient.connect();
39 |
40 | //Subscribe to all subtopics of homeautomation
41 | mqttClient.subscribe(TOPIC);
42 |
43 |
44 | } catch (MqttException e) {
45 | Toast.makeText(getApplicationContext(), "Something went wrong!" + e.getMessage(), Toast.LENGTH_LONG).show();
46 | e.printStackTrace();
47 | }
48 |
49 | super.onStart(intent, startId);
50 | }
51 |
52 | @Override
53 | public void onDestroy() {
54 | try {
55 | mqttClient.disconnect(0);
56 | } catch (MqttException e) {
57 | Toast.makeText(getApplicationContext(), "Something went wrong!" + e.getMessage(), Toast.LENGTH_LONG).show();
58 | e.printStackTrace();
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/de/eclipsemagazin/mqtt/push/PushCallback.java:
--------------------------------------------------------------------------------
1 | package de.eclipsemagazin.mqtt.push;
2 |
3 | import android.app.Notification;
4 | import android.app.NotificationManager;
5 | import android.app.PendingIntent;
6 | import android.content.Context;
7 | import android.content.ContextWrapper;
8 | import android.content.Intent;
9 | import org.eclipse.paho.client.mqttv3.MqttCallback;
10 | import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
11 | import org.eclipse.paho.client.mqttv3.MqttMessage;
12 | import org.eclipse.paho.client.mqttv3.MqttTopic;
13 |
14 |
15 | public class PushCallback implements MqttCallback {
16 |
17 | private ContextWrapper context;
18 |
19 | public PushCallback(ContextWrapper context) {
20 |
21 | this.context = context;
22 | }
23 |
24 | @Override
25 | public void connectionLost(Throwable cause) {
26 | //We should reconnect here
27 | }
28 |
29 | @Override
30 | public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception {
31 |
32 | final NotificationManager notificationManager = (NotificationManager)
33 | context.getSystemService(Context.NOTIFICATION_SERVICE);
34 |
35 | final Notification notification = new Notification(R.drawable.snow,
36 | "Black Ice Warning!", System.currentTimeMillis());
37 |
38 | // Hide the notification after its selected
39 | notification.flags |= Notification.FLAG_AUTO_CANCEL;
40 |
41 | final Intent intent = new Intent(context, BlackIceActivity.class);
42 | final PendingIntent activity = PendingIntent.getActivity(context, 0, intent, 0);
43 | notification.setLatestEventInfo(context, "Black Ice Warning", "Outdoor temperature is " +
44 | new String(message.getPayload()) + "°", activity);
45 | notification.number += 1;
46 | notificationManager.notify(0, notification);
47 | }
48 |
49 | @Override
50 | public void deliveryComplete(MqttDeliveryToken token) {
51 | //We do not need this because we do not publish
52 | }
53 | }
54 |
--------------------------------------------------------------------------------