Android Asynctask Sending Udp Packet
I want to use asynctask to send a datagram packet, but it not work!! I have add all permission that may need! I think that the problem may be in the asynctask of socket.send(
Solution 1:
Yes, well you need to actually allocate the socket, the exception is a null pointer because you declared the socket but did not allocate it before using it. Now, this should work, but you will be constantly allocating a new socket. You should take the line: socket = new DatagramSocket() that I inserted below and put that in the onCreate most likely.
protected Boolean doInBackground(String... arg0) {
byte [] ip_bytes = new byte[] {(byte)192,(byte)168,(byte)1,(byte)1};
try {
inet_addr = InetAddress.getByAddress(ip_bytes );
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("AT command: ","at_cmd)");
byte[] buffer = (at_cmd + "\r").getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet_addr, PORT);
try {
socket= new DatagramSocket();
socket.send(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("///send at command","at command topic");
return null;
}
Post a Comment for "Android Asynctask Sending Udp Packet"