I try to send a UDP packet from my mobile to a PC – IP 192.168.1.113, at the PC I use “Packet sender”with UDP server port 55777. But it does not work, I have sent UDP packet from a VBA script in Excel to the Packet sender and it works, I can also send through UDP apps I have downloaded to the mobile, to the “Packet sender”.
I use:
Android Studio 3.5
SDK platform 8.1
Mobile: Samsung S9+
I have added the INTERNET permission to the manifest:
I send the packet from a new thread since the main thread does not allow to send packages, the below code is the first step before I continue, need to make this to work first though.
public class MainActivity extends AppCompatActivity {
private Handler mainHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startThread(View view) {
ExampleRunnable runnable = new ExampleRunnable(10);
new Thread(runnable).start();
}
class ExampleRunnable implements Runnable {
@Override
public void run() {
DatagramSocket ds = null;
String message = "Hello";
try{
ds = new DatagramSocket();
InetAddress serverAddr = InetAddress.getByName("192.168.1.113");
DatagramPacket dp;
dp = new DatagramPacket(message.getBytes(), message.length(), serverAddr, 55777);
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
}
}