Guest User

Untitled

a guest
Aug 3rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. Connect Android to WiFi Enterprise network EAP(PEAP)
  2. public class WPAActivity extends LauncherActivity
  3. {
  4.  
  5. private static final String TAG = "WPAActivity";
  6.  
  7. /************* Definitions to find variables ***************************/
  8. private static final String INT_PRIVATE_KEY = "private_key";
  9. private static final String INT_PHASE2 = "phase2";
  10. private static final String INT_PASSWORD = "password";
  11. private static final String INT_IDENTITY = "identity";
  12. private static final String INT_EAP = "eap";
  13. private static final String INT_CLIENT_CERT = "client_cert";
  14. private static final String INT_CA_CERT = "ca_cert";
  15. private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";
  16. final String INT_ENTERPRISEFIELD_NAME ="android.net.wifi.WifiConfiguration$EnterpriseField";
  17. /************************************************************************/
  18.  
  19. /********************************Configuration Strings*********************/
  20. final String ENTERPRISE_EAP = "PEAP";
  21. final String ENTERPRISE_CLIENT_CERT = "";
  22. final String ENTERPRISE_PRIV_KEY = "";
  23. final String ENTERPRISE_PHASE2 = ""MSCHAPV2"";
  24. final String ENTERPRISE_ANON_IDENT = "";
  25. final String ENTERPRISE_CA_CERT = "";
  26. final String userName = ""my Username"";
  27. final String passString = ""my Password"";;
  28.  
  29. /**************************************************************************/
  30.  
  31.  
  32. /** Called when the activity is first created. */
  33. @Override
  34. public void onCreate(Bundle savedInstanceState)
  35. {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.main);
  38.  
  39. WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  40. WifiConfiguration wc = new WifiConfiguration();
  41. wc.SSID = ""mySSID"";
  42. wc.preSharedKey = ""my Password"";
  43. wc.hiddenSSID = true;
  44. wc.status = WifiConfiguration.Status.ENABLED;
  45.  
  46. wc.allowedKeyManagement.clear();
  47. wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
  48. wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
  49.  
  50.  
  51. /*Group Ciphers*/
  52. wc.allowedGroupCiphers.clear();
  53. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
  54. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
  55.  
  56. /*Protocols*/
  57. wc.allowedProtocols.clear();
  58. wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
  59. wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
  60.  
  61. Class[] enterpriseFieldArray = WifiConfiguration.class.getClasses();
  62. Class<?> enterpriseFieldClass = null;
  63.  
  64.  
  65. for(Class<?> myClass : enterpriseFieldArray)
  66. {
  67. if(myClass.getName().equals(INT_ENTERPRISEFIELD_NAME))
  68. {
  69. enterpriseFieldClass = myClass;
  70. break;
  71. }
  72. }
  73. Log.d(TAG, "class chosen " + enterpriseFieldClass.getName() );
  74.  
  75.  
  76. Field anonymousId = null, caCert = null, clientCert = null,
  77. eap = null, identity = null, password = null,
  78. phase2 = null, privateKey = null;
  79.  
  80. Field[] fields = WifiConfiguration.class.getFields();
  81.  
  82.  
  83. for (Field tempField : fields)
  84. {
  85. if (tempField.getName().trim().equals(INT_ANONYMOUS_IDENTITY))
  86. {
  87. anonymousId = tempField;
  88. Log.d(TAG, "field " + anonymousId.getName() );
  89. }
  90. else if (tempField.getName().trim().equals(INT_CA_CERT))
  91. {
  92. caCert = tempField;
  93. }
  94. else if (tempField.getName().trim().equals(INT_CA_CERT))
  95. {
  96. }
  97. else if (tempField.getName().trim().equals(INT_CLIENT_CERT))
  98. {
  99. clientCert = tempField;
  100. Log.d(TAG, "field " + clientCert.getName() );
  101. }
  102. else if (tempField.getName().trim().equals(INT_EAP))
  103. {
  104. eap = tempField;
  105. Log.d(TAG, "field " + eap.getName() );
  106. }
  107. else if (tempField.getName().trim().equals(INT_IDENTITY))
  108. {
  109. identity = tempField;
  110. Log.d(TAG, "field " + identity.getName() );
  111. }
  112. else if (tempField.getName().trim().equals(INT_PASSWORD))
  113. {
  114. password = tempField;
  115. Log.d(TAG, "field " + password.getName() );
  116. }
  117. else if (tempField.getName().trim().equals(INT_PHASE2))
  118. {
  119. phase2 = tempField;
  120. Log.d(TAG, "field " + phase2.getName() );
  121.  
  122. }
  123. else if (tempField.getName().trim().equals(INT_PRIVATE_KEY))
  124. {
  125. privateKey = tempField;
  126. }
  127. }
  128.  
  129.  
  130. Method setValue = null;
  131.  
  132.  
  133. for(Method m: enterpriseFieldClass.getMethods())
  134. {
  135. if(m.getName().trim().equals("setValue"))
  136. {
  137. Log.d(TAG, "method " + m.getName() );
  138. setValue = m;
  139. break;
  140. }
  141. }
  142.  
  143. try
  144. {
  145. // EAP
  146. setValue.invoke(eap.get(wc), ENTERPRISE_EAP);
  147.  
  148. // EAP Phase 2
  149. setValue.invoke(phase2.get(wc), ENTERPRISE_PHASE2);
  150.  
  151. // EAP Anonymous Id
  152. setValue.invoke(anonymousId.get(wc), ENTERPRISE_ANON_IDENT);
  153.  
  154. // EAP CA Certificate
  155. setValue.invoke(caCert.get(wc), ENTERPRISE_CA_CERT);
  156.  
  157. // Private Key
  158. setValue.invoke(privateKey.get(wc), ENTERPRISE_PRIV_KEY);
  159.  
  160. // EAP Identity
  161. setValue.invoke(identity.get(wc), userName);
  162.  
  163. // EAP Password
  164. setValue.invoke(password.get(wc), passString);
  165.  
  166. // EAP Client certificate
  167. setValue.invoke(clientCert.get(wc), ENTERPRISE_CLIENT_CERT);
  168.  
  169. }
  170. catch (Exception e)
  171. {
  172.  
  173. }
  174.  
  175. Log.d("WifiPreference", "2");
  176. int res = wifi.addNetwork(wc);
  177. Log.d("WifiPreference", "add Network returned " + res );
  178. boolean b = wifi.enableNetwork(res, true);
  179. Log.d("WifiPreference", "enableNetwork returned " + b );
  180. }
  181. }
  182.  
  183. 02-09 09:23:30.514: I/ActivityManager(2084): Displayed activity com.test.wpa/.WPAActivity: 445 ms (total 445 ms)
  184.  
  185. 02-09 09:23:31.514: I/wpa_supplicant(27633): CTRL-EVENT-SCAN-RESULTS Ready
  186.  
  187. 02-09 09:23:31.514: I/wpa_supplicant(27633): Trying to associate with 00:1c:0f:82:04:e0 (SSID='*****' freq=2437 MHz)
  188.  
  189. 02-09 09:23:31.514: I/wpa_supplicant(27633): CTRL-EVENT-STATE-CHANGE id=-1 state=3
  190.  
  191. 02-09 09:23:31.649: V/WifiMonitor(2084): Event [Trying to associate with 00:1c:0f:82:04:e0 (SSID='*****' freq=2437 MHz)]
  192.  
  193. 02-09 09:23:31.649: V/WifiMonitor(2084): Event [CTRL-EVENT-STATE-CHANGE id=-1 state=3]
  194.  
  195. 02-09 09:23:31.654: V/WifiStateTracker(2084): Changing supplicant state: SCANNING ==> ASSOCIATING
  196.  
  197. 02-09 09:23:31.654: D/NetworkStateTracker(2084): setDetailed state, old =SCANNING and new state=CONNECTING
  198.  
  199. 02-09 09:23:31.659: D/ConnectivityService(2084): ConnectivityChange for WIFI: CONNECTING/CONNECTING
  200.  
  201. 02-09 09:23:32.621: I/wpa_supplicant(27633): CTRL-EVENT-STATE-CHANGE id=0 state=4
  202.  
  203. 02-09 09:23:32.621: V/WifiMonitor(2084): Event [CTRL-EVENT-STATE-CHANGE id=0 state=4]
  204.  
  205. 02-09 09:23:32.624: I/wpa_supplicant(27633): Associated with 00:1c:0f:82:04:e0
  206.  
  207. 02-09 09:23:32.624: I/wpa_supplicant(27633): CTRL-EVENT-EAP-STARTED EAP authentication started
  208.  
  209. 02-09 09:23:32.629: V/WifiMonitor(2084): Event [Associated with 00:1c:0f:82:04:e0]
  210.  
  211. **02-09 09:23:32.629: V/WifiMonitor(2084): Event [CTRL-EVENT-EAP-STARTED EAP authentication started]**
  212.  
  213. 02-09 09:23:32.629: V/WifiStateTracker(2084): Changing supplicant state: ASSOCIATING ==> ASSOCIATED
  214.  
  215. **02-09 09:23:32.629: D/NetworkStateTracker(2084): setDetailed state, old =CONNECTING and new state=CONNECTING**
  216.  
  217. **02-09 09:23:32.634: I/wpa_supplicant(27633): CTRL-EVENT-DISCONNECTED - Disconnect event - remove keys**
  218.  
  219. 02-09 09:23:32.644: I/wpa_supplicant(27633): CTRL-EVENT-STATE-CHANGE id=0 state=0
  220.  
  221. **02-09 09:23:32.644: V/WifiMonitor(2084): Event [CTRL-EVENT-DISCONNECTED - Disconnect event - remove keys]**
  222.  
  223. 02-09 09:23:32.644: V/WifiMonitor(2084): Event [CTRL-EVENT-STATE-CHANGE id=0 state=0]
Add Comment
Please, Sign In to add comment