为牛轧糖设置Tethering / hotspot

时间:2017-03-13 19:46:50

标签: android android-7.0-nougat tethering

我使用此代码激活或停用网络共享热点,以便其他设备可以使用我的设备的互联网数据。 它曾经运作良好,但它不再适用于牛轧糖,你有任何提示吗? 感谢。

这是我使用的代码:

  private boolean ActivateTethering(boolean bEstate) 
  {  WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

     Method[] methods = wifiManager.getClass().getDeclaredMethods();
     for(Method method : methods) 
     {  if(method.getName().equals("setWifiApEnabled")) 
        {  try 
           {  if(bEstate == true)
              {  wifiManager.setWifiEnabled(false); //Turning off wifi because tethering requires wifi to be off
                 method.invoke(wifiManager, null, true); //Activating tethering
                 return true;
              }
              else
              {  method.invoke(wifiManager, null, false); //Deactivating tethering
                 return true;
              }
           }
           catch(Exception e) 
           {  return false;
           }           
         }
     }

     //Error setWifiApEnabled not found 
     return false;
 }

我收到错误 java.lang.reflect.InvocationTargetException

1 个答案:

答案 0 :(得分:0)

改为使用

try {
    Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
    method.invoke(wifiManager, null, true); // true to enable, false to disable
} catch (NoSuchMethodException e) {
    // WiFi tethering is blocked.
}

根据您的需要进行更改 它适用于我的S7 Edge而getDeclaredMethods();方式不适用。