Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it compatible with Android? #70

Open
Robson96 opened this issue Nov 24, 2024 · 5 comments
Open

Is it compatible with Android? #70

Robson96 opened this issue Nov 24, 2024 · 5 comments

Comments

@Robson96
Copy link

Robson96 commented Nov 24, 2024

When I try to run it, I get this error:

object ClientMqtt {
  val client = MQTTClient(
    MQTTVersion.MQTT5,
    "mqtt://192.168.18.100",
    1883,
    null,
  ) {
    it.payload?.toByteArray()?.toString(Charset.defaultCharset())
      ?.let { it1 -> Logger.d("AppChip", it1) }
  }
}
lifecycleScope.launch(Dispatchers.IO) {
        ClientMqtt.client.publish(false, Qos.EXACTLY_ONCE, "events/temperatura/user/userId/in", "hello".encodeToByteArray().toUByteArray())
        ClientMqtt.client.run()
      }
implementation("io.github.davidepianca98:kmqtt-common-jvm:1.0.0")
implementation("io.github.davidepianca98:kmqtt-client-jvm:1.0.0")

E FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.system.process, PID: 11679

@davidepianca98
Copy link
Owner

Hello, please post the full exception. The library is compatible with Android.

@Robson96
Copy link
Author

I just display this error message

FATAL EXCEPTION: DefaultDispatcher-worker-3
Process: com.system.process, PID: 10842

@davidepianca98
Copy link
Owner

This is not the actual exception that is triggering the crash. There has to be more a longer stack trace. Otherwise I cannot do much with that info only, unfortunately.

@RankoR
Copy link

RankoR commented Jan 25, 2025

@davidepianca98 I'm getting the same on Android 13 on Pixel 6.

The only log in logcat is

FATAL EXCEPTION: DefaultDispatcher-worker-8
                                                                                                    Process: %package%, PID: 22760

If I set an exception handler while calling runSuspend, it's getting called with this:

Exception occured in MQTT: Read to buffer error End Of Stream (-1)
                                                                                                    io.github.davidepianca98.socket.SocketClosedException: Read to buffer error End Of Stream (-1)
                                                                                                    	at io.github.davidepianca98.socket.tcp.Socket.readToBuffer(Socket.kt:65)
                                                                                                    	at io.github.davidepianca98.socket.tcp.Socket.read--5HJl4c(Socket.kt:75)
                                                                                                    	at io.github.davidepianca98.ClientSocket.read--5HJl4c(ClientSocket.kt:40)
                                                                                                    	at io.github.davidepianca98.MQTTClient.check(MQTTClient.kt:389)
                                                                                                    	at io.github.davidepianca98.MQTTClient.step(MQTTClient.kt:466)
                                                                                                    	at io.github.davidepianca98.MQTTClient.run(MQTTClient.kt:476)
                                                                                                    	at io.github.davidepianca98.MQTTClient$runSuspend$2.invokeSuspend(MQTTClient.kt:491)
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                    	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:829)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704)

This error happens when the device goes to sleep and network activity gets suspended, but it doesn't lead to the crash directly.

It detects that it's disconnected, then I'm trying to reconnect by creating a new instance and a second later it crashes with the crash log that OP mentioned.

Here are the full logs:

2025-01-25 16:25:04.666 22760-22814 PlatformMqttClient      redacted             D  onDisconnected
2025-01-25 16:25:04.668 22760-22814 PlatformMqttClient      redacted             D  Disconnection was not initiated by us, will reconnect after 1000 ms
2025-01-25 16:25:04.678 22760-22814 PlatformMqttClient      redacted             E  Exception occured in MQTT: Read to buffer error End Of Stream (-1)
                                                                                                    io.github.davidepianca98.socket.SocketClosedException: Read to buffer error End Of Stream (-1)
                                                                                                    	at io.github.davidepianca98.socket.tcp.Socket.readToBuffer(Socket.kt:65)
                                                                                                    	at io.github.davidepianca98.socket.tcp.Socket.read--5HJl4c(Socket.kt:75)
                                                                                                    	at io.github.davidepianca98.ClientSocket.read--5HJl4c(ClientSocket.kt:40)
                                                                                                    	at io.github.davidepianca98.MQTTClient.check(MQTTClient.kt:389)
                                                                                                    	at io.github.davidepianca98.MQTTClient.step(MQTTClient.kt:466)
                                                                                                    	at io.github.davidepianca98.MQTTClient.run(MQTTClient.kt:476)
                                                                                                    	at io.github.davidepianca98.MQTTClient$runSuspend$2.invokeSuspend(MQTTClient.kt:491)
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                    	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:829)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704)
2025-01-25 16:25:05.670 22760-22814 PlatformMqttClient      redacted             D  connectInternal
2025-01-25 16:25:05.672 22760-22814 TrafficStats            redacted             D  tagSocket(5) with statsTag=0xffffffff, statsUid=-1
2025-01-25 16:25:05.681 22760-22814 AndroidRuntime          redacted             E  FATAL EXCEPTION: DefaultDispatcher-worker-8
                                                                                                    Process: redacted, PID: 22760
2025-01-25 16:25:05.708 22760-22814 Process                 redacted             I  Sending signal. PID: 22760 SIG: 9

@RankoR
Copy link

RankoR commented Jan 25, 2025

@davidepianca98 I've added a demo here: https://github.com/RankoR/kmqtt-android-crash-demo

It crashes every time if you follow the instructions.

Edit: It crashes here io/github/davidepianca98/ClientSocket.kt:27. Easier way to reproduce without putting device to sleep - turn on the airplane mode.

I guess that there is no readable message because the socket connection happens in the constructor and NIO throws while constructing objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants