2014-12-11 22 views
2

我有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中通過共享網絡進行文件共享。此應用程序創建無線共享,無需共享設備的互聯網連接。創建的共享僅用於共享不共享互聯網的文件。如何實現這個概念。誰能幫我?如何在android中無需共享互聯網連接(Hotspot)來創建wifi共享?

回答

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