0

I got this error while integration with Mqtt this party trying to handle connection

java.lang.IllegalArgumentException:Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.

fun connect(
    username: String,
    password: String,
    cbConnect: IMqttActionListener,
    cbClient: MqttCallback
) {
    mqttClient.setCallback(cbClient)
    val options = MqttConnectOptions()
    options.userName = username
    options.password = password.toCharArray()
    
    try {
        mqttClient.connect(options, null, cbConnect)
    } catch (e: MqttException) {
        e.printStackTrace()
    }
}


fun startMQTTConnection() {
    mqttClient.connect(
        username,
        password,
        object : IMqttActionListener {
            override fun onSuccess(asyncActionToken: IMqttToken?) {
                Toast.makeText(context, "MQTT Connection success", Toast.LENGTH_SHORT).show()
            }
            
            override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
              Toast.makeText(
                    context, "MQTT Connection fails: ${exception.toString()}",
                    Toast.LENGTH_SHORT
                ).show()
            }
        },
        object : MqttCallback {
            override fun messageArrived(topic: String?, message: MqttMessage?) {
                val msg = "Receive message: ${message.toString()} from topic: $topic"
               Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
            }
            
            override fun connectionLost(cause: Throwable?) {
                Log.d(this.javaClass.name, "Connection lost ${cause.toString()}")
            }
            
            override fun deliveryComplete(token: IMqttDeliveryToken?) {
                Log.d(this.javaClass.name, "Delivery complete")
            }
        })
}
hardillb
  • 54,545
  • 11
  • 67
  • 105
rou
  • 1
  • Does this answer your question? [Targeting S+ (version 31 and above) requires that one of FLAG\_IMMUTABLE or FLAG\_MUTABLE be specified](https://stackoverflow.com/questions/70894168/targeting-s-version-31-and-above-requires-that-one-of-flag-immutable-or-flag) – user2357113 Apr 11 '23 at 08:13
  • @mujammil no I already added this but doesn't handle the issue – rou Apr 11 '23 at 08:19
  • Make sure you are on the latest version of your MQTT client library. If you are, and you are still getting this error, contact the developers of that library. – CommonsWare Apr 11 '23 at 11:19

0 Answers0