Android之Wifi学习(1)

本文介绍了Android中对Wifi操作的主要类ScanResult、WifiConfiguration、WifiInfo和WifiManager,详细讲解了各类的功能和常用方法,并提供了一个简单的Demo示例,演示了如何使用这些类进行Wifi管理。在实践中,需注意在AndroidManifest.xml中添加必要的权限。
摘要由CSDN通过智能技术生成

在Android中对Wifi操作,android本身提供了一些有用的包,在android.net.wifi包下面。简单介绍一下:

大致可以分为四个主要的类ScanResult,wifiConfiguration,WifiInfo,WifiManager

(1)ScanResult,主要是通过wifi 硬件的扫描来获取一些周边的wifi 热点的信息。

(2)wifiConfiguration 在我们连通一个wifi 接入点的时候,需要获取到的一些信息。大家可以跟我们有线的设备进行对比一下。

(3)WifiInfo 在我们的wifi 已经连通了以后,可以通过这个类获得一些已经连通的wifi 连接的信息获取当前链接的信息,这里信息就比较简单了,这里简单介绍一下这里的方法:

       getBSSID() 获取BSSID

       getDetailedStateOf() 获取客户端的连通性

       getHiddenSSID() 获得SSID 是否被隐藏

       getIpAddress() 获取IP 地址

       getLinkSpeed() 获得连接的速度

       getMacAddress() 获得Mac 地址

       getRssi() 获得802.11n 网络的信号

       getSSID() 获得SSID

       getSupplicanState() 返回具体客户端状态的信息

(4)wifiManager 这个不用说,就是用来管理我们的wifi 连接,这里已经定义好了一些类,可以供我们使用。这里来说相对复杂,里面的内容比较多,但是通过字面意思,我们还是可以获得很多相关的信息。这个类里面预先定义了许多常量,我们可以直接使用,不用再次创建。

介绍完了,开始写一个Demo吧。程序已启动效果图如下:

看代码,其中有注释,我就不多说了。

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/mScrollView" android:layout_width="fill_parent"  
    android:layout_height="wrap_content" android:scrollbars="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/allNetWork"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="当前没有扫描到Wifi网络"
    />
<Button
    android:id="@+id/scan"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="扫描网络"
    />
<Button
    android:id="@+id/start"
    android:layout_width="wrap_content" 
    android:layout_height=&#
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值