B4A Library Android Broadcast Address

Hi there,

I was working with UDP library to send messages to "255.255.255.255" to let all the devices on my local network receive my message.

That worked until I found that this is not the correct way to do that. It works under some pre defined conditions, mostly in home lans that everyone is on WiFi only for example, there is a lot of exceptions.

I was looking for an explanation and I found what is the right way to do that, however I don't know how to get that information with B4A.

The fact is that there is always 1 IP andress on the local sub-net reserved for Brodcast, for example:

192.168.0.1 - my ip for example
255.255.255.0 - my mask (needed to calculate this)

192.168.0.255 - Broadcast IP

Anything send to the specific Broadcast IP on this network will be listen by all devices and routed by any access point or router (of course depends on the topology) with no need to configure it.

It is not the case on sending to all 255 IP network. Many routers do not even consider it.

What I have found so far is that each platform has a way to access this information. It is part of the basic lan management. Somehow Android has this information. How to get it PLEASE?

More Info: http://stackoverflow.com/questions/2993874/android-broadcast-address

Many thanks,

Eduardo
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can take this code snippet and compile it into a small library with Simple Library Compiler:
B4X:
pacakge aaa.bbb;

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import anywheresoftware.b4a.BA.ShortName;


@ShortName("GetBroadcast")
public class GetBroadcast{
   
   public static String getBroadcast() throws SocketException {
    System.setProperty("java.net.preferIPv4Stack", "true");
    for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum.hasMoreElements();) {
    NetworkInterface ni = niEnum.nextElement();
    if (!ni.isLoopback()) {
    for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
    return interfaceAddress.getBroadcast().toString().substring(1);
    }
    }
    }
    return "";
   }
}
The source folder structure should be aaa\bbb\GetBroadcast.java
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Many thanks Erel,

I wonder if you could give me a final help. I have made the package as you stated, and could use it etc.

However I am getting a null pointer exception here:

return interfaceAddress.getBroadcast().toString().substring(1);

I have attached the project directory of this library.

I am newbie on java, I have no idea how to trace this or get it working. It seems to be that toString, but that because getBroadcast is null, it should not if I see correctly.

Any help is much appreciated!

PS. I am little lost yet on the naming conventions for creating this library, a lot of GetBroadcast everywhere, I am not sure this could be a problem.

Eduardo
 

Attachments

  • GetBroadcast.zip
    1.5 KB · Views: 307
Last edited:

EduardoElias

Well-Known Member
Licensed User
Longtime User
Sure! I was having this problem in some customers and they were behind me, trying to catch me, now I am back again having peace!

Attached is the GetBroadcast tiny library, with the java source code and the library already compiled.

usage:

- copy the jar and xml to the libraries folder
- add the getbroadcast library to the project
- and this sub as a suggested usage:
- when needed to send a UDP message, use this BroadcastIP sub to get the right address


B4X:
sub BroadcastIP as string

dim b as GetBroadcast
dim bip as string = b.broadcast
if bip = null then
  return "255.255.255.255"
else
  return bip
end if
end sub

have fun,

Eduardo
 

Attachments

  • GetBroadcast.zip
    2.9 KB · Views: 388
Last edited:

EduardoElias

Well-Known Member
Licensed User
Longtime User
Please, I am having the following problem with the java code above in this line

B4X:
if (!ni.isLoopback()) {

The isLoopback is only accepted on api 9 and higher. I have tried to use in a device with api 8 and got an exception:


java.lang.NoSuchMethodError: java.net.NetworkInterface.isLoopback

How to test the API level before calling that function on the Java library above.

Could someone give a hand on point me that?


Many thanks.
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Thank you for that, I have add on B4A a check for the api level before calling this tiny library. If it is Level 8 or less I will use the "255.255.255.255" broadcast address, that is not guaranteed to work in all routers, but there is no other way.

Thank you
 
Top