日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WifiManager详解

發布時間:2023/12/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WifiManager详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近被分到做無線wifi連接信息展示的模塊,保存下搜到的總結。
1.怎樣獲取wifi對象并進行操作
要操作WIFI設備,需要先獲取Context.getSystemService(Context.WIFI_SERVICE)來獲取WifiManager對象,并通過這個對象來管理WIFI設備。
?
addNetwork(WifiConfiguration config) 添加一個config描述的WIFI網絡,默認情況下,這個WIFI網絡是DISABLE狀態的。
calculateSignalLevel(int rssi , int numLevels) 計算信號的等級
compareSignalLevel(int rssiA, int rssiB) 對比網絡A和網絡B的信號強度
createWifiLock(int lockType, String tag) 創建一個WIFI 鎖,鎖定當前的WIFI連接
disableNetwork(int netId) 讓一個網絡連接失效
disconnect() 斷開當前的WIFI連接
enableNetwork(int netId, Boolean disableOthers) 連接netId所指的WIFI網絡,并是其他的網絡都被禁用
getConfiguredNetworks() 獲取網絡連接的狀態
getConnectionInfo() 獲取當前連接的信息
getDhcpInfo() 獲取DHCP 的信息
getScanResulats() 獲取掃描測試的結果
getWifiState() 獲取當前WIFI設備的狀態
isWifiEnabled() 判斷WIFI設備是否打開
pingSupplicant() ping操作,和PC的ping操作相同作用
ressociate() 重新連接WIFI網絡,即使該網絡是已經被連接上的
reconnect() 重新連接一個未連接上的WIFI網絡
removeNetwork() 移除某一個網絡
saveConfiguration() 保留一個配置信息
setWifiEnabled() 讓一個連接有效
startScan() 開始掃描
updateNetwork(WifiConfiguration config) 更新一個網絡連接
2.常用的wifi狀態
WIFI_STATE_DISABLED WIFI網卡不可用?
WIFI_STATE_DISABLING WIFI網卡正在關閉?
WIFI_STATE_ENABLED WIFI網卡可用?
WIFI_STATE_ENABLING WIFI網卡正在打開?
WIFI_STATE_UNKNOWN WIFI網卡狀態不可知
3.列表查看有連接信號的wifi熱點
ScanResult對象就是用來表示附近wifi熱點的屬性的,可以通過WifiManager.getScanResults()返回一個ScanResult列表,后面我附上查看附近wifi熱點的demo,ScanResult的重要屬性有一下幾個:
BSSID 接入點的地址
SSID 網絡的名字,唯一區別WIFI網絡的名字
Capabilities 網絡接入的性能
Frequency 當前WIFI設備附近熱點的頻率(MHz)
Level 所發現的WIFI網絡信號強度
4.連接wifi熱點
通過WifiManager.getConfiguredNetworks()方法會返回WifiConfiguration對象的列表,然后再調用WifiManager.enableNetwork();方法就可以連接上指定的熱點。
5.查看已經連接上的wifi信息
WifiInfo是專門用來表示連接的對象,這個對象可以通過WifiManager.getConnectionInfo()來獲取。WifiInfo中包含了當前連接中的相關信息。
?
getBSSID() 獲取BSSID屬性
getDetailedStateOf() 獲取客戶端的連通性
getHiddenSSID() 獲取SSID 是否被隱藏
getIpAddress() 獲取IP 地址
getLinkSpeed() 獲取連接的速度
getMacAddress() 獲取Mac 地址
getRssi() 獲取802.11n 網絡的信號
getSSID() 獲取SSID
getSupplicanState() 獲取具體客戶端狀態的信息
在wifi操作中常用的類和方法就這些,下面給出一個列表顯示wifi熱點的demo。
?
1.activity的布局很簡單就是一個ListView,activity_wifi_list.xml內容如下:
[html]??
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"??
? ? xmlns:tools="http://schemas.android.com/tools"??
? ? android:layout_width="match_parent"??
? ? android:layout_height="match_parent"??
? ? android:paddingBottom="@dimen/activity_vertical_margin"??
? ? android:paddingLeft="@dimen/activity_horizontal_margin"??
? ? android:paddingRight="@dimen/activity_horizontal_margin"??
? ? android:paddingTop="@dimen/activity_vertical_margin"??
? ? tools:context=".WifiListActivity" >??
??
? ? <ListView??
? ? ? ? android:id="@+id/listView"??
? ? ? ? android:layout_width="match_parent"??
? ? ? ? android:layout_height="wrap_content" >??
? ? </ListView>??
??
</RelativeLayout>??
2.ListView的item布局文件item_wifi_list.xml內容如下:
[html]?
<?xml version="1.0" encoding="utf-8"?>??
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"??
? ? android:layout_width="match_parent"??
? ? android:layout_height="match_parent" >??
??
? ? <ImageView??
? ? ? ? android:id="@+id/imageView"??
? ? ? ? android:layout_width="wrap_content"??
? ? ? ? android:layout_height="wrap_content"??
? ? ? ? android:layout_alignParentLeft="true"??
? ? ? ? android:layout_alignParentTop="true"??
? ? ? ? android:src="@drawable/ic_launcher" />??
??
? ? <TextView??
? ? ? ? android:id="@+id/textView"??
? ? ? ? android:layout_width="wrap_content"??
? ? ? ? android:layout_height="wrap_content"??
? ? ? ? android:layout_alignBottom="@+id/imageView"??
? ? ? ? android:layout_marginBottom="14dp"??
? ? ? ? android:layout_toRightOf="@+id/imageView"??
? ? ? ? android:text="TextView" />??
??
? ? <TextView??
? ? ? ? android:id="@+id/signal_strenth"??
? ? ? ? android:layout_width="wrap_content"??
? ? ? ? android:layout_height="wrap_content"??
? ? ? ? android:layout_alignBaseline="@+id/textView"??
? ? ? ? android:layout_alignBottom="@+id/textView"??
? ? ? ? android:layout_alignParentRight="true"??
? ? ? ? android:text="TextView" />??
??
</RelativeLayout>??
3.下面就activity的代碼了,這里需要自己自定義列表。
[java]??
public class WifiListActivity extends Activity {??
??
? ? private WifiManager wifiManager;??
? ? List<ScanResult> list;??
? ? @Override??
? ? protected void onCreate(Bundle savedInstanceState) {??
? ? ? ? super.onCreate(savedInstanceState);??
? ? ? ? setContentView(R.layout.activity_wifi_list);??
? ? ? ? init();??
? ? }??
??
? ? private void init() {??
? ? ? ? wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);??
? ? ? ? openWifi();??
? ? ? ? list = wifiManager.getScanResults();??
? ? ? ? ListView listView = (ListView) findViewById(R.id.listView);??
? ? ? ? if (list == null) {??
? ? ? ? ? ? Toast.makeText(this, "wifi未打開!", Toast.LENGTH_LONG).show();??
? ? ? ? }else {??
? ? ? ? ? ? listView.setAdapter(new MyAdapter(this,list));??
? ? ? ? }??
? ? ? ? ??
? ? }??
? ? ??
? ? /**?
? ? ?*? 打開WIFI?
? ? ?*/??
? ? private void openWifi() {??
? ? ? ?if (!wifiManager.isWifiEnabled()) {??
? ? ? ? wifiManager.setWifiEnabled(true);??
? ? ? ?}??
? ? ? ??
? ? }??
??
? ? public class MyAdapter extends BaseAdapter {??
??
? ? ? ? LayoutInflater inflater;??
? ? ? ? List<ScanResult> list;??
? ? ? ? public MyAdapter(Context context, List<ScanResult> list) {??
? ? ? ? ? ? // TODO Auto-generated constructor stub??
? ? ? ? ? ? this.inflater = LayoutInflater.from(context);??
? ? ? ? ? ? this.list = list;??
? ? ? ? }??
??
? ? ? ? @Override??
? ? ? ? public int getCount() {??
? ? ? ? ? ? // TODO Auto-generated method stub??
? ? ? ? ? ? return list.size();??
? ? ? ? }??
??
? ? ? ? @Override??
? ? ? ? public Object getItem(int position) {??
? ? ? ? ? ? // TODO Auto-generated method stub??
? ? ? ? ? ? return position;??
? ? ? ? }??
??
? ? ? ? @Override??
? ? ? ? public long getItemId(int position) {??
? ? ? ? ? ? // TODO Auto-generated method stub??
? ? ? ? ? ? return position;??
? ? ? ? }??
??
? ? ? ? @Override??
? ? ? ? public View getView(int position, View convertView, ViewGroup parent) {??
? ? ? ? ? ? // TODO Auto-generated method stub??
? ? ? ? ? ? View view = null;??
? ? ? ? ? ? view = inflater.inflate(R.layout.item_wifi_list, null);??
? ? ? ? ? ? ScanResult scanResult = list.get(position);??
? ? ? ? ? ? TextView textView = (TextView) view.findViewById(R.id.textView);??
? ? ? ? ? ? textView.setText(scanResult.SSID);??
? ? ? ? ? ? TextView signalStrenth = (TextView) view.findViewById(R.id.signal_strenth);??
? ? ? ? ? ? signalStrenth.setText(String.valueOf(Math.abs(scanResult.level)));??
? ? ? ? ? ? ImageView imageView = (ImageView) view.findViewById(R.id.imageView);??
? ? ? ? ? ? //判斷信號強度,顯示對應的指示圖標??
? ? ? ? ? ? if (Math.abs(scanResult.level) > 100) {??
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_0));??
? ? ? ? ? ? } else if (Math.abs(scanResult.level) > 80) {??
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_1));??
? ? ? ? ? ? } else if (Math.abs(scanResult.level) > 70) {??
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_1));??
? ? ? ? ? ? } else if (Math.abs(scanResult.level) > 60) {??
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_2));??
? ? ? ? ? ? } else if (Math.abs(scanResult.level) > 50) {??
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_3));??
? ? ? ? ? ? } else {??
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_4));??
? ? ? ? ? ? }??
? ? ? ? ? ? return view;??
? ? ? ? }??
??
? ? }??
??
}??
程序運行效果圖如下:

總結

以上是生活随笔為你收集整理的WifiManager详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。