Unity Wifi 통신사 전파 수준의 취득(Android)

8710 단어 AndroidUnity
■ Unity 쪽(WIFI는 Unity 쪽으로만 완료)
using UnityEngine;

/// <summary>
/// 電波状態の取得クラス(WIFI/キャリア)
/// </summary>
public static class NetworkStatus
{
    /// <summary>
    /// 電波強度MAX値
    /// </summary>
    public const int MAX_SIGANAL_STRENGTH = 4;

#if !UNITY_EDITOR && UNITY_ANDROID
    private static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    private static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
#endif

    /// <summary>
    /// 通信状態を取得
    /// </summary>
    /// <returns></returns>
    public static bool CanUseNetwork
    {
        get
        {
            // 通信可能ならTrue
            return Application.internetReachability != NetworkReachability.NotReachable;
        }
    }

    /// <summary>
    /// 通信状況を取得
    /// </summary>
    public static int Level
    {
        get
        {
            int level = 0;
            switch(Application.internetReachability)
            {
                // WIFI接続可能
                case NetworkReachability.ReachableViaLocalAreaNetwork:
                    level = WifiLevel;
                    break;
                // LTE/3G接続可能
                case NetworkReachability.ReachableViaCarrierDataNetwork:
                    //level = CarrierLevel;
                    break;
                default:
                    level = 0;
                    break;
            }
            return level;

        }
    }

    /// <summary>
    /// Wifiの電波レベルを取得して返す
    /// </summary>
    /// <returns></returns>
    private static int WifiLevel
    {
        get
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            var wifiManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "wifi");
            int rssi = wifiManager.Call<AndroidJavaObject>("getConnectionInfo").Call<int>("getRssi");
            return wifiManager.CallStatic<int>("calculateSignalLevel",rssi,5);
#endif
            return MAX_SIGANAL_STRENGTH;

        }
    }

}

운영자 등급을 취득하는 데 사용되는 등급 (GetCarrier.java)

package jp.co.test.mobilestate;


import android.telephony.CellInfo;
import android.content.Context;
import android.os.Bundle;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.util.List;
import com.unity3d.player.UnityPlayerActivity;
import static com.unity3d.player.UnityPlayer.UnitySendMessage;


public class GetCarrier extends UnityPlayerActivity  {

    TelephonyManager telephonyManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("TM", "onCreate");

        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    }

    @Override
    public void onResume()
    {
        super.onResume();

        Log.d("TM", "onResume");
        telephonyManager.listen(phoneState, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_CELL_LOCATION);

    }

    @Override
    public void onPause()
    {
        super.onPause();

        telephonyManager.listen(phoneState, PhoneStateListener.LISTEN_NONE);
        Log.d("TM", "onPause");
    }

    /**
     * 電波強度の変更を検出するListner
     */
    public PhoneStateListener phoneState;
    {
        phoneState = new PhoneStateListener() {
            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {

                Log.d("TM", "PhoneStateListener");

                List<CellInfo> cellInfoList;
                int cellSig,  cellLv = 0;

                try {
                    cellInfoList = telephonyManager.getAllCellInfo();
                    for (CellInfo cellInfo : cellInfoList)
                    {
                        if (cellInfo instanceof CellInfoLte)
                        {
                            Log.d("TM", "CellInfoLte");
                            // cast to CellInfoLte and call all the CellInfoLte methods you need
                            // gets RSRP cell signal strength:
                            cellSig = ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm();

                            Log.d("TM", "cellSig=" + String.valueOf(cellSig));
                            // Gets the LTE TAC: (returns 16-bit Tracking Area Code, Integer.MAX_VALUE if unknown)
                            cellLv = ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel();
                            Log.d("TM", "cellLv=" + String.valueOf(cellLv));

                            UnitySendMessage("GetSignal","onCallBack","CellInfoLte cellLv=" + String.valueOf(cellLv) + " getDbm=" + String.valueOf(cellSig));
                        }
                        if (cellInfo instanceof CellInfoGsm)
                        {
                            Log.d("TM", "CellInfoGsm");

                            cellSig = ((CellInfoGsm) cellInfo).getCellSignalStrength().getDbm();
                            Log.d("TM", "getDbm=" + String.valueOf(cellSig));

                            // Gets the LTE TAC: (returns 16-bit Tracking Area Code, Integer.MAX_VALUE if unknown)
                            cellLv = ((CellInfoGsm) cellInfo).getCellSignalStrength().getLevel();
                            Log.d("TM", "cellLv=" + String.valueOf(cellLv));
                            UnitySendMessage("GetSignal","onCallBack","CellInfoGsm cellLv=" + String.valueOf(cellLv) + " getDbm=" + String.valueOf(cellSig));
                        }

                        if (cellInfo instanceof CellInfoCdma)
                        {
                            Log.d("TM", "CellInfoCdma");

                            cellSig = ((CellInfoCdma) cellInfo).getCellSignalStrength().getDbm();
                            Log.d("TM", "getDbm=" + String.valueOf(cellSig));

                            // Gets the LTE TAC: (returns 16-bit Tracking Area Code, Integer.MAX_VALUE if unknown)
                            cellLv = ((CellInfoCdma) cellInfo).getCellSignalStrength().getLevel();
                            Log.d("TM", "cellLv=" + String.valueOf(cellLv));

                            UnitySendMessage("GetSignal","onCallBack","CellInfoCdma cellLv=" + String.valueOf(cellLv) + " getDbm=" + String.valueOf(cellSig));
                        }
                    }
                } catch (Exception e) {
                    Log.d("SignalStrength", "+++++++++++++++++++++++++++++++ null array spot 3: " + e);
                }

                super.onSignalStrengthsChanged(signalStrength);
            }

        };
    }
}

build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28



    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'], exclude: ['classes.jar'])
    compileOnly fileTree(dir: 'libs', include: ['classes.jar'])
}

■class.jar을 안드로이드 스튜디오에서 지정한libs 폴더 아래에 놓기
다음 경로에서
C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes
or
C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Development\Classes
■ aar 만드는 법
● 빨간환의 Sync를 만든 후
gradle 보기를 열고 assebleRelease를 실행합니다

■UnitySendMessage
첫 번째 파라미터는 게임 대상의 명칭이다
등급 제도의 이름과 일치해야 한다
줄곧 반 이름인 줄 알고 소박했다.

좋은 웹페이지 즐겨찾기