Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3003481
  • 博文数量: 674
  • 博客积分: 17881
  • 博客等级: 上将
  • 技术积分: 4849
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 10:15
文章分类

全部博文(674)

文章存档

2013年(34)

2012年(146)

2011年(197)

2010年(297)

分类: LINUX

2011-11-04 18:22:23

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.
阅读(1376) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~