. Confirm if Wifi is On Using isEnabled() in WifiManager. If not, use setEnabled(true) to turn on it. Note: May take some time, suggest to wait 1~3 seconds.
2. Scan AP Using scanAP() in WifiManager The result will not reply immediately, so we set a receiver to receive the result latter. The receiver should extend from BroadcastReceiver to receive the broadcast message. Ex: if( wifiManager.startScan()){ IntentFilter inf = new IntentFilter(); inf.addAction(WifiManager.SCAN_RESULT_AVAILABLE_ACTION); registerReceiver(new WifiResultManager(), inf); }
class WifiResultManager extends BroadcastReceiver{ public WifiResultManager(){ }
public void onReceive(Context cxt, Intent it){ //Do something } }

3. Connect to AP Using WifiConfiguration to set the specified settings. (1) If a password is needed, you should set to string wepKeys[i] (i=1~3). If the password only contains 0-9A-Fa-f (Hex), you can just put the password to the string, but if it have ascii codes, you should add additional /" to both side of the string. (Ex: if password="wifimanager00", you should save as "/"wifimanager00/"")
(2) Set the BSSID but SSID. Note: I don't know why it's not work after I set the SSID.
(3) Choose the correct settings. For example, you can set it for open network. conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); conf.status=WifiConfiguration.Status.ENABLED;
then add it to manager and enable it. conf.networkId = manager.addNetwork(conf); //At this part, you can check the existing network to update it by using updateNetwork(); //Simplely, just add a new one. manager.enableNetwork(conf.networkId, true);
After you done these works, you will find that, yes, the connection is established, but no ip address. This is because it will take some time to get the IP Address from AP. You can add another receiver to listen the NETWORK_STATE_CHANGED_ACTION message. In general, it may take 3~10 seconds to get the correct IP Address.
Now, you can use it. Have fun to your Java.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 移动安全-java JEB安装使用
  2. ubuntu android studio 打不开windows a
  3. 【Android】Handler的应用(一):从服务器端加
  4. android webview加载不出ajax的问题
  5. Android icon适配mipmap
  6. Android之telnet编程
  7. android samplerExternalOES 纹理
  8. Google应用在Android上的Push机制以及C2D
  9. Android项目总结5
  10. Eclipse使用