[Android] 我如何来重构WLAN测试程序?

本文阐述了重构WLAN测试程序的原因,包括改进UI设计、直观呈现测试流程,并提供了开启、搜索、连接和关闭WLAN的具体步骤。同时,介绍了如何设计友好的用户界面布局,以提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我如何来重构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.
    你在注册Broadcast receiver时回用到Intent以及Intent Extra字段,它保存了wifi的状态信息。如下来自SDK的描述:
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).

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
  • true if the operation succeeded, i.e., the scan was initiated
    再次提醒,你需要明白它是一次异步调用,它并不会立即返回结果。搜寻结果会在一小段时间后由Wifi service发送给你。
    那你怎么知道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<ScanResultgetScanResults ()
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?

    
    



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值