【问题标题】:Failed with exception Connection lost (32109) when an Android app subscribes events from IBM IoT platform当 Android 应用程序从 IBM IoT 平台订阅事件时失败并出现异常连接丢失 (32109)
【发布时间】:2018-01-02 15:04:41
【问题描述】:

我正在尝试从 IBM IoT 平台订阅事件。

我测试的示例来自 IBM 博客:https://www.ibm.com/developerworks/library/iot-mobile-phone-iot-device-bluemix-apps-trs/。在这个链接中,可以下载代码。

通过此示例,Android 应用程序可以成功连接并将事件发布到 IBM IoT 平台,但是当我尝试“订阅”时,总是收到错误“连接丢失”。是什么导致了这个问题?谢谢!

相关代码为:

public IMqttToken subscribeToEvent(String deviceType, String deviceId, String event, String format, int qos, Object userContext, IMqttActionListener listener) throws MqttException {
    String eventTopic = "iot-2/type/" + deviceType + "/id/" + deviceId + "/evt/" + event + "/fmt/"+format;
    return subscribe(eventTopic, qos, userContext, listener);
}

public static void subscribeEvent(Context context,String event) {
    Log.v(TAG, ".subscribeEvent() entered")

    try {
        MyIoTActionListener listener = new MyIoTActionListener(context, Constants.ActionStateStatus.SUBSCRIBE);
        IoTClient iotClient = IoTClient.getInstance(context);
        String deviceType = "Android";
        String deviceId = "...";
        iotClient.subscribeToEvent(deviceType, deviceId, event, "json", 0, context,listener);

    } catch (MqttException e) {
        Log.d(TAG, ".SubscribeEvent() received exception on SubscribeEvent()");
    }
}

连接网址是:

String connectionURI = "tcp://" + this.getOrganization() + ".messaging.internetofthings.ibmcloud.com:1883";

ssl 无法正常工作。

【问题讨论】:

  • 您如何进行身份验证?您是否使用创建设备 ID 时生成的令牌?如果您订阅主题字符串 eventTopic = "iot-2/type/" + deviceType + "/id/" + deviceId + "/evt/" + event + "/fmt/"+format;不允许并且连接已关闭。尝试生成 API 密钥和令牌,并使用它们进行身份验证和订阅。
  • 谢谢伊丹!现在我已经解决了这个问题。设备客户端只能发布其事件和订阅命令。应用程序客户端可以发布和订阅事件和命令。当我尝试使用设备客户端订阅事件时,显然失败了。
  • 是的,你是对的,这就是为什么我问身份验证是如何完成的。很高兴它现在可以工作了。

标签: android ibm-cloud mqtt watson-iot


【解决方案1】:

这个问题已经解决了。

需要澄清两个概念:设备客户端和应用程序客户端。

设备客户端只能发布事件和订阅命令。在问题中,我使用设备客户端订阅事件,物联网平台无法响应并返回错误“连接丢失”。正确的编码方式是:

    public IMqttToken subscribeToCommand(String command, String format, int qos, Object userContext, IMqttActionListener listener) throws MqttException {
        String commandTopic = getCommandTopic(command, format);
        return subscribe(commandTopic, qos, userContext, listener);
    }

public static String getCommandTopic(String command, String format) {
        return "iot-2/cmd/" + command + "/fmt/json";
    }

public static void subscribeCommand(Context context, String command) {
        try {
            MyIoTActionListener listener = new MyIoTActionListener(context, Constants.ActionStateStatus.SUBSCRIBE);
            IoTClient iotClient = IoTClient.getInstance(context);

        iotClient.subscribeToCommand(command, "json", 0, context,listener);
    } catch (MqttException e) {
        Log.d(TAG, ".SubscribeCommand() received exception on SubscribeEvent()");
    }
}

IBM 提供的示例包含设备客户端的 subscribeEvent 和 publishCommand 等函数。这些功能让我感到困惑。

如果要发布命令和订阅事件,则需要创建应用程序客户端。创建应用客户端的方法是:

import com.ibm.iotf.client.app.ApplicationClient;

Properties appProperties = new Properties();
appProperties.put("id",applicationId);
appProperties.put("Organization-ID",organizationId);
appProperties.put("Authentication-Method","apikey");
appProperties.put("API-Key",apiKey);
appProperties.put("Authentication-Token",token);

appClient = new ApplicationClient(appProperties);

appClient.connect();

https://github.com/ibm-watson-iot/iot-java/blob/master/src/main/java/com/ibm/iotf/client/app/ApplicationClient.java查看更多关于应用程序客户端的信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多