Azure IoT中心MQTT失败没有SDK

问题描述

我正在尝试使用Microsoft Azure IOT Hub(MQTT)将Java客户端设备连接到IOT Hub(不带SDK)。我根据需要使用TTL创建了设备和SAS。我正在使用Paho客户端连接到IoT中心。我遇到了以下错误。我从IoT中心工具创建的SAS令牌有效期为5小时。 无权连接(5个) 在org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28) 在org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1040) 在org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151) 在java.lang.Thread.run(未知来源)

下面是我正在尝试的代码

String deviceId = "mqqdirectdevice1";     
          String brokerUri = "ssl://xxxxx:8883";
          String clientId = deviceId;
          System.out.println( "Connecting to " + brokerUri +" as "+clientId);

          MqttAsyncClient client = null;
          try {           
              String sasToken="SharedAccessSignature sr=xxxxxxx.azure-devices.net%2Fdevices%2Fmqqdirectdevice1&sig=iJ3jacTNMdyyhIrQueBN5X6uEqURYCKfa5Z63ePKDRs%3D&se=1600335545";
              client = new MqttAsyncClient( brokerUri,clientId,new MemoryPersistence());
              
            if ( client != null ) {             
              MqttConnectOptions options = new MqttConnectOptions();                          
              client.setCallback( new AzureCallback() );             
              options.setUserName("xxxxxx.net/mqqdirectdevice1?api-version=2018-06-30");
              options.setPassword(sasToken.toCharArray());
             // options.setPassword(sasToken.toCharArray());
              options.setKeepAliveInterval(230);
              options.setCleanSession(false);
              options.setMqttVersion(4);              
              IMqttToken token=client.connect( options );
              token.waitForCompletion(60 * 1000);
              System.out.println("Sent MQTT CONNECT packet was acknowledged");
              if ( client.isConnected() ) {
                System.out.println( "Success!" );
              } else {
                System.out.println( "Could not connect to Azure IoT hub,timed-out" );
              }
            }
          } catch ( MqttException) {
            //client.getDebug().dumpBaseDebug();
            e.printStackTrace();
          } finally {
            if ( client != null ) {
              try {
                client.disconnect();
              } catch ( MqttException ignore ) {}
            }
          }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)