Wifi

[code]
. 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.
[/code]


mScanResult.capabilities的几个参数的含义:
capabilities=[WPA2-PSK-CCMP]
capabilities=[WPA-PSK-TKIP]
意思是:
认证类型:WPA-PSK 和 WPA2-PSK
加密算法:TKIP 和 AES,CCMP
Describes the authentication, key management, and encryption schemes supported by the access point. (就是路由器支持的认证类型, key管理类型以及加密协议)

获取本台手机的wifi网卡ip
[code]
public String getIpAddress(){
String ip = null;
if(mWifiInfo!=null){
ip = int2Ip(mWifiInfo.getIpAddress());
}
return ip;
}

private String int2Ip(int i) {

return ((i >> 24 ) & 0xFF ) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
( i & 0xFF) ;
}
[/code]


SSID指AP设备名称,BSSID指AP设备的MAC地址

android连接加密网络,其中 一个netId对应一个config,所以只要有netId就可以连接网络
[code]
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
boolean bRet1 = wifiManager.setWifiEnabled(true);

//add network
if (wifiManager.startScan()) //扫描可用的无线网络
{
List<ScanResult> scanResultList = wifiManager.getScanResults();
for (int i = 0; i < scanResultList.size(); i++)
{
ScanResult scanRet = scanResultList.get(i);
if (scanRet.SSID.equalsIgnoreCase("TEST")) //找到 TEST
{
WifiConfiguration config = new WifiConfiguration();

config.SSID = "\"" + scanRet.SSID + "\"";
config.preSharedKey = "\"password\""; //指定密码
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.status = WifiConfiguration.Status.ENABLED;

int netID = wifiManager.addNetwork(config);
Log.d("WifiPreference", "add Network returned " + netID );
boolean bRet = wifiManager.enableNetwork(netID, true);
Log.d("WifiPreference", "enableNetwork returned " + bRet );
}
}
}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值