Skip to content

andorid cannot connect mqtt #3658

@Wlazly

Description

@Wlazly

Describe the bug

the logcat is:
I/AWSIotMqttManager(16659): resetting reconnect attempt and retry time
D/AWSIotMqttManager(16659): MQTT broker: wss://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/mqtt:443
D/AWSIotMqttManager(16659): ready to do mqtt connect
I/AWSIotMqttManager(16659): metrics collection is enabled, username: ?SDK=Android&Version=2.77.1
I/AWSIotMqttManager(16659): resetting reconnect attempt and retry time
D/AWSIotMqttManager(16659): Setting up Callback for MqttClient
W/AWSIotMqttManager(16659): onFailure: connection failed.

I/AWSIotMqttManager(16659): schedule Reconnect attempt 0 of 10 in 4 seconds.
D/AWSIotMqttManager(16659): TID: 19822 trying to reconnect to session
I/AWSIotMqttManager(16659): attempting to reconnect to mqtt broker
D/AWSIotMqttManager(16659): Reconnect to mqtt broker: wss://xxxxx-ats.iot.ap-southeast-1.amazonaws.com/mqtt mqttWebSocketURL: wss://wss://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/mqtt:443/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA2ZIOM6VNJBWZGRCI%2F20250115%2Fap-southeast-1%2Fiotdata%2Faws4_request&X-Amz-Date=20250115T035520Z&X-Amz-SignedHeaders=host&X-Amz-Signature=3a2e39e084d9895eadf487969b04ef7efd27bdddcefa4366781590a878d0c7ac&X-Amz-Security-Token=FwoGZXIvYXdzED0aDA1rOP6ik6NJUGNgECK9A2%2FMWDxqbJNBkAYELrQzzpUnQfK8WBePgE1zypa9LjlnhuIMVwI5NBEFwyjq%2FArxMkg1YnjzB5rsQUVkAKPkw4hN0jPJJiEaMPoYtjR2XpBovK3%2F3rzS9wAlKomwFzWPHfVdtHHoB3lHslEFNTpTr0sdHJzlt3EUwbeKQxx3K6oTBnOkrMy32ALRpyHYHtb4udYCFA3noNEBjjQsSBh4U4e8QVzdHRU0GaUes9geawPV7tz%2B277UK9C5voSPAQSTFzPIyFPr5dXNAIN0sc22Bk9MO3Y61l0kI7GUkgg8B%2FvWnIPnbSK0OkxDPr6B6M%2FRsLRn7wEIRzYHROy6BA1oExSBXQk3xzIDya6OB%2BcBIVnqmipt78pJQwu0%2FNaGjwx3aSkNyAUh%2Bn%2BDs%2FYQGKj5%2By6KEADJGO6OHErcckxeyNWivzii6PqhvLVPJ6dVpxWPaovBhc8xBfVg07vrihEZwg18DtGFUZ7hTlQR5Mm8kTtg70FMV5Efnq94prB8eamPYxlYN3bLtOe1W%2BuG4z70arCDPya5kUa5NQtVyoyLBXfZ1tTfp1ni4Hg58%2BquSJCI4ylm0fQxgbw0H5h%2F0Z0oo%2BOcvAYyLdg%2Bbqwn48vRIv%2B6hGJrOQFXPM7vHDUBdy0gb7q70XEn6Li60Ii45J%2BKP2RGdQ%3D%3D
D/AWSIotMqttManager(16659): Setting up Callback for MqttClient
D/AWSIotMqttManager(16659): mqtt reconnecting attempt 1
W/AWSIotMqttManager(16659): Reconnect failed

I/AWSIotMqttManager(16659): schedule Reconnect attempt 1 of 10 in 8 seconds.
D/AWSIotMqttManager(16659): TID: 19827 trying to reconnect to session
I/AWSIotMqttManager(16659): attempting to reconnect to mqtt broker
D/AWSIotMqttManager(16659): Reconnect to mqtt broker: wss://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/mqtt mqttWebSocketURL: wss://wss://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/mqtt:443/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA2ZIOM6VNJBWZGRCI%2F20250115%2Fap-southeast-1%2Fiotdata%2Faws4_request&X-Amz-Date=20250115T035528Z&X-Amz-SignedHeaders=host&X-Amz-Signature=4306b5c197afe94cb42941ebabf11ca1c4596c5d6fbc4c23679b903e7824167e&X-Amz-Security-Token=FwoGZXIvYXdzED0aDA1rOP6ik6NJUGNgECK9A2%2FMWDxqbJNBkAYELrQzzpUnQfK8WBePgE1zypa9LjlnhuIMVwI5NBEFwyjq%2FArxMkg1YnjzB5rsQUVkAKPkw4hN0jPJJiEaMPoYtjR2XpBovK3%2F3rzS9wAlKomwFzWPHfVdtHHoB3lHslEFNTpTr0sdHJzlt3EUwbeKQxx3K6oTBnOkrMy32ALRpyHYHtb4udYCFA3noNEBjjQsSBh4U4e8QVzdHRU0GaUes9geawPV7tz%2B277UK9C5voSPAQSTFzPIyFPr5dXNAIN0sc22Bk9MO3Y61l0kI7GUkgg8B%2FvWnIPnbSK0OkxDPr6B6M%2FRsLRn7wEIRzYHROy6BA1oExSBXQk3xzIDya6OB%2BcBIVnqmipt78pJQwu0%2FNaGjwx3aSkNyAUh%2Bn%2BDs%2FYQGKj5%2By6KEADJGO6OHErcckxeyNWivzii6PqhvLVPJ6dVpxWPaovBhc8xBfVg07vrihEZwg18DtGFUZ7hTlQR5Mm8kTtg70FMV5Efnq94prB8eamPYxlYN3bLtOe1W%2BuG4z70arCDPya5kUa5NQtVyoyLBXfZ1tTfp1ni4Hg58%2BquSJCI4ylm0fQxgbw0H5h%2F0Z0oo%2BOcvAYyLdg%2Bbqwn48vRIv%2B6hGJrOQFXPM7vHDUBdy0gb7q70XEn6Li60Ii45J%2BKP2RGdQ%3D%3D
D/AWSIotMqttManager(16659): Setting up Callback for MqttClient
D/AWSIotMqttManager(16659): mqtt reconnecting attempt 2
W/AWSIotMqttManager(16659): Reconnect failed

I/AWSIotMqttManager(16659): schedule Reconnect attempt 2 of 10 in 16 seconds.
xxx
xxx
xxxx
xxxx
xxxx
xxxxx

code is:

`

   LogFactory.setLevel(LogFactory.Level.ALL);
    // 替换为你的 AWS IoT 配置
    val endpoint = "wss://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/mqtt" // 替换为 AWS IoT Core 的端点
    val clientId = UUID.randomUUID().toString()
    // 配置凭证
    val sessionCredentials = BasicSessionCredentials(
        accessKey,
        secretKey,
        sessionToken
    )

    // 初始化 MQTT Manager
    mqttManager = AWSIotMqttManager(clientId, endpoint)
    mqttManager?.keepAlive = 10
    mqttManager?.resetReconnect();
    mqttManager?.isAutoReconnect=true
    mqttManager?.isOfflinePublishQueueEnabled = false   
    mqttManager?.connect(StaticCredentialsProvider(sessionCredentials)) { status, throwable ->
        when (status) {
            AWSIotMqttClientStatus.Connecting -> {
                println("mqtt Connecting to AWS IoT")
            }

            AWSIotMqttClientStatus.Connected -> {
                println("mqtt Connected to AWS IoT")
            }

            AWSIotMqttClientStatus.ConnectionLost -> {
                println("mqtt Connection lost: ${throwable?.message}")
            }

            AWSIotMqttClientStatus.Reconnecting -> {
                println("mqtt Reconnecting to AWS IoT")                 
            }

            else -> {
                println("mqtt Connection status: $status")
            }
        }
    }

`

build.gradle is
`

    implementation 'com.amazonaws:aws-android-sdk-core:2.77.1' // 确保与其他模块版本一致
    implementation 'com.amazonaws:aws-android-sdk-apigateway-core:2.77.1'
    implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.77.1'
    implementation 'com.amazonaws:aws-android-sdk-iot:2.77.1'

`

No response

SDK version used

targetsdk android 34,minsdk 26

Environment details (OS name and version, etc.)

android studio

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionGeneral question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions