Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MemoryPersistence mPer = new MemoryPersistence();
- String clientId = UUID.randomUUID().toString();
- String brokerUrl = "tcp://192.168.1.102:1883";
- MqttAndroidClient c = new MqttAndroidClient(getApplicationContext(), brokerUrl, clientId, mPer);
- IMqttToken tok;
- try {
- tok = c.connect();
- } catch (MqttException e) {
- String emsg = getResources().getString(R.string.err_mqtt_connect_1) + e.getMessage() ;
- Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_SHORT).show();
- return;
- }
- try {
- tok.waitForCompletion(3000);
- } catch (MqttException e) {
- String emsg = getResources().getString(R.string.err_mqtt_connect_2) + e.getMessage() ;
- Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_LONG).show();
- return;
- }
- Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
- String pubTopic = "random/moreRandom";
- MqttMessage msg = new MqttMessage();
- String payload = "samepl message"
- msg.setPayload(payload.getBytes());
- msg.setQos(2);
- msg.setRetained(false);
- try {
- c.publish(pubTopic, msg);
- } catch (MqttPersistenceException e) {
- String emsg = getResources().getString(R.string.err_mqtt_publish_1) + e.getMessage() ;
- Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_SHORT).show();
- return;
- } catch (MqttException e) {
- String emsg = getResources().getString(R.string.err_mqtt_publish_2) + e.getMessage() ;
- Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_SHORT).show();
- return;
- } catch (NullPointerException e) {
- e.printStackTrace();
- }
- Toast.makeText(getApplicationContext(), "Published", Toast.LENGTH_SHORT).show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement