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

Hii Dasha,
I have build my application and it is working while Iโ€™m using HIVEMQ credentials but when I use my own server address and port it just show up an error:>

W/DefaultChannelPipeline: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.channel.ChannelPipelineException: com.hivemq.client.internal.mqtt.handler.MqttChannelInitializer.handlerAdded() has thrown an exception; removed.
at io.netty.channel.DefaultChannelPipeline.callHandlerAdded0(DefaultChannelPipeline.java:624)
at io.netty.channel.DefaultChannelPipeline.access$100(DefaultChannelPipeline.java:46)
at io.netty.channel.DefaultChannelPipeline$PendingHandlerAddedTask.execute(DefaultChannelPipeline.java:1463)
at io.netty.channel.DefaultChannelPipeline.callHandlerAddedForAllHandlers(DefaultChannelPipeline.java:1115)
at io.netty.channel.DefaultChannelPipeline.invokeHandlerAddedIfNeeded(DefaultChannelPipeline.java:650)
at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:502)
at io.netty.channel.AbstractChannel$AbstractUnsafe.access$200(AbstractChannel.java:417)
at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:474)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:1012)
Caused by: java.lang.IllegalArgumentException: Invalid input to toASCII: tcp://139.59.31.99
at java.net.IDN.toASCII(IDN.java:115)
at javax.net.ssl.SNIHostName.(SNIHostName.java:99)
at com.android.org.conscrypt.Platform.getSSLParameters(Platform.java:187)
at com.android.org.conscrypt.ConscryptEngine.getSSLParameters(ConscryptEngine.java:525)
at com.android.org.conscrypt.Java8EngineWrapper.getSSLParameters(Java8EngineWrapper.java:62)
at com.hivemq.client.internal.mqtt.handler.ssl.MqttSslInitializer.initChannel(MqttSslInitializer.java:70)
at com.hivemq.client.internal.mqtt.handler.MqttChannelInitializer.initSsl(MqttChannelInitializer.java:121)
at com.hivemq.client.internal.mqtt.handler.MqttChannelInitializer.initProxy(MqttChannelInitializer.java:110)
at com.hivemq.client.internal.mqtt.handler.MqttChannelInitializer.handlerAdded(MqttChannelInitializer.java:104)
at io.netty.channel.AbstractChannelHandlerContext.callHandlerAdded(AbstractChannelHandlerContext.java:971)
at io.netty.channel.DefaultChannelPipeline.callHandlerAdded0(DefaultChannelPipeline.java:609)
at io.netty.channel.DefaultChannelPipeline.access$100(DefaultChannelPipeline.java:46)
at io.netty.channel.DefaultChannelPipeline$PendingHandlerAddedTask.execute(DefaultChannelPipeline.java:1463)
at io.netty.channel.DefaultChannelPipeline.callHandlerAddedForAllHandlers(DefaultChannelPipeline.java:1115)
at io.netty.channel.DefaultChannelPipeline.invokeHandlerAddedIfNeeded(DefaultChannelPipeline.java:650)
at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:502)
at io.netty.channel.AbstractChannel$AbstractUnsafe.access$200(AbstractChannel.java:417)
at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:474)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:1012)
Caused by: The input does not conform to the STD 3 ASCII rules. line: 0. preContext: . postContext: //139

    at android.icu.impl.IDNA2003.convertToASCII(IDNA2003.java:218)
    at android.icu.impl.IDNA2003.convertIDNToASCII(IDNA2003.java:276)
    at android.icu.text.IDNA.convertIDNToASCII(IDNA.java:654)
    at com.android.icu.text.ExtendedIDNA.convertIDNToASCII(ExtendedIDNA.java:50)
    at java.net.IDN.toASCII(IDN.java:108)
    	... 24 more

Hi @jugendra ,

As far as I understand, you have tested your application and it is working with HiveMQ Cloud, now you have installed your own HiveMQ broker and your app fails. If so, then I need some more info to help you troubleshoot this.

  • your source code, that is throwing the exception
  • your HiveMQ config.xml file from $HIVEMQ_HOME/conf/config.xml
  • your HiveMQ log file from $HIVEMQ_HOME/log/hivemq.log
  • what other extensions do you have enabled,
  • do you have Enterprise Security Extension enabled

Thanks,
Kind regards,
Dasha from HiveMQ team