我如何来重构WLAN测试程序?
Preface
”我为什么要重构WLAN测试程序?“
(1)UI设计很烂
(2)测试流程不能直观地呈现
1. WLAN
2. 打开WLAN
你需要使用到WifiManager服务, 参考mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE)
然后你调用mWifiManger.setWifiEnable(true)开启wifi。
mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
mWifiManager.setWifiEnable(true);
3. 搜索WLAN
在你发送搜寻请求之前,需要确认WiFi成功被开启。那怎样获得wifi开启或者是关闭的状态呢?
获得Wifi状态
最好的方式是注册侦听wifi事件,你可以参看如下来自于SDK的详细描述:
| String | WIFI_STATE_CHANGED_ACTION | Broadcast intent action indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. |
public static final String EXTRA_WIFI_STATE
Added in
API level 1
The lookup key for an int that indicates whether Wi-Fi is enabled, disabled, enabling, disabling, or unknown. Retrieve it with getIntExtra(String, int).
See Also
Constant Value: "wifi_state"
一旦你的程序接收来自Wifi service发送过来的消息,WIFI_STATE_ENABLED。此时,你可以认为Wifi已被成功开启,
接下来你会思考:“哦!我是不是应该扫描一下周围有没有wifi?”。可以调用Wifi startScan()函数发送扫描申请
到Wifi
service,需要你注意的是它是一次异步调用,函数会立即返回,请参看如下来自SDK上的描述:
public boolean startScan ()
Added in
API level 1
Request a scan for access points. Returns immediately. The availability of the results is made known later by means of an asynchronous event sent on completion of the scan.
Returns
trueif the operation succeeded, i.e., the scan was initiated
那你怎么知道Wifi service已经为你准备OK? 通过捕获来自wifi service的广播
事件,会是一种不错的放松。
“哦!我要的结果已经被准备好,我这就去拿出来!”
public static final String SCAN_RESULTS_AVAILABLE_ACTION
Added in
API level 1
An access point scan has completed, and results are available from the supplicant. CallgetScanResults() to obtain the results.
可能你也看到了,通过调用getScanResult()来得到搜需结果,函数的描述信息如下:
public List<ScanResult> getScanResults ()
Added in
API level 1
Return the results of the latest access point scan.
Returns
- the list of access points found in the most recent scan.
可以看到的是搜需结果(scan results)被保存进一串列表(list),列表存放了由ScanResult类封装的数据。
| Fields | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| public String | BSSID | The address of the access point. | |||||||||
| public String | SSID | The network name. | |||||||||
| public String | capabilities | Describes the authentication, key management, and encryption schemes supported by the access point. | |||||||||
| public int | frequency | The frequency in MHz of the channel over which the client is communicating with the access point. | |||||||||
| public int | level | The detected signal level in dBm. | |||||||||
| public long | timestamp | Time Synchronization Function (tsf) timestamp in microseconds when this result was last seen. | |||||||||
(待补充)
4. 连接WLAN
你需要选择可用的WiFi访问节点,可能需要你输入某访问节点的密码。等待侦测到网络成功连接
(待补充)
5. 关闭WLAN
测试WLAN完毕,需要你关闭WLAN。如果当前wifi模组已开启,调用setWifiEnable(false)将会
关闭Wifi模组;如果当前Wifi模组已关闭,虽然是关闭状态,你再调用setWifiEnable(false)仍会
返回true。
mWifiManager.setWifiEnable(false);
6. 设计UI布局
你需要考虑这样一个设计,“我怎样友好地呈现wifi access points给用户?”。参考Settings设计会是
一种不错的选择。不过有这样一种设计,使用了Dialog设计,它把你自定义的UI控件堆放到Dialog
中部位置,在底部设计处加入了如"cancel", "ok"按钮,在顶部设计处有tile提示,如"My Local Network"
可能的样子:
怎样实现这样的设计呢? 请参[Android] 我如何设计Android Dialog?
重构WLAN测试程序指南
本文阐述了重构WLAN测试程序的原因,包括改进UI设计、直观呈现测试流程,并提供了开启、搜索、连接和关闭WLAN的具体步骤。同时,介绍了如何设计友好的用户界面布局,以提升用户体验。
1170

被折叠的 条评论
为什么被折叠?



