Android获取到手机信号强度信息后传回给unity

      之前做了一个在unity里面获取安卓手机信号强度的功能,看了很多帖子,大多数讲得很好,但是对于一窍不通的我来说,帖子还是有点不全,我不能按照帖子很顺利的做出来,经过一番尝试,最终还是做出了一个版本,顺便也记下来,免得下次爬坑!

      下面简单记下,首先在AndroidManifest.xml里面加入获取手机信号的权限:

Android获取到手机信号强度信息后传回给unity_第1张图片

接着在自己的类里判断是否有网络连接:

public class MainActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


// unity调用的函数
public String AndroidCall(String name) {
int level=0;
Send("Unity");
ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
   if (connectivityManager == null) {
    level=0;
} else {
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
if(networkInfo == null || !networkInfo.isAvailable())  
{  
    //当前有可用网络  
level=0;
}  
else   
{  
    //当前无可用网络  
MyGetGsmSignalStrength.getPhoneSigal(this);
level=MyGetGsmSignalStrength.getSignalPower();
}
}
return String.valueOf(level);
}


// 发送给Unity msg的函数
public void Send(String msg) {
System.out.println("tjg::java:" + msg);
// 调用名字叫myButton的物体上绑定的脚本里面名字叫CallBack的函数
UnityPlayer.UnitySendMessage("myButton", "CallBack", msg);
}


}

下面这个是监听手机信号强度改变的类:

public class MyGetGsmSignalStrength


{


/*
* This variables need to be global, so we can used them onResume and
* onPause method to

* stop the listener
*/
static int level=0;


public static void getPhoneSigal(Activity content) {
TelephonyManager phoneManager = (TelephonyManager) content
.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {


@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// TODO Auto-generated method stub
super.onSignalStrengthsChanged(signalStrength);
XMSignalStrenth signalPower =new  XMSignalStrenth(signalStrength);
int signal1=signalPower.getLevel();
level=signal1;
}
};
phoneManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);


}

public static  int getSignalPower()
{
return level;
}

这个是具体获取手机信号强度的类:
public class phoneSignalStrenth {

private SignalStrength m_SignalStrength;

private static final int SIGNAL_STRENGTH_NONE_OR_UNKNOWN = 0;
private static final int SIGNAL_STRENGTH_POOR = 1;
private static final int SIGNAL_STRENGTH_MODERATE = 2;
private static final int SIGNAL_STRENGTH_GOOD = 3;
private static final int SIGNAL_STRENGTH_GREAT = 4;


public phoneSignalStrenth(SignalStrength signalStrength) {
m_SignalStrength = signalStrength;
}
    /**
     * Get signal level as an int from 0..4
     *
     * @hide
     */
    public int getLevel() {
        int level;


        if (m_SignalStrength.isGsm()) {
            //level = getLteLevel();
            level = getGsmLevel();
            
        } else {
            int cdmaLevel = getCdmaLevel();
            int evdoLevel = getEvdoLevel();
            if (evdoLevel == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
                /* We don't know evdo, use cdma */
                level = getCdmaLevel();
            } else if (cdmaLevel == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
                /* We don't know cdma, use evdo */
                level = getEvdoLevel();
            } else {
                /* We know both, use the lowest level */
                level = cdmaLevel < evdoLevel ? cdmaLevel : evdoLevel;
            }
        }
        System.out.println("tjg333::java::level:" + level);
        return level;
    }
    
    /**
     * Get gsm as level 0..4
     *
     */
    private int getGsmLevel() {
        int level;


        // ASU ranges from 0 to 31 - TS 27.007 Sec 8.5
        // asu = 0 (-113dB or less) is very weak
        // signal, its better to show 0 bars to the user in such cases.
        // asu = 99 is a special case, where the signal strength is unknown.
        int asu = m_SignalStrength.getGsmSignalStrength();
        if (asu <= 2 || asu == 99) level = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
        else if (asu >= 12) level = SIGNAL_STRENGTH_GREAT;
        else if (asu >= 8)  level = SIGNAL_STRENGTH_GOOD;
        else if (asu >= 5)  level = SIGNAL_STRENGTH_MODERATE;
        else level = SIGNAL_STRENGTH_POOR;
        return level;
    }
    
    /**
     * Get cdma as level 0..4
     *
     */
    private int getCdmaLevel() {
        final int cdmaDbm = m_SignalStrength.getCdmaDbm();
        final int cdmaEcio = m_SignalStrength.getCdmaEcio();
        int levelDbm;
        int levelEcio;


        if (cdmaDbm >= -75) levelDbm = SIGNAL_STRENGTH_GREAT;
        else if (cdmaDbm >= -85) levelDbm = SIGNAL_STRENGTH_GOOD;
        else if (cdmaDbm >= -95) levelDbm = SIGNAL_STRENGTH_MODERATE;
        else if (cdmaDbm >= -100) levelDbm = SIGNAL_STRENGTH_POOR;
        else levelDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;


        // Ec/Io are in dB*10
        if (cdmaEcio >= -90) levelEcio = SIGNAL_STRENGTH_GREAT;
        else if (cdmaEcio >= -110) levelEcio = SIGNAL_STRENGTH_GOOD;
        else if (cdmaEcio >= -130) levelEcio = SIGNAL_STRENGTH_MODERATE;
        else if (cdmaEcio >= -150) levelEcio = SIGNAL_STRENGTH_POOR;
        else levelEcio = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;


        int level = (levelDbm < levelEcio) ? levelDbm : levelEcio;
        return level;
    }
    
    /**
     * Get Evdo as level 0..4
     *
     */
    private int getEvdoLevel() {
        int evdoDbm = m_SignalStrength.getEvdoDbm();
        int evdoSnr = m_SignalStrength.getEvdoSnr();
        int levelEvdoDbm;
        int levelEvdoSnr;


        if (evdoDbm >= -65) levelEvdoDbm = SIGNAL_STRENGTH_GREAT;
        else if (evdoDbm >= -75) levelEvdoDbm = SIGNAL_STRENGTH_GOOD;
        else if (evdoDbm >= -90) levelEvdoDbm = SIGNAL_STRENGTH_MODERATE;
        else if (evdoDbm >= -105) levelEvdoDbm = SIGNAL_STRENGTH_POOR;
        else levelEvdoDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;


        if (evdoSnr >= 7) levelEvdoSnr = SIGNAL_STRENGTH_GREAT;
        else if (evdoSnr >= 5) levelEvdoSnr = SIGNAL_STRENGTH_GOOD;
        else if (evdoSnr >= 3) levelEvdoSnr = SIGNAL_STRENGTH_MODERATE;
        else if (evdoSnr >= 1) levelEvdoSnr = SIGNAL_STRENGTH_POOR;
        else levelEvdoSnr = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;


        int level = (levelEvdoDbm < levelEvdoSnr) ? levelEvdoDbm : levelEvdoSnr;
        return level;
    }
}

小伙伴需要使用的话可以直接复制过去改一下自己的类名应该就可以啦,祝您一次成功!!!!!!

你可能感兴趣的:(Android获取到手机信号强度信息后传回给unity)