Hello,
I'm trying to implement a auto discovery system, but Android 6 is blocking receiving UDP sent via broadcast, the app can send broadcast but not receive.
Android 6 do not receive "255.255.255.255" and not even "192.168.1.255/24"
Android 6 only receives UDP if the transmitter directs to its IP address.
Already in Android 2.3 is not blocking.
Any configuration that can be performed?
Source: https://www.b4x.com/android/forum/threads/mqtt-chat-with-auto-discovery.75713/
Log from Android 6:
Log from Android 2:
I'm trying to implement a auto discovery system, but Android 6 is blocking receiving UDP sent via broadcast, the app can send broadcast but not receive.
Android 6 do not receive "255.255.255.255" and not even "192.168.1.255/24"
Android 6 only receives UDP if the transmitter directs to its IP address.
Already in Android 2.3 is not blocking.
Any configuration that can be performed?
B4X:
#Region Project Attributes
#ApplicationLabel: B4A 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
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private autodiscover As UDPSocket
Private BroadcastTimer As Timer
'Private serializator As B4XSerializator
Private const discoverPort As Int = 22222
Private server As ServerSocket 'ignore
Public DiscoveredServer As String
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
autodiscover.Initialize("autodiscover", 22222 , 8192)
BroadcastTimer.Initialize("BroadcastTimer", 5000)
BroadcastTimer.Enabled = True
Log(server.GetMyWifiIP)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Private Sub autodiscover_PacketArrived (Packet As UDPPacket)
Dim msg As String
msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
Log(msg)
Try
'Dim bc As ByteConverter
'Dim data(Packet.Length) As Byte
'bc.ArrayCopy(Packet.Data, Packet.Offset, data, 0, Packet.Length)
Dim ds As String = msg ' serializator.ConvertBytesToObject(data)
If ds <> DiscoveredServer Then
DiscoveredServer = ds
Log("Discovered server: " & DiscoveredServer)
'CallSub(Main, "UpdateState")
End If
Catch
Log(LastException)
End Try
End Sub
Private Sub BroadcastTimer_Tick
Dim address As String = GetBroadcastAddress
Dim data() As Byte = server.GetMyWifiIP.GetBytes("UTF8")
Log("GetBroadcastAddress: " & address)
If address <> "" Then
Dim up As UDPPacket
'up.Initialize(serializator.ConvertObjectToBytes(server.GetMyWifiIP), address, discoverPort)
up.Initialize(data, address, discoverPort)
autodiscover.Send(up)
End If
End Sub
'Returns the UDP broadcast address.
'Returns an empty string if not available.
Private Sub GetBroadcastAddress As String
Dim niIterator As JavaObject
niIterator = niIterator.InitializeStatic("java.net.NetworkInterface").RunMethod("getNetworkInterfaces", Null)
Do While niIterator.RunMethod("hasMoreElements", Null)
Dim ni As JavaObject = niIterator.RunMethod("nextElement", Null)
If ni.RunMethod("isLoopback", Null) = False Then
Dim addresses As List = ni.RunMethod("getInterfaceAddresses", Null)
For Each ia As JavaObject In addresses
Dim broadcast As Object = ia.RunMethod("getBroadcast", Null)
If broadcast <> Null Then
Dim b As String = broadcast
Return b.SubString(1)
End If
Next
End If
Loop
Return ""
End Sub
Source: https://www.b4x.com/android/forum/threads/mqtt-chat-with-auto-discovery.75713/
Log from Android 6:
GetBroadcastAddress: 192.168.1.255
192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.61
Log from Android 2:
GetBroadcastAddress: 192.168.1.255
192.168.1.43
192.168.1.61
Discovered server: 192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.43
Discovered server: 192.168.1.43
192.168.1.61
Discovered server: 192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.43
Discovered server: 192.168.1.43
GetBroadcastAddress: 192.168.1.255
192.168.1.43
192.168.1.61
Discovered server: 192.168.1.61
192.168.1.61
GetBroadcastAddress: 192.168.1.255
192.168.1.43