如何在Android> = 7.1(包括共享互联网访问)上以编程方式打开Wifi-Hotspot?

时间:2021-10-25 17:19:06

We have been using reflection to enable the wifi hotspot in Android with our app (as everybody else, as it seems):

我们一直在使用反射来启用Android中的wifi热点和我们的应用程序(就像其他所有人一样):

WifiManager mWifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, wifiConfig, enabled);

Since Android 7.1 this is not working any more. Although it starts the hotspot (I see the hotspot icon in the info bar), the connecting client can't use the internet connection of the phone.

从Android 7.1开始,这不再适用了。虽然它启动了热点(我在信息栏中看到了热点图标),但是连接客户端无法使用手机的互联网连接。

With Oreo (Android 8.0) the situation is getting worse as it does not do anything at all.

随着Oreo(Android 8.0)的出现,情况越来越糟,因为它根本没有做任何事情。

I found this solution for a local-only-hotspot: How to turn on/off wifi hotspot programatically in Android 8.0 (Oreo) But we need to share the internet connection, so we can't use this one.

我发现这个解决方案只适用于本地热点:如何在Android 8.0(Oreo)中以编程方式打开/关闭wifi热点但是我们需要共享互联网连接,所以我们不能使用这个。

Also having root access to the phone is no solution for us.

对我们来说,拥有root权限也无法解决问题。

Did we miss any official or unofficial way to achieve our goal or is there really no way of doing this any more since Android 7.1?

我们是否错过任何官方或非官方的方式来实现我们的目标,或者自从Android 7.1以来真的没有办法做到这一点吗?

1 个解决方案

#1


-1  

Although its not a proper solution but u can give it a try if there is no alternative.

虽然它不是一个合适的解决方案,但如果没有替代方案,你可以尝试一下。

First: CHECK IF HOTSPOT ACTIVE

第一:检查HOTSPOT是否有效

boolean isHotSpotActive(Context cxt)
{
    boolean isSuccess = false;
    WifiManager wifi = (WifiManager)cxt.getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods = wifi.getClass().getDeclaredMethods();
    for (Method method : wmMethods)
    {
        if (method.getName().equals("isWifiApEnabled"))
        {
            try 
            {
                boolean isWifiApEnabled = (boolean)method.invoke(wifi);
                isSuccess = isWifiApEnabled;
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }catch (IllegalAccessException e){
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    return  isSuccess;
}

Second: If hotspot active , turn Wifi ON and then OFF.

第二:如果热点处于活动状态,请打开Wifi然后再关闭。

WifiManager wifimanager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
boolean isWifiOn =  wifimanager.setWifiEnabled(true); // turning wifi on 
//will turn off the hotspot
if(isWifiOn)
{
    wifimanager.setWifiEnabled(false);
}

#1


-1  

Although its not a proper solution but u can give it a try if there is no alternative.

虽然它不是一个合适的解决方案,但如果没有替代方案,你可以尝试一下。

First: CHECK IF HOTSPOT ACTIVE

第一:检查HOTSPOT是否有效

boolean isHotSpotActive(Context cxt)
{
    boolean isSuccess = false;
    WifiManager wifi = (WifiManager)cxt.getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods = wifi.getClass().getDeclaredMethods();
    for (Method method : wmMethods)
    {
        if (method.getName().equals("isWifiApEnabled"))
        {
            try 
            {
                boolean isWifiApEnabled = (boolean)method.invoke(wifi);
                isSuccess = isWifiApEnabled;
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }catch (IllegalAccessException e){
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    return  isSuccess;
}

Second: If hotspot active , turn Wifi ON and then OFF.

第二:如果热点处于活动状态,请打开Wifi然后再关闭。

WifiManager wifimanager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
boolean isWifiOn =  wifimanager.setWifiEnabled(true); // turning wifi on 
//will turn off the hotspot
if(isWifiOn)
{
    wifimanager.setWifiEnabled(false);
}