【问题标题】:Start / stop built-in Wi-Fi / USB tethering from code?从代码开始/停止内置 Wi-Fi/USB 网络共享?
【发布时间】:2010-08-08 22:12:22
【问题描述】:

如何在我的应用程序中启动或停止 Android 2.2 中的内置网络共享?

【问题讨论】:

标签: android wifi android-2.2-froyo


【解决方案1】:

ConnectivityManager 中有一个非公开的 Tethering API。如上所示,您可以使用反射来访问它。我在许多 Android 2.2 手机上试过这个,它适用于所有手机(我的 HTC 打开网络共享,但没有在状态栏中显示这个......,所以从另一端检查)。下面是一些粗略的代码,它会发出调试信息并打开 usb0 上的网络共享。

ConnectivityManager cman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Method[] methods = cman.getClass().getDeclaredMethods();
for (Method method : methods) {
    if (method.getName().equals("getTetherableIfaces")) {
        try {
            String[] ifaces = (String[]) method.invoke(cman);
            for (String iface : ifaces) {
                Log.d("TETHER", "Tether available on " + iface);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (method.getName().equals("isTetheringSupported")) {
        try {
            boolean supported = (Boolean) method.invoke(cman);
            Log.d("TETHER", "Tether is supported: " + (supported ? "yes" : "no"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (method.getName().equals("tether")) {
        Log.d("TETHER", "Starting tether usb0");
        try {
            int result = (Integer) method.invoke(cman, "usb0");
            Log.d("TETHER", "Tether usb0 result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

请注意:此代码需要以下权限才能工作:

android.permission.ACCESS_NETWORK_STATE
android.permission.CHANGE_NETWORK_STATE

【讨论】:

    【解决方案2】:

    我回答了这个问题here。总之,有可能,代码如下:

    private void setWifiTetheringEnabled(boolean enable) {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    
        Method[] methods = wifiManager.getClass().getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals("setWifiApEnabled")) {
                try {
                    method.invoke(wifiManager, null, enable);
                } catch (Exception ex) {
                }
                break;
            }
        }
    }
    

    您的应用应具有以下权限:

    android.permission.CHANGE_WIFI_STATE

    【讨论】:

    • 但是如何检查客户端是否与设备连接?
    • 我也想知道这个。要禁用网络共享,您会调用方法setWifiApDisabled 吗?或者你可以使用method.invoke(wifiManager, null, disable);吗?
    • 不幸的是,它不再适用于 API 28 (Oreo)。看到这个:medium.com/@jean.creuzedeschatelliers/…
    【解决方案3】:

    Android SDK 中没有用于管理网络共享的公共 API——抱歉!

    【讨论】:

      【解决方案4】:

      我使用了Android How to turn on hotspot in Android Programmatically的代码!我为 android 4.2 启用了便携式热点。这是代码。

      WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      // TODO Auto-generated method stub
      WifiConfiguration wifi_configuration = null;
      wifiManager.setWifiEnabled(false);
      
      try 
      {
        //USE REFLECTION TO GET METHOD "SetWifiAPEnabled"
      Method method=wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
      method.invoke(wifiManager, wifi_configuration, true);
      } 
      catch (NoSuchMethodException e){
      // TODO Auto-generated catch block
        e.printStackTrace();
      }catch (IllegalArgumentException e) {
          // TODO Auto-generated catch block
      e.printStackTrace();
      }catch (IllegalAccessException e) {
          // TODO Auto-generated catch block
       e.printStackTrace();
      }catch (InvocationTargetException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        • 1970-01-01
        • 2012-01-07
        • 2023-03-16
        • 2014-12-30
        相关资源
        最近更新 更多