Android studio first project

First my apologies. I am a longtime developer but that was when C++ was king :slight_smile:

Now I want to build an Android app to interact with my hivemq broker. I own a software company and I am too stubborn to ask our developers for help :frowning:

But I have downloaded the hivemq client. And the paho client.

Do I really have to compile those libraries or are ready to use libraries exist?

My app compiles but dumps when I call the connect method.

Also - do I use my cryptic hivemq irl or broker.hivemq.com and if the latter does it depend on username and password to route the payloads?

And last but certainly not least - does anyone have a studio project that will just compile and run?

My humble thanks to all.

Ernest Riggen

Hi Ernest,

You have asked several questions that I am going to address one by one.

You have asked: " Also - do I use my cryptic hivemq irl or broker.hivemq.com and if the latter does it depend on username and password to route the payloads?"

Answer:

  • broker.hivemq.com is a public broker, that anybody can connect (no authorization, like username and password, is used). Ports are 1883 (TCP) and 8000 (WebSocket).
  • HiveMQ cloud broker (the one with the โ€œcrypticโ€ URL that you access via cloud.hivemq.com) is your personal one, only you are authorised to connect, using simple authentication with username and password. The connection is SSL encrypted. Ports are 8883 (TLS) and 8884 (WebSocket).

You asked: โ€œdoes anyone have a studio project that will just compile and run?โ€

I do not think it makes sense to share my project, since you can always generate a new sample project from the Android Studio itself. Please note that you must select Java as the project language!

Follow these detailed instructions to โ€œtellโ€ your Android Studio project to use the HiveMQ MQTT Client library: Android - HiveMQ MQTT Client

Basically, in order to use HiveMQ MQTT Client library in the Android Studio project, you actually do not have to download the library. You only need to add it to the project dependencies in the appโ€™s build.gradle file:

dependencies {
    implementation 'com.hivemq:hivemq-mqtt-client:1.3.0'
    ...
}

You have to grant your app the permission to use internet communication in the AndroidManifest.xml file:

<manifest>
    ...
    <uses-permission android:name="android.permission.INTERNET"/>
    ...
</manifest>

You also need to update the ProGuard settings as described in the doc.

When the preparations are done, you are ready to use the MQTT Client library in your Java code:

import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;


public class MyConnectionMaker {

    public String connect() {
        try {
            // attempt a connect
            Mqtt5Client client = Mqtt5Client.builder()
                .identifier(UUID.randomUUID().toString())
                .serverHost("18b93f452b8445ba86ac0ec6d2228eb9.s2.eu.hivemq.cloud")
                .serverPort(8883)
                .sslWithDefaultConfig()
                .simpleAuth()
                .username("HiveUser1")
                .password("HiveUser1".getBytes())
                .applySimpleAuth()
                .build();

            Mqtt5ConnAck connAckMessage = client.toBlocking().connect();
            //success
            return connAckMessage.getReasonCode().toString();

        } catch (Exception e) {
            //failure
            return e.getMessage();
        }
    }
}

After the source code is ready and has no errors, you can run your project in the Android Studio on an Emulator:

I hope this helps, but if you have further questions please let me know.

Kind regards,
Dasha from HiveMQ Team

1 Like