sample-lowpan
sample-lowpan copied to clipboard
Issues with UDP Sockets
There is pretty much no error logs but it can't actually send anything over mesh network on udp. Seems to receive without any issues but on send it doesn't go.
try { sock = new DatagramSocket(null); udpPacket = new DatagramPacket(data.getBytes(), data.length(), InetAddress.getByName(deviceAddr), portServer);
sock.send(udpPacket);
Log.e(TAG, "Connection send: " + data);
if (!sock.isClosed()) {
sock.close();
Log.e(TAG, "Socket Closed");
}
} catch (SocketException e) {
Log.e(TAG, "Socket Issue: " + e.getMessage());
} catch (UnknownHostException e) {
Log.e(TAG, "Host Issue: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IO Issue: " + e.getMessage());
}
thats the code for send over udp. Here is the addr: fdde:ad00:beef:0:8142:58d9:a2a3:7377 port is 1212 and on receive there are no trouble.
Any thoughts?
Hi, We have the same problem. We are developing a use case using a nRF52840 Dongle and Android Thinkgs connected to a nRF52840DK. The dongle is able to send messages over UDP and Android Things correctly receive them, but cannot send anything back. No error is raised. Did you find some more informations regarding this issue or, maybe, you resolved it?
Best regards Giorgio
Nope nothing back. The error we have see on the device is that the network is not found. Unreachable and thats pretty much all we get. We have tried quite a lot to test it. And we have nowhere to go. Please if you find something share.
Thanks!
Hi, We achieved a solution for the problem. I'll post the code that is working for us: In our example, we receive a message and send back an answer
var running = true
var buf : ByteArray
var socket : DatagramSocket = DatagramSocket(_1234_) //Specify the port, not NULL
socket.broadcast = true //Needed to send broadcast messages
// FOR UNICAST ON WPAN1 LowpanNetwork is retrieved using the ConnectivityManager
lowpanNetwork.bindSocket(socket)
var packet : DatagramPacket
while (running) {
buf = ByteArray(80)
packet = DatagramPacket(buf, buf.size)
socket.receive(packet)
var received = String(packet.getData(), 0, packet.data.size).replace(0.toChar().toString(), "")
var address = packet.address.toString()
address = address.substring(1) //To remove initial '/'
buf = "ciao topogigio".toByteArray()
packet = DatagramPacket(buf, buf.size,_Inet6Address.getByName(address),1234_)
socket.send(packet)
}
socket.close()
@TavecchiaG Do you think that you can send without the receive? Can you try to do that. We have a full receiver working without sending. The solution above works but not in my case i need the Android Device to perform a send without receive.
We tried to send without receive and it works, you need to specify IP address and port. Then remember to perform a Bind in the receiver device, in our case the nRF52840 Dongle. Without Bind, the Dongle is not able to receive anything! Best regards, Giorgio
@TavecchiaG Can you post the code that you use in the Android Device to be able to send? Without the receive? We have been trying for a while and no result.
Thanks in advance.
var running = true
var buf : ByteArray
var counter = 0
Log.d("THREAD_CONNECTION","PARTE` IL SEND")
var socket : DatagramSocket = DatagramSocket(1234) //Our port
socket.broadcast = true
lowpanNetwork.bindSocket(socket) // FOR UNICAST ON WPAN1
var packet : DatagramPacket
while (running) {
buf = ByteArray(80)
counter ++;
//FOR UNICAST
var address = "fd00:400:2099:0:5e01:e31:7927:71d5" //Our nRF52840Dongle address
buf = "ciao topogigio".toByteArray()
packet = DatagramPacket(buf, buf.size,Inet6Address.getByName(address),1234)
if(counter<20) {
socket.send(packet)
Log.d("THREAD_CONNECTION", "messaggio inviato")
}else running = false
}
socket.close()
This is our code, supposing that the 2 devices are connected on the same Thread Network. If you use the CLI example for the nRF52840 dongle you can do this.
- udp open
- udp bind :: 1234
Then you will be able to receive the messages from Android Things
@TavecchiaG Did you guys try to send between two devices on Android? But if you are receiving on the nordic device you should be able to get it on the other one. As far as i understand. I was doing the same thing but between the two Android devices doesn't seem to go true.
I'm sorry but we don't have 2 android device so we are unable to try that. Hope you'll find a solution soon! Best regards Giorgio
@TavecchiaG Thanks for all the help provided! I will try to find a solution and post it on the topic as soon as i can find it.
EDIT: I have found a solution that works for me. Its poorly designed the whole API. And on top of that the implementation of the UDP Sockets on Android is not the best. But it works fine.
Kind Regards, Niki
Issue can be closed.