【问题标题】:Android 7.1 couldn't enable wifi hotspot programmaticallyAndroid 7.1 无法以编程方式启用 wifi 热点
【发布时间】:2020-04-06 13:07:55
【问题描述】:

我正在尝试在 android 中打开 Wifi 热点,当我运行下面的代码时,它在 Api 28 上成功运行我可以打开 Wifi 热点而不会出错,但是当我在 Android 7.1 设备的 Api 25 中运行它时,它会出现以下错误,它说获得 WRITE_SETTINGS 权限,但是当我测试 Api 28 时不需要运行应用程序,为什么我会得到这个错误以及如何为 android Api 25 运行它

遇到错误

W/System.err: java.lang.NoSuchMethodException: setWifiApEnabled  [class android.net.wifi.WifiConfiguration, boolean]
        at java.lang.Class.getMethod(Class.java:1981)
        at java.lang.Class.getMethod(Class.java:1637)
        at com.kocsistem.pixageoneandroid.utils.Utils.configApState(Utils.java:1126)
        at com.kocsistem.pixageoneandroid.utils.Utils.openHotspot(Utils.java:1095)
        at com.kocsistem.pixageoneandroid.network.broadcast.NetworkChangeReceiver$2.run(NetworkChangeReceiver.java:120)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

打开热点代码

public static void openHotspot(Context context,boolean enable){
    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        assert manager != null;
        try {
            manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

                @SuppressLint("SetTextI18n")
                @Override
                public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                    super.onStarted(reservation);
                    Log.i("Wifi Hotspot is on now , reservation is : %s", reservation.toString());
                    mReservation = reservation;
                    WifiManager.LocalOnlyHotspotReservation mReservation = reservation;
                    String key = mReservation.getWifiConfiguration().preSharedKey;
                    Log.i("key:",key);
                    String ussid = mReservation.getWifiConfiguration().SSID;
                    Log.i("ussid:",ussid);
                }

                @Override
                public void onStopped() {
                    super.onStopped();
                    Log.i("onStopped: ","");
                }

                @Override
                public void onFailed(int reason) {
                    super.onFailed(reason);
                    Log.i("onFailed: ","");
                }
            }, new Handler());
        }catch (Exception e){
            e.printStackTrace();
        }
    }else{
      //for Api<26
        configApState(context);
    }
}

检查wifi热点是打开还是关闭

public static boolean isApOn(Context context) {
    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    try {
        Method method = wifimanager.getClass().getDeclaredMethod("isWifiAp Enabled");
        method.setAccessible(true);
        return (Boolean) method.invoke(wifimanager);
    }
    catch (Throwable ignored) {}
    return false;
}

打开或关闭 wifi 热点

    public static boolean configApState(Context context) {
        WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
        WifiConfiguration wificonfiguration = null;
        try {
// if WiFi is on, turn it off
            if(isApOn(context)) {
                wifimanager.setWifiEnabled(false);
            }
            Method method = wifimanager.getClass().getMethod("setWifiApEnabled ", WifiConfiguration.class, boolean.class);
            method.invoke(wifimanager, wificonfiguration, !isApOn(context));
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

【问题讨论】:

    标签: android wifimanager hotspot wifi


    【解决方案1】:

    此代码适用于 Android Api 26 (Oreo) 及更高版本,它不适用于牛轧糖 (api 25) 还有另一种方法可以做到这一点,您必须获取名为“setWifiApEnabled”的方法检查此代码的和平

    PS:您必须将代码从 c# 转换为 java 并且不要忘记授予写入设置权限...

      private bool ActivateTethering(string ssid, string password)
        {
            var myConfiguration = new WifiConfiguration();
            WifiManager wifimanager = (WifiManager)Context.GetSystemService(Context.WifiService);
    
            myConfiguration.Ssid = ssid;
            myConfiguration.PreSharedKey = password;
            myConfiguration.AllowedAuthAlgorithms.Set((int)AuthAlgorithmType.Shared);
            myConfiguration.AllowedProtocols.Set((int)ProtocolType.Rsn);
            myConfiguration.AllowedProtocols.Set((int)ProtocolType.Wpa);
            myConfiguration.AllowedKeyManagement.Set((int)KeyManagementType.WpaPsk);
    
        
            var enableWifi = wifimanager.Class.GetDeclaredMethods();
    
            try
            {
                bool setWifiConfig = false;
                foreach (var method in enableWifi)
                {
                    if (method.Name.Equals("setWifiApEnabled"))
                    {
                        setWifiConfig = (bool)method.Invoke(wifimanager, myConfiguration, true);
                        break;
                    }
                }
            }
            catch (InvocationTargetException e)
            {
                Console.WriteLine(e.Data.ToString());
            }
           
            return setWifiConfig ;
        }
    

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多