Android市场上有一款叫Wifijumper的软件,实现相同ssid的多个AP之间根据wifi信号的强弱与阀值进行判断,实现自动切换AP的功能。目前在android 4.2之前系统都没有该功能,对于google来讲,这是个相当简单的问题,不明白为什么一直都不支持该功能。鄙人之前在某个方案公司就遇到过客户需要该功能。以下是鄙人实现的具体过程,希望对大家有些许的帮助。
首先我们必须时刻监听当前wifi的信号强度,那么我们的手机连上wifi之后状态兰就会有wifi图标出来,并且信号强度变化信号格也要跟随变化,这部分工作是在SystemUI完成的。在目录framework/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java这个类就是SystemUI监听网络连接状态的Receiver。
在OnReceive方法中“
if (action.equals(WifiManager.RSSI_CHANGED_ACTION)
|| action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)
|| action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
updateWifiState(intent);
refreshViews();
}
调用了 updateWifiState(Intent intent)方法,那么我们的切换功能就可以放到这个updateWifiState方法中:
mWifiRssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200);
mWifiLevel = WifiManager.calculateSignalLevel(
mWifiRssi, WifiIcons.WIFI_LEVEL_COUNT);
+
+ if (mWifiConnected) { //判断是否已链接wifi
+ WifiInfo wifiInfo = ((WifiManager) mContext
+ .getSystemService(Context.WIFI_SERVICE))
+ .getConnectionInfo();
+ //----
+ if(wifiInfo != null && !isWorking){//当前wifi必须是能工作的
+ String curentSSID = wifiInfo.getSSID();//获取当前wifi的ssid
+ String currentBssid = wifiInfo.getBSSID();
+ Xlog.e(TAG,"updateWifiState curentSSID == "+curentSSID+" currentBssid = "+currentBssid+" mWifiRssi = "+mWifiRssi+" mWifiLevel = "+mWifiLevel );
+ List<ScanResult> sameSSIDList = new ArrayList<ScanResult>();
+ List<ScanResult> list = mWifiManager.getScanResults();//获取当前扫描到的wifi列表
+ if(list != null){
+ for(ScanResult rt : list){
+ Xlog.e(TAG, "list ---------- onReceive():ScanResult = "+ rt);
+ if(curentSSID.equals(rt.SSID)){//从wifi列表中获取具有相同ssid的ap
+ sameSSIDList.add(rt);
+ }
+ }
+ }
+ if(sameSSIDList.size() >= 2){//判断列表中存在相同ssid的ap,至少有2个
+ ScanResult strongestRssi = sameSSIDList.get(0);
+ for(int i = 1; i <= sameSSIDList.size()-1;i++){
+ if(sameSSIDList.get(i).level > strongestRssi.level){//获取信号最强的那个ap
+ strongestRssi = sameSSIDList.get(i);
+ }
+ }
+ //int strongestLevel = WifiManager.calculateSignalLevel(
+ // strongestRssi.level, WifiIcons.WIFI_LEVEL_COUNT);
+ Xlog.e(TAG, "strongestRssi = "+ strongestRssi +" mWifiRssi = "+mWifiRssi);
+ //if(!currentBssid.equals(strongestRssi.BSSID) && (strongestLevel - mWifiLevel) >= 1){
+ if((strongestRssi.level - mWifiRssi) >= 18){//这个18是一个信号阀值
+ Xlog.e(TAG,"do reconnect now !!!!!!!!!!!!");
+ //count++;
+ //if(count > 1){
+ mWifiManager.disconnect();//先断开当前wifi,再重新连接。这样连接成功之后就是连接到了信号最强的那个ap了
+ mWifiManager.reconnect();
+ isWorking = true;
+ //count = 0;
+ //}
+ }
+ //else{
+ // count = 0;
+ //}
+ }
+ }
+ //----
+ }
+
}