Englisch version   Deutsche Version


Version Anpassungen
1 (2018-01-27) Initial version
2 (2018-12-08) Since December 2018 there had been problems with network accesses. These accesses must now be executed in a separate thread. The extension had been rewritten accordingly.
In addition, the property InitErr was introduced, which can be used to retrieve errors during initialization of the component.
3 (2019-01-11) Unfortunately, version 2 had a few problems. Version 3 is restructured. Unfortunately, it is not compatible with the previous version. For this reason, please observe the migration instructions listed below for updating existing App Inventor projects.
The old version 2 is archived here.

Motivation

For a project, an Android app should be developed that communicates with an ESP8266 (project). For easy development of the app MIT App Inventor 2 was choosen.

The IP addresses of the available ESP8266 devices should not be fixed in the project. The app should itself determine which devices are currently active and which are the addresses they can be addressed from.

To accomplish this task (name service), you can use the broadcast function of UDP. You simply send a broadcast datagram to a previously agreed port to all devices on the local network, requesting their connection data. The devices that are listening on the agreed port then return their IP and possibly further data to the sender. The sender collects the answers and thus knows all active devices. Because UDP packet delivery is not guaranteed, it's a good idea to repeat this process and use the union set of responses.

Um gezielt die Geräte eines Projekts ansprechen zu können, vereinbart man entweder unterschiedliche Ports oder man gibt eine Geräte-Kennung in der Antwort mit. Der Empfänger filtert dann diejenigen Geräte heraus, die ihn interessieren.

In order to be able to address the devices of a project in a targeted manner, you either agree on different ports or you enter a device identifier in the response. The receiver then filters out those devices it is interested in.

The problem is: App Inventor has no built-in UDP and I have found no working extension. So do it yourself.

Download

The ZIP archive UrsAI2UDPv3 for download. The archive contains the source code, the compiled binary to upload to App Inventor and a sample application.

Migrating from version 2 to version 3

The main differences are:

  • The Xmit and StartListening methods are now functions that return a value indicating whether the method ran successfully.
  • The events XmitError and RcvError are omitted. During App development it was difficult to associate the events with the corresponding action.
  • The names of some functions and function parameters have been adapted to the common names.

The best way to migrate an existing project is as follows:

  • Save the existing project ("Save project as ..." in App Inventors project menu).
  • Import the new version of the extension (in addition to the old one!).
  • In App Inventor Designer: Drag a new instance of the extension onto the screen.
  • In the block editor, transfer the affected code blocks to the new extension block. Pay attention to warnings and errors!
  • It is best to do another backup at this point.
  • In the Designer: Delete the instance of the old extension version and than the outdated extension.
  • In the block editor: check that everything is still correct.

Usage

Transmit datagrams

Block Function Notes
Xmit The Xmit block sends datagrams to the specified remote endpoint (RemoteIP: RemotePort). The local endpoint 0.0.0.0:LocalPort is used, i.e. the service provider selects the appropriate network interface.

The parameter RemoteIP can specify unicast or broadcast addresses.

The parameter LocalPort is also allowed to specify 0 or negative values. In this case, the service provider will search for a random free port.

The return code is:
Value Meaning
0 Xmit successful.
1 RemoteIP can not be converted to an IPv4 address.
2 No socket can be created with the specification of LocalPort. Port number is not free or > 65535.
3 Xmit not successful.
An Android device usually has only one active network interface at a time. This is either the mobile data connection or the WLAN.


For UDP address schemes, see UdpClient: Aufs Bit geschaut / Adressierungsschemata (there is a link to a translator).

To endpoints in Java UdpClient: Aufs Bit geschaut / Java (there is a link to a translator).



This block shows a typical application:
Xmit datagrams
Last Error The LastErrorMessage property can be used to retrieve an English text describing the last error.  

 

Receive datagrams

Block Function Note
StartListening  StartListening starts listening on datagrams sent to the specified port (UDP server).

Return codes:
Value Meaning
0 successful.
1 Port can not be used to create a socket. Port number is not free, < 1 oder > 65535.
2 StartListening before previous StartListening has been completed.

StopListening stops the server. A multiple call to StopListening is not critical.
To endpoints in Java UdpClient: Aufs Bit geschaut / Java (there is a link to a translator).


This block shows a typical application:
v
Last Error The LastErrorMessage property can be used to retrieve an English text describing the last error.  
Server Start/Stop There are corresponding events that are fired when the server was successfully started or stopped. LocalIP and LocalPort is the local endpoint.  
IsRunning The property isRunning can be used to query whether the server is currently active.  
Receive datagrams When data is received, the DataReceived event is fired. Data has the datagram data, RemoteIP and RemotePort is the senders address.  
DropSentToYourself  The property DropSentToYourself controls the behavior of receiving broadcast datagrams. By default, the block is set to ignore messages sent from its own IP. If these are still to be received, DropSentToYourself should be set to false.

This property can also be set in the Designer.
The default is true.

Miscellaneous

Block Function Note
LocalHost LocalHost retrieves the local IP address (in the Android environment this is usually unique). If no network can be reached, the value specified with Default is returned. This block shows a typical application:
LocalHost

Example

Screenshot   Designer

The blocks in the example are not difficult to understand. Most blocks concern the validation of entries.

Tools

For developing own extensions I gathered some tips: AI2 FAQ: Develop Extensions.

The extensions are developed with Java. A tutorial on datagrams can be found in the Oracle Java documentation. There you will also find information about the DatagramSocket class used.