Getting Broadcast Wi-Fi Address in Android Wi-Fi

I am developing an application that uses Wi-Fi to send UDP messages between all mobile phones that are on the same network where my application is.

I managed to send / receive packets from many cell phones with an external AP that my router has.

But given the lack of an AP, I want users to be able to use their Wifi Hotspot feature on their phone so they can use my apps. Thus, one of the cell phones will be a Wi-Fi access point, and everyone else will be connected to it.

I need users to connect to Wi-Fi on their own. Either on an external AP or on a specified access point. Then, when my application starts, it checks if the phone is connected to the Wi-Fi network by calling WifiManager.isWifiEnabled () and NetworkInfo.isConnected ().

The problem is that if I call these functions on a cell phone using the hotspot function, the isConnected () function will return false. And I can not get the broadcast address using WifiManager.getDhcpInfo (). Other phones that are connected to a hot spot, they work fine. But the hotspot mobile phone cannot send any broadcasts since WifiManager is disabled.

So my question is: "Is there a way to check if the cell phone is currently a Wi-Fi access point? And if so, can I get its broadcast address?"

+8
android udp wifi broadcast
source share
1 answer

First you can check what your IP address is:

public InetAddress getIpAddress() { InetAddress inetAddress = null; InetAddress myAddr = null; try { for (Enumeration < NetworkInterface > networkInterface = NetworkInterface .getNetworkInterfaces(); networkInterface.hasMoreElements();) { NetworkInterface singleInterface = networkInterface.nextElement(); for (Enumeration < InetAddress > IpAddresses = singleInterface.getInetAddresses(); IpAddresses .hasMoreElements();) { inetAddress = IpAddresses.nextElement(); if (!inetAddress.isLoopbackAddress() && (singleInterface.getDisplayName() .contains("wlan0") || singleInterface.getDisplayName().contains("eth0") || singleInterface.getDisplayName().contains("ap0"))) { myAddr = inetAddress; } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return myAddr; } 

and I used this IP for translation as follows:

 public InetAddress getBroadcast(InetAddress inetAddr) { NetworkInterface temp; InetAddress iAddr = null; try { temp = NetworkInterface.getByInetAddress(inetAddr); List < InterfaceAddress > addresses = temp.getInterfaceAddresses(); for (InterfaceAddress inetAddress: addresses) iAddr = inetAddress.getBroadcast(); Log.d(TAG, "iAddr=" + iAddr); return iAddr; } catch (SocketException e) { e.printStackTrace(); Log.d(TAG, "getBroadcast" + e.getMessage()); } return null; } 

This can be done, of course, in one way, but in my implementation it was useful to split it into two methods.

To determine if Wifi Tether is enabled, you can use this code:

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); Method[] wmMethods = wifi.getClass().getDeclaredMethods(); for (Method method: wmMethods) { if (method.getName().equals("isWifiApEnabled")) { try { if ((Boolean) method.invoke(wifi)) { isInetConnOn = true; iNetMode = 2; } else { Log.d(TAG, "WifiTether off"); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } 

If client devices need to know if the server device is a mobile access point, a specific IP address can be used. As far as I know, all Tethering devices have the same address 192.168.43.1. This is the same on Android 2.3 and 4.+, tested on many phones and tablets. Of course, this is not the best solution, but it is fast. In my application, client devices check (sends a packet to this address) and the response to my server devices in a predefined form, for example, "yesIamInTheterModeIamYourServer".

+13
source share

All Articles