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")
}
})
}