3

我有 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 网络共享而不共享设备的互联网连接。创建的网络共享仅用于共享文件而不用于共享互联网。如何实现这个概念。谁能帮我?

4

1 回答 1

1

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

1.创建wifi网络共享(热点)

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();
    }
}
于 2014-12-15T12:16:20.773 回答