【问题标题】:Mqtt connection failed with exception 'Not authorized to connect (5)' androidMqtt 连接失败,出现异常“未授权连接 (5)”android
【发布时间】:2017-07-07 01:16:51
【问题描述】:

您好,我正在尝试使用我刚刚注册的 android 设备连接到 mqtt,但我一直收到错误消息

Not authorized to connect (5)

这是我的一些代码

        client = new MqttAndroidClient(context, connectionURI, clientID);
        client.setCallback(callbacks);

        String username = IOT_DEVICE_USERNAME;
        char[] password = this.getAuthorizationToken().toCharArray();

        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        options.setUserName(username);
        options.setPassword(password);

        Log.d(TAG, "Connecting to server: " + connectionURI);
        try {
            // connect
            return client.connect(options, context, listener);
        } catch (MqttException e) {
            Log.e(TAG, "Exception caught while attempting to connect to server", e.getCause());
            throw e;
        }

其中 IOT_DEVICE_USERNAME 是字符串 "use-token-auth" connectionUri"tcp://.messaging.internetofthings.ibmcloud.com:8883"

有趣的是,就在这个错误之前,我得到了这个日志

Unregister alarmreceiver to MqttServiced:<"organisation">:<"DeviceType">:<"deviceId">

这里是错误的stackTrace

07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err: Not authorized to connect (5)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:988)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:140)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.lang.Thread.run(Thread.java:818)

【问题讨论】:

  • 嗨,Michael,如果你知道 6 字符的组织结构,我会检查日志中是否有任何可能有助于解决问题的错误。
  • 另外,您是否遵循任何食谱或教程?例如,这个秘籍展示了如何使用设备类型和 ID 设置客户端 ID。即,字符串 iotClientId = "d:"+ORG+":"+DEVICE_TYPE+":"+DEVICE_ID; developer.ibm.com/recipes/tutorials/android-wear-iot-bluemix
  • @ValerieLampkin 4rxa4d 是该组织。应该有一些其他设备类型可以顺利连接,但安卓设备有问题
  • @ValerieLampkin 我目前指的是 github.com/ibm-watson-iot/iot-starter-for-android> 我会看看上面的食谱。如果你发现什么,请告诉我。谢谢。

标签: java android mqtt watson-iot


【解决方案1】:

我发现了这个错误:

令牌验证失败(在数据库中找不到设备/网关):ClientID='d:4rxa4d:AndroidDevice:1c39476b8dd0'

您是否已在 Watson IoT Platform 中为 AndroidDevice 创建了一个设备类型并将 ID 1c39476b8dd0 注册到您的组织?

这个tutorial 展示了如何注册设备。

【讨论】:

  • 有趣的是,我确实已经在 Watson IoT 上注册了该设备。是不是找不到设备?你会推荐什么?也许删除设备并重新注册?
  • 我更新了添加错误堆栈跟踪的问题,如果这有助于理解发生了什么。
  • 嗨迈克尔,我看到你发现它被注册为网关而不是设备,现在已经相应地更新了连接信息。很高兴听到它现在正在工作。
【解决方案2】:

问题已解决。我确实使用它作为客户端 ID 作为 MqttAndroidClient 对象的构造函数的输入。

String iotClientId = "d:"+ORG+":"+DEVICE_TYPE+":"+DEVICE_ID;

但是,由于我将 Android 设备注册为网关,因为它实际上连接到传感器实际所在的多个外围设备。为了解决这个问题,我将上面的行改为

String iotClientId = "g:"+ORG+":"+DEVICE_TYPE+":"+DEVICE_ID;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多