Created
January 19, 2016 08:26
-
-
Save KazuyukiEguchi/2f43353cfacea546e1f1 to your computer and use it in GitHub Desktop.
MQTT クライアント を Android に実装する ref: http://qiita.com/KazuyukiEguchi/items/c67524e8b3c9c6459b2d
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
<uses-permission android:name="android.permission.WAKE_LOCK" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
... 省略 | |
<service android:name="org.eclipse.paho.android.service.MqttService" > | |
</service> | |
</application> |
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
android { | |
... 省略 | |
packagingOptions { | |
exclude 'META-INF/ECLIPSE_.SF' | |
exclude 'META-INF/ECLIPSE_.RSA' | |
} | |
} |
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
{ | |
"host": "lite.mqtt.shiguredo.jp", | |
"port": 1883, | |
"options": { | |
"username": "hoge@github", | |
"password": "sango", | |
"keepalive": 10000 | |
}, | |
"topic": "hoge@github" | |
} |
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
btn01.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(mqttAndroidClient == null) | |
return; | |
try { | |
if(mqttAndroidClient.isConnected()) { | |
IMqttDeliveryToken token = mqttAndroidClient.publish("hoge@github/test", "hello world".getBytes(), 0, true); | |
} | |
} catch (MqttPersistenceException e) { | |
Log.d(TAG,e.toString()); | |
} catch (MqttException e) { | |
Log.d(TAG,e.toString()); | |
} | |
} | |
}); |
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
var mqtt = require("mqtt"), | |
config = require("./config.json"), | |
client = mqtt.createClient(config.port, config.host, config.options); | |
setInterval(function() { | |
client.publish(config.topic + "/test", "abc"); | |
}, 30000); | |
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
var mqtt = require("mqtt"), | |
config = require("./config.json"), | |
client = mqtt.createClient(config.port, config.host, config.options); | |
client.subscribe(config.topic + "/#"); | |
client.on("message", function(topic, message) { | |
console.log(topic, message.toString()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment