I am using the following code from the previous post on StackOverflow regarding this issue,
public static boolean configApState(Context context) {
WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wificonfiguration = null;
try {
// if WiFi is on, turn it off
if(isApOn(context)) {
if (wifimanager != null) {
wifimanager.setWifiEnabled(false);
}
}
Method method = wifimanager != null ? wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class) : null;
if (method != null) {
method.invoke(wifimanager, null, !isApOn(context));
}else{
return false;
}
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
It seems working perfectly fine. I am using this code on Android TV Box X96 mini Running Android 7.0 as far as I use for static IP. But when I try to connect automatically then the DHCP of Android TV provide very strange IP to connecting device.
The one I am getting is 169.254.68.26 On the other hand, if I try to ping the device which is connected through static IP 192.168.43.50 it gives me PING: transmit failed. General failure. Error message.
On the other hand, If I try to enable wifi hotspot manually from the setting menu then the Obtained IP is normal and ping also works fine. Again I switch off hotspot and try to on hotspot via App then It shows the same above mentioned behavior.
Is there something which I am missing?
Here is the service class in which I am using the above-mentioned function
public class MyPersistingService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(),""+AppManager.isApOn(getApplicationContext()),Toast.LENGTH_LONG).show();
if(!AppManager.isApOn(getApplicationContext())) {
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifi != null) {
wifi.setWifiEnabled(false);
AppManager.configApState(getApplicationContext());
}else{
Toast.makeText(getApplicationContext(), "No Wifi Found", Toast.LENGTH_SHORT).show();
}
}
return START_STICKY;
}}
Edit I have tried following function in both of cases to obtain Ip address
public static String getIPInfo2(Context context) throws SocketException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
String finalIP = "";
while (interfaces.hasMoreElements())
{
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback())
continue; // Don't want to broadcast to the loopback interface
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses())
{
InetAddress broadcast = interfaceAddress.getBroadcast();
InetAddress ip = interfaceAddress.getAddress();
// interfaceAddress.getNetworkPrefixLength() is another way to express subnet mask
// Android seems smart enough to set to null broadcast to
// the external mobile network. It makes sense since Android
// silently drop UDP broadcasts involving external mobile network.
if (broadcast == null)
continue;
finalIP = ip.getHostAddress();
}
}
return finalIP;
}
and I observed strange behavior. When I on Hotspot Tethering manually from settings. This function returns 192.168.43.1 but when I turn wifi via Code then the above function returns "" empty string.
192.168.43.1 is the correct ip address that should be returned either your device is turning on hotspot programmatically or manually. Since the ip address for an android device doesn't changes.
P.S: Unless its android pie or Google Pixel devices.
Use the following code to turn on your hotspot. This is reflection using the private APIs of android. It's not recommended but if you've no options left then maybe you want to go this way:
And here's the code you can use to get the ip address. Works fine on all devices from API 15 up to Pie. Tested it on my demo app Spotserve.