Android gets the current Wi-Fi Channel

Source: Internet
Author: User

Android gets the current Wi-Fi Channel

For Network diagnosis, You Need To diagnose whether the current Wi-Fi channel is congested. Currently, it only solves the problem of obtaining the current Wi-Fi channel, mainly using the frequency field of ScanResult, you also need to use the SSID and BSSID to match the current Wi-Fi signal wifiInfo from ScanResults. The specific code is as follows:


Public static int getCurrentChannel (Context context) {WifiManager wifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); WifiInfo wifiInfo = wifiManager. getConnectionInfo (); // List of current Wi-Fi connection information
 
  
ScanResults = wifiManager. getScanResults (); for (ScanResult result: scanResults) {if (result. BSSID. equalsIgnoreCase (wifiInfo. getBSSID () & result. SSID. equalsIgnoreCase (wifiInfo. getSSID (). substring (1, wifiInfo. getSSID (). length ()-1) {return getChannelByFrequency (result. frequency) ;}} return-1;}/*** obtain the channel by frequency ** @ param frequency * @ return */public static int getChannelByFrequency (int frequency) {int channel =-1; switch (frequency) {case 2412: channel = 1; break; case 2417: channel = 2; break; case 2422: channel = 3; break; case 2427: channel = 4; break; case 2432: channel = 5; break; case 2437: channel = 6; break; case 2442: channel = 7; break; case 2447: channel = 8; break; case 2452: channel = 9; break; case 2457: channel = 10; break; case 2462: channel = 11; break; case 2467: channel = 12; break; case 2472: channel = 13; break; case 2484: channel = 14; break; case 5745: channel = 149; break; case 5765: channel = 153; break; case 5785: channel = 157; break; case 5805: channel = 161; break; case 5825: channel = 165; break;} return channel ;}
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.