2017. 9. 10. 17:24ㆍ안드로이드 개발
엄청 쉽다.
http://androidkt.com/android-mqtt/
Installation
The most convenient way to start a new Android Application is to use Android Studio. Download Paho Android Service and Android MQTT Client library.Go to your libs
folder inside app
folder and paste all your .jar
To add the Paho Android Service as a dependency to you app add the following parts to your gradle file.
1 2 3 4 | dependencies { compile files('libs/org.eclipse.paho.android.service-1.1.1.jar') compile files('libs/org.eclipse.paho.client.mqttv3-1.1.1.jar') } |
Permission
The Paho Android Service needs the following permissions to work
To be able to create a binding to the Paho Android Service, the service needs to be declared in the AndroidManifest.xml.
Add the following within the <application>
tag
Receive MQTT Message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ... pahoMqttClient = new PahoMqttClient(); mqttAndroidClient = pahoMqttClient.getMqttClient( getApplicationContext(), Constants.MQTT_BROKER_URL, Constants.CLIENT_ID); mqttAndroidClient.setCallback(new MqttCallbackExtended() { @Override public void connectComplete(boolean b, String s) { } @Override public void connectionLost(Throwable throwable) { } @Override public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { setMessageNotification(s, new String(mqttMessage.getPayload())); } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { } }); .... |
마지막으로 주요 기능을 wrapping한 PahoMqttClient 클래스만 하나 만들어주면 끝!
* MQTT Broker URL의 스키마는 tcp:// 이다!
https://stackoverflow.com/questions/24791118/android-paho-mqtt-service-for-publishing