赞
踩
1. 简述
WiFi是Android手机重要功能,在设计过程中需要经常测试和比较WiFi信号、扫描到AP个数等。本实例利用Android框架的WiFiManager对象扫描AP列表,每个AP的信息包括SSID、BSSID、加密方式、信号等级、频率、带宽、时间戳等。说明一下,Android系统定义了带宽常数CHANNNEL_WIDTH_20MHZ=0; CHANNNEL_WIDTH_40MHZ=1;CHANNNEL_WIDTH_80MHZ=2;CHANNNEL_WIDTH_160MHZ=3;CHANNNEL_WIDTH_80MHZ_PLUS_MHZ=4,在扫描结果中用常数表示带宽。本实例有3个部分,UI线程主要设置用户界面显示AP列表、翻页处理、初始化SimpleAdapter、获得WiFiManager对象、开始扫描线程、注册广播接收器;定时扫描线程只负责扫描;广播接收器BroadcastReceiver mWifiStateReceiver,处理扫描返回数据,并在用户界面空闲时更新数据。程序源码如下:WiFi是Android手机重要功能,在设计过程中需要经常测试和比较WiFi信号、扫描到AP个数等。本实例利用Android框架的WiFiManager对象扫描AP列表,每个AP的信息包括SSID、BSSID、加密方式、信号等级、频率、带宽、时间戳等。说明一下,Android系统定义了带宽常数CHANNNEL_WIDTH_20MHZ=0; CHANNNEL_WIDTH_40MHZ=1;CHANNNEL_WIDTH_80MHZ=2;CHANNNEL_WIDTH_160MHZ=3;CHANNNEL_WIDTH_80MHZ_PLUS_MHZ=4,在扫描结果中用常数表示带宽。本实例有3个部分,UI线程主要设置用户界面显示AP列表、翻页处理、初始化SimpleAdapter、获得WiFiManager对象、开始扫描线程、注册广播接收器;定时扫描线程只负责扫描;广播接收器BroadcastReceiver mWifiStateReceiver,处理扫描返回数据,并在用户界面空闲时更新数据。程序源码如下:
2. WiFiAdmin.java
//列出手机扫描的WiFi AP,1秒更新一次。 package com.lmy.wifiadmin; import android.Manifest; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.AbsListView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import static java.lang.Thread.sleep; public class WiFiAdmin extends AppCompatActivity { //定义WifiManager对象 private WifiManager mWifiManager; private final static int REQUEST_LOCATION=100; private ListView lvWifiList; private TextView mytxv; private SimpleAdapter myadapter; private ArrayList<HashMap<String,Object>> data=new ArrayList<HashMap<String, java.lang.Object>>(); private int position; private int myscrollstate=AbsListView.OnScrollListener.SCROLL_STATE_IDLE; private IntentFilter mWifiStateFilter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); verifyCoarseLocationPermissions(WiFiAdmin.this); verifyFineLocationPermissions(WiFiAdmin.this); lvWifiList = (ListView) findViewById(R.id.lv); lvWifiList.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { myscrollstate = scrollState; if(scrollState==AbsListView.OnScrollListener.SCROLL_STATE_IDLE){ position = view.getFirstVisiblePosition(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } });
myadapter = new SimpleAdapter(WiFiAdmin.this, data, R.layout.oneline, new String[]{"no", "ap"}, new int[]{R.id.txvno, R.id.txvap}); mytxv=findViewById(R.id.txv); mWifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE); if(mWifiManager==null) return; if(!mWifiManager.isWifiEnabled()){ mWifiManager.setWifiEnabled(true); } mWifiStateFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); registerReceiver(mWifiStateReceiver, mWifiStateFilter); new Thread(new Runnable() { @Override public void run() { while(true){ mWifiManager.startScan(); try { sleep(1000); }catch (Exception e){ e.printStackTrace(); } } } }).start(); } private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { handleScanResultsAvailable(); } else { Log.e("WiFi", "Received an unknown Wifi Intent"); } } }; private void handleScanResultsAvailable() { int i; List<ScanResult> mWifiList = mWifiManager.getScanResults(); if(mWifiList!=null) { if (myscrollstate == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { data.clear(); for (i = 0; i < mWifiList.size(); i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("no", Integer.toString(i + 1)); map.put("ap", mWifiList.get(i).toString()); data.add(map); } mytxv.setText("Total AP:" + Integer.toString(mWifiList.size())); lvWifiList.setAdapter(myadapter); if (position < mWifiList.size()) lvWifiList.setSelection(position); } } } private void verifyCoarseLocationPermissions(Activity activity){ int permissionLocation = ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION); if (permissionLocation != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION} ,REQUEST_LOCATION); } } private void verifyFineLocationPermissions(Activity activity){ int permissionLocation = ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION); if (permissionLocation != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION} ,REQUEST_LOCATION); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case REQUEST_LOCATION: if ((grantResults.length>0)&&(grantResults[0]==PackageManager.PERMISSION_GRANTED)){ Toast.makeText(getApplicationContext(),permissions[0]+"权限申请成功",Toast.LENGTH_LONG).show(); }else{ Toast.makeText(getApplicationContext(),permissions[0]+"权限申请失败",Toast.LENGTH_LONG).show(); } break; } }
}
3. main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".WiFiAdmin"> <TextView android:id="@+id/txv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="48px" android:text="WiFi List" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
4.oneline.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txvno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/txvap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
5. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lmy.wifiadmin" > <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".WiFiAdmin" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。