First you can check what is your IP address :
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 to get Broadcast this way:
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;
}
It can be done of course in one method, but in my implementation separating it into two method was useful.
To identify if Wifi Tether is on 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 needs to know if server device is mobile Hotspot, specific IP address can be used. As far as I know, all Tethering devices have the same address 192.168.43.1 It is the same on Android 2.3 and on 4.+, Checked on many phones and tablets. Of course it is not best solution but it is fast. In my application client devices checks (sends packet to this address) and my server devices response in predefined way like "yesIamInTheterModeIamYourServer".
Best Answer
Use mac filtering could be an option but the clients can always change his mac address, and with this method you only can block if the client is going to connected. If you like to disconnect clients you need use deauth packets.
There is an app in android call wifikill to send deauth packets.
http://forum.xda-developers.com/showthread.php?t=1282900
In this app you select the clients that you would like to disconnect and the android device will perform a deauth attack.
You could try to develop something but this app works successfully to me.
But if you use an open network will be very tricky to control access even thougth you deauth the clients they always can change theirs macs and try to look like a legitimate user. You will never have 100% control of the clients in an open network.