简体   繁体   English

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

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

I'm having android(OS_VERSION 4.0) device. 我有android(OS_VERSION 4.0)设备。 I would like to share the files to another android device through the wifi networks. 我想通过wifi网络将文件分享到另一个Android设备。 I know, This can be done through wifi p2p(WifiDirect) in android 4.0 above. 我知道,这可以通过上面的android 4.0中的wifi p2p(WifiDirect)来完成。 But this is not possible in android 2.3.3 devices(Prior to Android 4.0). 但这在Android 2.3.3设备中是不可能的(在Android 4.0之前)。 I found the Superbeam application does the file sharing through shared networks in android 2.3.3.This application create the wifi tethering without sharing the internet connection of the device. 我发现Superbeam应用程序通过android 2.3.3中的共享网络进行文件共享。此应用程序创建wifi网络共享而不共享设备的互联网连接。 The created tethering is only used for sharing the files not for sharing the internet. 创建的网络共享仅用于共享不用于共享Internet的文件。 How to achieve this concept. 如何实现这一理念。 Can anyone help me? 谁能帮我?

This answer may help to someone having the same question. 这个答案可能有助于有同样问题的人。 The simple logic i implemented is, 我实现的简单逻辑是,

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

2.Disable the mobile data connection 2.禁用移动数据连接

Code is, 代码是,

//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