투케이2K

947. (Android/Java) [간단 소스] AWSIotMqttManager AWS MQTT User Properties 사용자 정의 속성 추가 및 확인 방법 본문

Android

947. (Android/Java) [간단 소스] AWSIotMqttManager AWS MQTT User Properties 사용자 정의 속성 추가 및 확인 방법

투케이2K 2025. 2. 12. 19:02

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------

- 언어 : Java / Kotlin

- 개발 툴 : AndroidStudio

- 기술 구분 : AWSIotMqttManager / MQTT / User Properties

// --------------------------------------------------------------------------------------






// --------------------------------------------------------------------------------------
[사전) Aws MQTT User Properties 설명]
// --------------------------------------------------------------------------------------

1. Aws MQTT 를 사용하기 위한 AWSIotMqttManager 는 기본적으로 MQTT 3.1.1 을 사용합니다

2. User Properties 사용자 정의 속성은 MQTT 5.0 기능으로 AWSIotMqttManager 에서 직접적으로 기능을 지원하지 않지만, 
   헤더 처럼 JSON 데이터에 포함하여 활용할 수 있습니다

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[User Properties 설정 샘플 소스 코드] : publish
// --------------------------------------------------------------------------------------

// [JSONObject 객체 생성]
JSONObject payload = new JSONObject();
try {
    payload.put("transactionId", "123456");
    payload.put("userProperties", new JSONObject()
        .put("key1", "value1")
        .put("key2", "value2")
    );
} catch (JSONException e) {
    e.printStackTrace();
}
 
String topic = "twok/topic";
mqttManager.publishString(payload.toString(), topic, AWSIotMqttQos.QOS1);

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[User Properties 확인 샘플 소스 코드] : subscribe
// --------------------------------------------------------------------------------------

String topic = "twok/topic";
mqttManager.subscribeToTopic(topic, AWSIotMqttQos.QOS1, (topicName, data) -> {
    try {
        String message = new String(data, "UTF-8");
        JSONObject json = new JSONObject(message);
 
        // 트랜잭션 ID 확인
        String transactionId = json.optString("transactionId", "N/A");
 
        // User Properties 확인
        JSONObject userProperties = json.optJSONObject("userProperties");
        if (userProperties != null) {
            Iterator<String> keys = userProperties.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = userProperties.getString(key);
                Log.d("MQTT", "User Property - " + key + ": " + value);
            }
        } else {
            Log.d("MQTT", "User Properties 없음");
        }
 
    } catch (Exception e) {
        Log.e("MQTT", "메시지 처리 오류", e);
    }
});

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------

https://aws-amplify.github.io/aws-sdk-android/docs/reference/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.html

https://aws-amplify.github.io/aws-sdk-android/docs/reference/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.html#updateUserMetaData-java.util.Map-

// --------------------------------------------------------------------------------------
 
반응형
Comments