examples/iot-nirvana/client/src/main/java/com/google/cloud/demo/iot/nirvana/client/IotDevice.java
/** Create a Cloud IoT Core MQTT client and connect to the server. */
Create a Cloud IoT Core MQTT client and connect to the server.
Create a Cloud IoT Core MQTT client and connect to the server .
private MqttClient createAndConnectMqttClient() throws ClientException {
    try {
      MqttConnectOptions connectOptions = new MqttConnectOptions();
      connectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
      connectOptions.setUserName("notused");
      connectOptions.setPassword(createJwtRsa().toCharArray());
      MqttClient client =
          new MqttClient(generateMqttServerAddress(), getDevicePath(), new MemoryPersistence());
      client.connect(connectOptions);
      return client;
    } catch (MqttException e) {
      String message =
          String.format("Failed to connect to the IoT server. Cause: %s", e.getMessage());
      LogUtils.logDebug(LOGGER, getDeviceId(), getGcpProjectId(), message);
      throw new ClientException(message, e);
    }
  }
private MqttClient createAndConnectMqttClient ( ) throws ClientException { try { MqttConnectOptions connectOptions = new MqttConnectOptions ( ) ; connectOptions . setMqttVersion ( MqttConnectOptions . MQTT_VERSION_3_1_1 ) ; connectOptions . setUserName ( "notused" ) ; connectOptions . setPassword ( createJwtRsa ( ) . toCharArray ( ) ) ; MqttClient client = new MqttClient ( generateMqttServerAddress ( ) , getDevicePath ( ) , new MemoryPersistence ( ) ) ; client . connect ( connectOptions ) ; return client ; } catch ( MqttException e ) { String message = String . format ( "Failed to connect to the IoT server. Cause: %s" , e . getMessage ( ) ) ; LogUtils . logDebug ( LOGGER , getDeviceId ( ) , getGcpProjectId ( ) , message ) ; throw new ClientException ( message , e ) ; } }
Create a Cloud IoT Core MQTT client and connect to the server.
Create a Cloud IoT Core MQTT client and connect to the server .
// Cloud IoT core supports only MQTT 3.1.1 // Create a client, and connect to the Google MQTT bridge with the defined options