目前可以使用wireless-tools 或wpa_supplicant工具來配置無線網絡。請記住重要的一點是,對無線網絡的配置是全局性的,而非針對具體的接口。
wpa_supplicant是一個較好的選擇,但缺點是它不支援所有的驅動。請浏覽wpa_supplicant網站獲得它所支援的驅動清單。另外,wpa_supplicant目前隻能連接配接到那些你已經配置好ESSID的無線網絡。wireless-tools支援幾乎所有的無線網卡和驅動,但它不能連接配接到那些隻支援WPA的AP。
經過編譯後的wpa_supplicant源程式可以看到兩個主要的可執行工具:wpa_supplicant和wpa_cli。wpa_supplicant是核心程式,它和wpa_cli的關系就是服務和用戶端的關系:背景運作wpa_supplicant,使用wpa_cli來搜尋、設定、和連接配接網絡。
如何用wpa_supplicant使能一個wifi連接配接?
Step by step:
1、運作wpa_supplicant程式;
執行:/system/bin/wpa_supplicant -d -Dwext -iwlan0 -c/data/misc/wifi/wpa_supplicant.conf
其中:
-d :增加調試資訊
-Dwext :wext,驅動名稱
-iwlan0 :wlan0,網絡接口名稱
/system/bin/wpa_supplicant :wpa_supplicant可執行程式path
/data/misc/wifi/wpa_supplicant.conf :wpa_supplicant的配置檔案path
2、運作指令行工具wpa_cli ;
執行 :wpa_cli -iwlan0 -p/data/system/wpa_supplicant
注,-p/data/system/wpa_supplicant中的wpa_supplicant并不是可執行程式,而是個控制套接字。
此時會進入互動模式。其中互動模式的指令如下表:
Full command
Short command
Description
status
stat
displays the current connection status
disconnect
disc
prevents wpa_supplicant from connecting to any access point
quit
q
exits wpa_cli
terminate
term
kills wpa_supplicant
reconfigure
recon
reloads wpa_supplicant with the configuration file supplied (-c parameter)
scan
scans for available access points (only scans it, doesn't display anything)
scan_result
scan_r
displays the results of the last scan
list_networks
list_n
displays a list of configured networks and their status (active or not, enabled or disabled)
select_network
select_n
select a network among those defined to initiate a connection (ie select_network 0)
enable_network
enable_n
makes a configured network available for selection (ie enable_network 0)
disable_network
disable_n
makes a configured network unavailable for selection (ie disable_network 0)
remove_network
remove_n
removes a network and its configuration from the list (ie remove_network 0)
add_network
add_n
adds a new network to the list. Its id will be created automatically
set_network
set_n
shows a very short list of available options to configure a network when supplied with no parameters.
See next section for a list of extremely useful parameters to be used with set_network and get_network.
get_network
get_n
displays the required parameter for the specified network. See next section for a list of parameters
save_config
save_c
saves the configuration
設定網絡的基本格式:set_network <network id> <key> <parameter> [<parameter>]
顯示網絡資訊的基本格式:get_network <network id> <key>
相應的參數如下表:
Key
Parameters
ssid
Access point name
string
id_str
String identifying the network
priority
Connection priority over other APs
number (0 being the default low priority)
bssid
Mac address of the access point
mac address
scan_ssid
Enable/disbale ssid scan
0, 1, 2
key_mgmt
Type of key management
WPA-PSK, WPA_EAP, None
pairwise
Pairwise ciphers for WPA
CCMP, TKIP
group=TKIP
Group ciphers for WPA
CCMP, TKIP, WEP104, WEP40
psk
Pre-Shared Key (clear or encrypted)
wep_key0
WEP key (up to 4: wep_key[0123])
eap
Extensible Authentication Protocol
MD5, MSCHAPV2, OTP, GTC, TLS, PEAP, TTLS
identity
EAP identity string
password
EAP password
ca_cert
Pathname to CA certificate file
/full/path/to/certificate
client_cert
Pathname to client certificate
/full/path/to/certificate (PEM/DER)
private_key
Pathname to a client private key file
/full/path/to/private_key (PEM/DER/PFX)
eg.1、連接配接無加密的AP
>add_network (It will display a network id for you, assume it returns 0)
>set_network 0 ssid "666"
>set_network 0 key_mgmt NONE
>enable_network 0
>quit
eg.2、連接配接WEP加密AP
>add_network (assume return 1)
>set_network 1 ssid "666"
>set_network 1 key_mgmt NONE
>set_network 1 wep_key0 "your ap password"
>enable_network 1
eg.3、連接配接WPA-PSK/WPA2-PSK加密的AP
>add_network (assume return 2)
>set_network 2 ssid "666"
>set_network 2 psk "your pre-shared key"
>enable_network 2
到此,wifi子產品就能連接配接上AP了。
3、以上是通過指令行工具wpa_cli來實作wifi網絡的連接配接。當然,也可以通過wpa_supplicant的配置檔案來實作連接配接。
再回顧下運作wpa_supplicant時執行的指令:
/system/bin/wpa_supplicant -d -Dwext -iwlan0 -c/data/misc/wifi/wpa_supplicant.conf
我們在執行時加上了-c/data/misc/wifi/wpa_supplicant.conf,我們可以将我們要連接配接的AP的設定以一定的格式寫入wpa_supplicant.conf配置檔案中即可。
eg.
ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=system update_config=1
network={
ssid="my access point"
proto=WPA
key_mgmt=WPA-PSK
psk="you pass words"
}