In this Community you can find examples with various UDP extensions.
-
Voice chat over UDP:
https://community.appinventor.mit.edu/t/extension-udp-audio-chat-voice-wifi-send-sound-over-udp/72958 -
Send text using UDP with the @Ulrich_Bien extension:
https://community.appinventor.mit.edu/t/esp32-with-udp-send-receive-text-chat-mobile-mobile-udp-testing-extension-udp-by-ullis-ulrich-bien/72664 -
Let's create a simple UDP extension, here is the source code...
KIO4_UDP.java (deprecated code, see updated code in post #12)
Summary
package com.KIO4_UDP;
// Juan Antonio Villalpando
// http://kio4.com/appinventor/299K_extension_UDP.htm
// Diciembre 2022.
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
// Obtener IP
import java.net.SocketException;
import java.util.Enumeration;
import java.net.NetworkInterface;
import java.net.Inet4Address;
@DesignerComponent(version = 1,
description = "Send/Receive UDP. WiFi. GotText needs a Clock component." +
"Juan Antonio Villalpando - KIO4.COM ",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.INTERNET, android.permission.ACCESS_WIFI_STATE")
public class KIO4_UDP extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 1;
private ComponentContainer container;
public static final String DEFAULT_GOT_TEXT = "";
private String got_text = "";
InetAddress toAddr;
int toPort2;
String text2;
int fromPort2;
int portToReceive2;
int sizePacket2;
DatagramSocket dsocket;
public KIO4_UDP(ComponentContainer container) {
super(container.$form());
this.container = container;
GotText(DEFAULT_GOT_TEXT);
}
// Obtener el valor.
@SimpleProperty(category = PropertyCategory.BEHAVIOR)
public String GotText() {
return got_text;
}
// Establecer el valor
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING, defaultValue = KIO4_UDP.DEFAULT_GOT_TEXT)
@SimpleProperty(description = "This propertie changes with ToReceive block. It needs a Clock component.")
public void GotText(String nuevogotText) {
this.got_text = nuevogotText;
}
////////////////////// ENVIAR PAQUETE ////////////////////////////////////////////////////////////
@SimpleFunction(description = "Send Packet.")
public void SendPacket(String toIP, int toPort, String text, int fromPort) {
toPort2 = toPort;
text2 = text;
fromPort2 = fromPort;
try {
toAddr = InetAddress.getByName(toIP);
} catch (Exception ex) { }
Thread thread = new Thread(new Runnable(){
public void run() {
try {
DatagramSocket ds = new DatagramSocket(fromPort2);
DatagramPacket dp = new DatagramPacket(text2.getBytes(), text2.length(), toAddr, toPort2);
ds.send(dp);
ds.disconnect();
ds.close();
ds = null;
} catch (IOException e) { }
}
});
thread.start();
}
////////////////////// RECIBIR PAQUETE ////////////////////////////////////////////////////////////
@SimpleFunction(description = "Text is received in GotText propertie. Use GotText with a Clock component.")
public void ToReceive(int portToReceive, int sizePacket) {
portToReceive2 = portToReceive;
sizePacket2 = sizePacket;
Thread thread = new Thread(new Runnable(){
public void run() {
try {
dsocket = new DatagramSocket(portToReceive2);
byte[] buffer = new byte[sizePacket2];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
dsocket.receive(packet);
String aText = new String(buffer, 0, packet.getLength());
got_text = aText;
}
} catch (IOException e) { }
}
});
thread.start();
}
////////////////////////////////////////////////////////////////////////////////////////
@SimpleFunction(description = "Close to receive.")
public void CloseToReceive() {
//dsocket.disconnect();
dsocket.close();
}
////////////////////////////////////////////////////////////////////////////////////////
@SimpleFunction(description = "Get local IP.")
// http://www.java2s.com/example/java-api/java/net/networkinterface/getnetworkinterfaces-0-1.html
public String GetLocalIp() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {ex.printStackTrace(); }
return null;
}
}
- It is a very simple extension, it can be modified and improved.
- Let's see several examples...
You can download the extension and the example of...
http://kio4.com/appinventor/299K_extension_UDP.htm
- Designer.
- Blocks.