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

2k views Asked by At

I'm having android(OS_VERSION 4.0) device. I would like to share the files to another android device through the wifi networks. I know, This can be done through wifi p2p(WifiDirect) in android 4.0 above. But this is not possible in android 2.3.3 devices(Prior to 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. The created tethering is only used for sharing the files not for sharing the internet. How to achieve this concept. Can anyone help me?

1

There are 1 answers

0
Jayaprakash Marshal On BEST ANSWER

This answer may help to someone having the same question. The simple logic i implemented is,

1.Create the wifi tethering(Hotspot)

2.Disable the mobile data connection

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