繁体   English   中英

如何创建无线网络共享而无需在Android中共享互联网连接(Hotspot)?

[英]How to create the wifi tethering without sharing the internet connection(Hotspot) in android?

我有android(OS_VERSION 4.0)设备。 我想通过wifi网络将文件分享到另一个Android设备。 我知道,这可以通过上面的android 4.0中的wifi p2p(WifiDirect)来完成。 但这在Android 2.3.3设备中是不可能的(在Android 4.0之前)。 我发现Superbeam应用程序通过android 2.3.3中的共享网络进行文件共享。此应用程序创建wifi网络共享而不共享设备的互联网连接。 创建的网络共享仅用于共享不用于共享Internet的文件。 如何实现这一理念。 谁能帮我?

这个答案可能有助于有同样问题的人。 我实现的简单逻辑是,

1.创建wifi网络共享(Hotspot)

2.禁用移动数据连接

代码是,

//To enable the wifi hotspot
setWifiTetheringEnabled(true);    

//To disable the mobile data cnnection
setMobileDataEnabled(false);

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;
        }
    }
}


private void setMobileDataEnabled(Context context, boolean enabled) {
    try {
        final ConnectivityManager conman = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class
                .forName(conman.getClass().getName());
        final Field iConnectivityManagerField = conmanClass
                .getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField
                .get(conman);
        final Class iConnectivityManagerClass = Class
                .forName(iConnectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass
                .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);

        setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
    } catch (ClassNotFoundException | NoSuchFieldException
            | IllegalAccessException | IllegalArgumentException
            | NoSuchMethodException | InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM