Azure / azure-iot-sdk-java

A Java SDK for connecting devices to Microsoft Azure IoT services
https://azure.github.io/azure-iot-sdk-java/
Other
200 stars 237 forks source link

Assistance Required in Connecting Robot to IoT Central with Specific Project Settings #1735

Closed AlanPintor-git closed 1 year ago

AlanPintor-git commented 1 year ago

Dear IoT Team,

I am encountering challenges while attempting to integrate a robot with IoT Central. This is a critical issue as it pertains to one of many units that require connection.

Project Configuration Details:

The challenges faced are as follows:

Would you be able to provide a sample compatible with my project configuration, or advise me on appropriate solutions?

Enclosed code snippet:

import android.util.Log;
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.transport.IotHubConnectionStatus;
import org.json.JSONObject;

public class AzureIoTHelper {
    private DeviceClient deviceClient;

    public AzureIoTHelper(String connectionString) {
        try {
            deviceClient = new DeviceClient(connectionString, IotHubClientProtocol.HTTPS);
            deviceClient.registerConnectionStatusChangeCallback(new IotHubConnectionStatusChangeCallback() {
                @Override
                public void execute(IotHubConnectionStatus status, IotHubConnectionStatusChangeReason statusChangeReason, Throwable throwable, Object callbackContext) {
                    Log.v("IoTest", "Connection status changed to: " + status);
                }
            }, null);
        } catch (Exception e) {
            Log.v("IoTest", "Error initializing Azure IoT Central Helper: " + e.getMessage());
        }
    }

    public void iotConnect() {
        try {
            deviceClient.open();
            Log.v("IoTest", "Connected to Azure IoT Central.");
        } catch (Exception e) {
            Log.v("IoTest", "Error connecting to Azure IoT Central: " + e.getMessage());
        }
    }

    public void sendTelemetries(JSONObject telemetryData) {
        try {
            Message message = new Message(telemetryData.toString());
            deviceClient.sendEventAsync(message, new EventCallback(), null);
            Log.v("IoTest", "Telemetry sent: " + telemetryData.toString());
        } catch (Exception e) {
            Log.v("IoTest", "sendTelemetries - Exception: " + e.getMessage() + ", cause: " + e.getCause());
            e.printStackTrace();
            //V/IoTest: Telemetry sent with status: ERROR
        }
    }

    private static class EventCallback implements IotHubEventCallback {
        @Override
        public void execute(IotHubStatusCode responseStatus, Object callbackContext) {
            Log.v("IoTest", "Telemetry sent with status: " + responseStatus.name());
        }
    }
}

Best Regards, Alan

timtay-microsoft commented 1 year ago

Can you share the stacktrace that you see when sending telemetry fails?

AlanPintor-git commented 1 year ago

Hi @timtay-microsoft, thanks for the following.

The Error is coming from IotHubEventCallback:

private static class EventCallback implements IotHubEventCallback {
    @Override
    public void execute(IotHubStatusCode responseStatus, Object callbackContext) {

        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        StringBuilder stackTraceString = new StringBuilder();
        for (StackTraceElement element : stackTraceElements) {
            stackTraceString.append(element.toString()).append("\n");
        }

        Log.v("IoTest", "IotHubEventCallback >> Telemetry sent with status: " + responseStatus.name());
        Log.v("IoTest", "Current stack trace:\n" + stackTraceString);
    }
}

Error: image

timtay-microsoft commented 1 year ago

Do you see any errors when using other protocols? Such as:

deviceClient = new DeviceClient(connectionString, IotHubClientProtocol.AMQPS);

or

deviceClient = new DeviceClient(connectionString, IotHubClientProtocol.MQTT);
AlanPintor-git commented 1 year ago

Hi @timtay-microsoft, thanks for the follow-up.

Yes, the error is the same for all protocols except HTTPS. The error sample below is from the MQTT protocol:

V/IoTest: Error connecting to Azure IoT Central: Could not open the connection W/System.err: java.io.IOException: Could not open the connection at com.microsoft.azure.sdk.iot.device.DeviceIO.open(DeviceIO.java:162) at com.microsoft.azure.sdk.iot.device.InternalClient.open(InternalClient.java:176) at com.microsoft.azure.sdk.iot.device.DeviceClient.open(DeviceClient.java:438) at com.osak.csjrobotmanager.Services.AzureIoTHelper.iotConnect(AzureIoTHelper.java:40) W/System.err: at com.osak.csjrobotmanager.Activity.MainActivity.onCreate(MainActivity.java:171) at android.app.Activity.performCreate(Activity.java:6709) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6141) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803) W/System.err: Caused by: com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException: Unable to establish MQTT connection at com.microsoft.azure.sdk.iot.device.transport.mqtt.exceptions.PahoExceptionTranslator.convertToMqttException(PahoExceptionTranslator.java:46) at com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt.connect(Mqtt.java:136) at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging.start(MqttMessaging.java:63) at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection.open(MqttIotHubConnection.java:277) at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.openConnection(IotHubTransport.java:999) at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.open(IotHubTransport.java:392) at com.microsoft.azure.sdk.iot.device.DeviceIO.open(DeviceIO.java:158) ... 16 more W/System.err: Caused by: MqttException (0) - javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. W/System.err: at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38) at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:736) at java.lang.Thread.run(Thread.java:761) Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:361) at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:149) at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:722) ... 1 more Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:549) W/System.err: at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:508) at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:401) at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:375) at com.android.org.conscrypt.TrustManagerImpl.getTrustedChainForServer(TrustManagerImpl.java:304) at android.security.net.config.NetworkSecurityTrustManager.checkServerTrusted(NetworkSecurityTrustManager.java:94) at android.security.net.config.RootTrustManager.checkServerTrusted(RootTrustManager.java:88) at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:178) at com.android.org.conscrypt.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:596) at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357) ... 3 more Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. ... 14 more V/IoTest: sendTelemetries - Exception: Cannot send event from an IoT Hub client that is closed., cause: null, stackTraceString: java.lang.IllegalStateException: Cannot send event from an IoT Hub client that is closed. at com.microsoft.azure.sdk.iot.device.DeviceIO.sendEventAsync(DeviceIO.java:322) at com.microsoft.azure.sdk.iot.device.InternalClient.sendEventAsync(InternalClient.java:216) at com.osak.csjrobotmanager.Services.AzureIoTHelper.sendTelemetries(AzureIoTHelper.java:62) at com.osak.csjrobotmanager.Activity.MainActivity.onCreate(MainActivity.java:177) at android.app.Activity.performCreate(Activity.java:6709) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6141) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803) W/System.err: java.lang.IllegalStateException: Cannot send event from an IoT Hub client that is closed. at com.microsoft.azure.sdk.iot.device.DeviceIO.sendEventAsync(DeviceIO.java:322) W/System.err: at com.microsoft.azure.sdk.iot.device.InternalClient.sendEventAsync(InternalClient.java:216) at com.osak.csjrobotmanager.Services.AzureIoTHelper.sendTelemetries(AzureIoTHelper.java:62) at com.osak.csjrobotmanager.Activity.MainActivity.onCreate(MainActivity.java:177) at android.app.Activity.performCreate(Activity.java:6709) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6141) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)

timtay-microsoft commented 1 year ago

Have you tried upgrading the SDK version to either the latest v1 release (1.34.3) or to the latest v2 release (2.2.0)?

AlanPintor-git commented 1 year ago

Yes, I am experiencing issues with Android compatibility. However, I will attempt again using the specific version(s) you recommended. I will keep you updated. Thank you.

AlanPintor-git commented 1 year ago

Hello @timtay-microsoft,

I keep encountering the same Android compatibility issues. Initially, the IDE prompted me to upgrade, but after resolving many issues, it's now suggesting that I downgrade. I feel like I'm going in circles. Can you provide a compatible sample for my project or recommend effective solutions? Thanks in advance,

timtay-microsoft commented 1 year ago

Closing as stale

AlanPintor-git commented 9 months ago

Dear Azure Team,

I am reaching out to reopen a previously raised issue regarding the Azure SDK upgrade for our Android application. Despite earlier efforts, we continue to face challenges in this process.

Our main difficulty lies in the absence of detailed documentation. The available "Quickstart" guide only offers an APK, which unfortunately does not meet our needs as it lacks the source code.

To facilitate a smoother upgrade process, could you kindly provide us with the Android source code or a simple demonstration? Specifically, we are looking for a way to send telemetry data to Azure IoT Central from an Android device.

Your assistance in providing these resources would be highly valuable in helping us resolve the issues we are encountering.

Thank you for your attention and support.