I wonder if you could help me out. I am trying to adapt your code so that it were a function to which I could pass my string, which is merely of the type “1, -6” or two ints with a comma in between. myTargetIP and myTargetPort are two global variables I have and can be edited in a second activity in my app.
This is how my function now looks:
fun sendUDP(messageStr: String) {
// Hack Prevent crash (sending should be done using an async task)
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
try {
//Open a port to send the package
val socket = DatagramSocket()
socket.broadcast = true
val sendData = messageStr.toByteArray()
val sendPacket = DatagramPacket(sendData, sendData.size, InetAddress.getByName(myTargetIP), myTargetPort)
socket.send(sendPacket)
} catch (e: IOException) {
// Log.e(FragmentActivity.TAG, "IOException: " + e.message)
}
}
But the issue is that the DatagramPacket gets underlined and then I get this:
None of the following functions can be called with the arguments supplied:
public constructor DatagramPacket(p0: ByteArray!, p1: Int, p2: InetAddress!, p3: Int) defined in java.net.DatagramPacket
public constructor DatagramPacket(p0: ByteArray!, p1: Int, p2: Int, p3: SocketAddress!) defined in java.net.DatagramPacket
I just can’t figger out what I am doing wrong, so please, if possible, point me to the solution.
Another issue is that if I try to use the SoftOptions class, my app crashes as I try to go to the Settings activity. If I remove the class, no problem. I wonder what that may result from?
Many thanks in advance!