Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private NativeMe As JavaObject
    Private xui As XUI
    
    Dim udppack As UDPPacket    'zmienna jako UDPPacket do wysyłania
    Dim udp As UDPSocket        'zmienna jako UDPSocket
    Dim MyLan As ServerSocket    'do komunikacji TCP
    
 End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Dim matip As String                'zmienna przechowująca adres IP
    Dim matport As Int                'zmienna przechowująca adres port
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    matip = "192.168.1.17"        'wpisanie ip do zmiennej
    matport = 8899                'wpisanie portu do zmiennej
    
    If FirstTime Then
        NativeMe.InitializeContext
        MyLan.Initialize( "8899", "lan" )                    'inicjalizacja server socket
        udp.Initialize( "udpsrv", matport, 1024 )    'inicjalizacja UDP    ("udpsrv" - do zdarzenia odbiorczego, "" - bez zdarzenai obiorczego)
    End If
    
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then    'jeśli aplikacja zminimalizowana to
        Activity.Finish        'zakończ aplikację
        ExitApplication        'wyłącz aplikację
    End If
End Sub
Sub Button1_Click
    'Qui voglio chiamare una funzione java per visualizzare un elenco di indirizzi IP.
    NativeMe.RunMethod("NetworkSniffTask", Null)
End Sub
#If JAVA
static class NetworkSniffTask extends AsyncTask<Void, Void, Void> {
  private static final String TAG = Constants.TAG + "nstask";
  private WeakReference<Context> mContextRef;
  public NetworkSniffTask(Context context) {
    mContextRef = new WeakReference<Context>(context);
  }
  @Override
  protected Void doInBackground(Void... voids) {
    Log.d(TAG, "Let's sniff the network");
    try {
      Context context = mContextRef.get();
      if (context != null) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo connectionInfo = wm.getConnectionInfo();
        int ipAddress = connectionInfo.getIpAddress();
        String ipString = Formatter.formatIpAddress(ipAddress);
        Log.d(TAG, "activeNetwork: " + String.valueOf(activeNetwork));
        Log.d(TAG, "ipString: " + String.valueOf(ipString));
        String prefix = ipString.substring(0, ipString.lastIndexOf(".") + 1);
        Log.d(TAG, "prefix: " + prefix);
        for (int i = 0; i < 255; i++) {
          String testIp = prefix + String.valueOf(i);
          InetAddress address = InetAddress.getByName(testIp);
          boolean reachable = address.isReachable(1000);
          String hostName = address.getCanonicalHostName();
          if (reachable)
            Log.i(TAG, "Host: " + String.valueOf(hostName) + "(" + String.valueOf(testIp) + ") is reachable!");
        }
      }
    } catch (Throwable t) {
      Log.e(TAG, "Well that's not good.", t);
    }
  return null;
}
#End If