Mqtt连接失败,异常“未授权连接(5)”android

时间:2017-07-05 19:40:20

标签: java android mqtt watson-iot

您好我正在尝试使用我刚刚注册的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://<”organization“> .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)

2 个答案:

答案 0 :(得分:0)

我发现了这个错误:

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

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

tutorial显示了如何注册设备。

答案 1 :(得分:0)

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

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

然而,由于我将Android设备重新设置为网关,因为它实际上连接到传感器实际所在的多个外围设备。要解决这个问题,我上面的一行

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