#Region Project Attributes
#ApplicationLabel: Mcast Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
'i Ad this To Manifest
'AddPermission(android.permission.CHANGE_WIFI_MULTICAST_STATE)
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Dim MulticastLock As JavaObject
Dim udp As UDPSocket
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
AcquireMulticastLock
udp.Initialize("udp",5683,4096)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub udp_PacketArrived (Packet As UDPPacket)
Log(Packet.HostAddress)
End Sub
Sub AcquireMulticastLock
Try
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim WifiManager As JavaObject = ctxt.RunMethod("getSystemService", Array("wifi"))
MulticastLock = WifiManager.RunMethodJO("createMulticastLock", Array("b4a-udp"))
MulticastLock.RunMethod("acquire", Null)
Catch
Log(LastException)
End Try
End Sub
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private udp As UDPSocket
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
CreateMulticastSocket("udp", 1900, "239.255.255.250")
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Private Sub Button1_Click
xui.MsgboxAsync("Hello world!", "B4X")
End Sub
Sub CreateMulticastSocket (EventName As String, Port As Int, Group As String)
Dim multi As JavaObject
multi.InitializeNewInstance("java.net.MulticastSocket", Array(Port))
Dim inet As JavaObject
multi.RunMethod("joinGroup", Array(inet.InitializeStatic("java.net.InetAddress").RunMethod("getByName", Array(Group))))
Dim jo As JavaObject = Me
jo.RunMethod("SetMulticastSocket", Array(EventName, multi, udp))
End Sub
#if java
import java.net.*;
public void SetMulticastSocket (String EventName, MulticastSocket socket, anywheresoftware.b4a.objects.SocketWrapper.UDPSocket udp) {
udp.init(getBA(), EventName, 8192, socket);
}
#end if
Erel you are the best !!
I tested the code above and it works perfectly.
I transferred you € 20
Many Thanks
monki
javac 1.8.0_261
src\b4a\ssdpcapture\main.java:1275: error: cannot find symbol
udp.init(getBA(), EventName, 8192, socket);
^
symbol: method getBA()
location: class main
Erel you are the best !!
I tested the code above and it works perfectly.
I transferred you € 20
Many Thanks
monki
Hello Paul,
I have moved the relevant code into a class. the attached code works for me.
monki