android wifi讲解 wifi列表显示
生活随笔
收集整理的這篇文章主要介紹了
android wifi讲解 wifi列表显示
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(以下為JAVA ANDROID的相關(guān)操作,XAMARIN中需要進(jìn)行相應(yīng)的變更)?
最近項(xiàng)目中用到了wifi模塊,今天做一個(gè)簡(jiǎn)單的總結(jié)。
1.怎樣獲取wifi對(duì)象并進(jìn)行操作
要操作WIFI設(shè)備,需要先獲取Context.getSystemService(Context.WIFI_SERVICE)來獲取WifiManager對(duì)象,并通過這個(gè)對(duì)象來管理WIFI設(shè)備。 addNetwork(WifiConfiguration config) 添加一個(gè)config描述的WIFI網(wǎng)絡(luò),默認(rèn)情況下,這個(gè)WIFI網(wǎng)絡(luò)是DISABLE狀態(tài)的。 calculateSignalLevel(int rssi , int numLevels) 計(jì)算信號(hào)的等級(jí) compareSignalLevel(int rssiA, int rssiB) 對(duì)比網(wǎng)絡(luò)A和網(wǎng)絡(luò)B的信號(hào)強(qiáng)度 createWifiLock(int lockType, String tag) 創(chuàng)建一個(gè)WIFI 鎖,鎖定當(dāng)前的WIFI連接
disableNetwork(int netId) 讓一個(gè)網(wǎng)絡(luò)連接失效
disconnect() 斷開當(dāng)前的WIFI連接 enableNetwork(int netId, Boolean disableOthers) 連接netId所指的WIFI網(wǎng)絡(luò),并是其他的網(wǎng)絡(luò)都被禁用 getConfiguredNetworks() 獲取網(wǎng)絡(luò)連接的狀態(tài) getConnectionInfo() 獲取當(dāng)前連接的信息 getDhcpInfo() 獲取DHCP 的信息 getScanResulats() 獲取掃描測(cè)試的結(jié)果 getWifiState() 獲取當(dāng)前WIFI設(shè)備的狀態(tài) isWifiEnabled() 判斷WIFI設(shè)備是否打開 pingSupplicant() ping操作,和PC的ping操作相同作用 ressociate() 重新連接WIFI網(wǎng)絡(luò),即使該網(wǎng)絡(luò)是已經(jīng)被連接上的 reconnect() 重新連接一個(gè)未連接上的WIFI網(wǎng)絡(luò) removeNetwork() 移除某一個(gè)網(wǎng)絡(luò) saveConfiguration() 保留一個(gè)配置信息 setWifiEnabled() 讓一個(gè)連接有效 startScan() 開始掃描 updateNetwork(WifiConfiguration config) 更新一個(gè)網(wǎng)絡(luò)連接 2.常用的wifi狀態(tài) WIFI_STATE_DISABLED WIFI網(wǎng)卡不可用? WIFI_STATE_DISABLING WIFI網(wǎng)卡正在關(guān)閉? WIFI_STATE_ENABLED WIFI網(wǎng)卡可用? WIFI_STATE_ENABLING WIFI網(wǎng)卡正在打開? WIFI_STATE_UNKNOWN WIFI網(wǎng)卡狀態(tài)不可知 3.列表查看有連接信號(hào)的wifi熱點(diǎn) ScanResult對(duì)象就是用來表示附近wifi熱點(diǎn)的屬性的,可以通過WifiManager.getScanResults()返回一個(gè)ScanResult列表,后面我附上查看附近wifi熱點(diǎn)的demo,ScanResult的重要屬性有一下幾個(gè): BSSID 接入點(diǎn)的地址 SSID 網(wǎng)絡(luò)的名字,唯一區(qū)別WIFI網(wǎng)絡(luò)的名字 Capabilities 網(wǎng)絡(luò)接入的性能 Frequency 當(dāng)前WIFI設(shè)備附近熱點(diǎn)的頻率(MHz) Level 所發(fā)現(xiàn)的WIFI網(wǎng)絡(luò)信號(hào)強(qiáng)度 4.連接wifi熱點(diǎn) 通過WifiManager.getConfiguredNetworks()方法會(huì)返回WifiConfiguration對(duì)象的列表,然后再調(diào)用WifiManager.enableNetwork();方法就可以連接上指定的熱點(diǎn)。 5.查看已經(jīng)連接上的wifi信息 WifiInfo是專門用來表示連接的對(duì)象,這個(gè)對(duì)象可以通過WifiManager.getConnectionInfo()來獲取。WifiInfo中包含了當(dāng)前連接中的相關(guān)信息。 getBSSID() 獲取BSSID屬性 getDetailedStateOf() 獲取客戶端的連通性 getHiddenSSID() 獲取SSID 是否被隱藏 getIpAddress() 獲取IP 地址 getLinkSpeed() 獲取連接的速度 getMacAddress() 獲取Mac 地址 getRssi() 獲取802.11n 網(wǎng)絡(luò)的信號(hào) getSSID() 獲取SSID getSupplicanState() 獲取具體客戶端狀態(tài)的信息 在wifi操作中常用的類和方法就這些,下面給出一個(gè)列表顯示wifi熱點(diǎn)的demo。 1.activity的布局很簡(jiǎn)單就是一個(gè)ListView,activity_wifi_list.xml內(nèi)容如下: [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內(nèi)容如下: [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); ? //判斷信號(hào)強(qiáng)度,顯示對(duì)應(yīng)的指示圖標(biāo) ? 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; ? } ? } ? }總結(jié)
以上是生活随笔為你收集整理的android wifi讲解 wifi列表显示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机埃尼阿克的主要元件采用的是,计算机
- 下一篇: 机器学习分类器——案例(opencv s