LTE基站检测MNC android

问题描述

我正在创建一个Android应用程序(使用Kotlin),该应用程序提取周围LTE基站的信息。我想知道如何获取移动设备检测到的 ALL 基站的MNC。我只获得所连接站的MNC。 我可以提出要求吗?我一直在寻找下一个主题数周,但找不到解决方案。可能是因为如果您未连接到运营商,他们将不会提供该信息。我不知道。 :(

在这里,我附上我的服务活动代码获取LTE站的信息。

package com.mobilenet.monitoring.services

import android.Manifest
import android.annotation.SuppressLint
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Binder
import android.os.Handler
import android.os.IBinder
import android.telephony.*
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.mobilenet.monitoring.activities.SignalStrengthDetectorActivity
import com.mobilenet.monitoring.app.preferences
import org.json.JSONObject
import java.lang.Integer.parseInt


class ServiceNeighbourLte : Service() {

    val TAG = "TESTING"
    private var mBinder: IBinder = mybinder()
    private var mHandler = Handler()
    private lateinit var mContext:Context
    /* Lte Parameters   */
    var mSignalStrength: SignalStrength? = null
    var mListSignalStrength: List<CellSignalStrength>? = null
    var mSignalStrengthLte: CellSignalStrengthLte? = null
    var mManager: TelephonyManager? = null

    /* LteConnection   */
    private var mCellLocation: CellLocation? = null
    private var mDone = false

    private var mNeighbourLteData:JSONObject = JSONObject()

    override fun onBind(intent: Intent): IBinder? {
        return mBinder
    }

    @SuppressLint("MissingPermission")
    override fun onCreate() {
        super.onCreate()
        Log.d(TAG,"onCreate")
        mContext = this
        /* LTE */
        mManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        mManager!!.listen( mListener,PhonestateListener.LISTEN_SIGNAL_STRENGTHS or PhonestateListener.LISTEN_CELL_LOCATION)
    }

    override fun onStartCommand(intent: Intent?,flags: Int,startId: Int): Int {
        Log.d(TAG,"onStartCommand")
        if(intent?.getStringExtra("action") == "Destroy"){
            onDestroy()
        }
        return super.onStartCommand(intent,flags,startId)
    }

    fun getNeighbourLteData(): JSONObject {
        return mNeighbourLteData
    }

    // Listener for signal strength.
    val mListener: PhonestateListener = object : PhonestateListener() {
        override fun onCellLocationChanged(mLocation: CellLocation) {
            if (mDone) return
            Log.d(SignalStrengthDetectorActivity.TAG,"Cell location obtained.")
            mCellLocation = mLocation
            update()
        }

        override fun onSignalStrengthsChanged(sstrength: SignalStrength) {
            if (mDone) return
            Log.d(SignalStrengthDetectorActivity.TAG,"Signal strength obtained.")
            mSignalStrength = sstrength
            update()
        }
    }

    private fun update() {

        if (mSignalStrength == null || mCellLocation == null) return

        val runnable = object : Runnable {
            @SuppressLint("MissingPermission")
            override fun run() {
                var dataneighbour = HashMap< String,String>()
                var jsonObject = JSONObject()

                if (ActivityCompat.checkSelfPermission(mContext,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    val infos = mManager!!.allCellInfo

                    if (preferences.NeighbourLteEnable == true){
                        jsonObject.put("NumberOfSites","${Integer.toString(infos.size)}")
                    }else{
                        return
                    }

                    for (i in infos.indices) {
                        try {
                            val info = infos[i]
                            if (info is CellInfoLte)  //if LTE connection
                            {
                                mManager!!.getNetworkOperator()

                                val lte = info.cellSignalStrength
                                val identityLte = info.cellIdentity
                                var ci = identityLte.ci
                                var MCC = identityLte.mccString
                                var MNC = identityLte.mncString
                                var PLMN = ""
                                if(MNC == null || MCC == null) PLMN = "XXXX"
                                else if (MNC.length == 3) {
                                        PLMN = "${MCC[1]}${MCC[0]}${MNC[2]}${MCC[2]}${MNC[1]}${MNC[0]}"
                                    } else {
                                        PLMN = "${MCC[1]}${MCC[0]}F${MCC[2]}${MNC[1]}${MNC[0]}"
                                    }
                                val cellidHex: String = String.format("%x",ci)
                                val eNBHex = cellidHex.substring(0,cellidHex.length - 2)
                                val eNB: Int = parseInt(eNBHex,16)
                                //ECI = 256 * eNBId + Cellid

                                dataneighbour["Registered"] = "${info.isRegistered()}"
                                if (preferences.NeighbourLteRsrp == true)                   dataneighbour["RSRP"]   = "${lte.rsrp}"
                                if (preferences.NeighbourLteRSSi == true)                   dataneighbour["RSSI"]   = "${lte.RSSi}"
                                if (preferences.NeighbourLteCqi == true)                    dataneighbour["CQI"]    = "${lte.cqi}"
                                if (preferences.NeighbourLteDbm == true)                    dataneighbour["Dbm"]    = "${lte.dbm}"
                                if (preferences.NeighbourLteLevel == true)                  dataneighbour["LCID"]   = "${lte.level}"
                                if (preferences.NeighbourLteRsrq == true)                   dataneighbour["RSRQ"]   = "${lte.rsrq}"
                                if (preferences.NeighbourLteRSSnr == true)                  dataneighbour["RSSNR"]  = "${lte.RSSnr}"

                                if (preferences.NeighbourLtePci == true)                    dataneighbour["PCI"]    = "${identityLte.pci}"
                                if (preferences.NeighbourLteCi == true)                     dataneighbour["CI"]     = "${ci}"
                                if (preferences.NeighbourLteMnc == true)                    dataneighbour["MNC"]    = "${MNC}"
                                if (preferences.NeighbourLteMcc == true)                    dataneighbour["MCC"]    = "${MCC}"
                                if (PLMN != null)                                           dataneighbour["PLMN"]   = "${PLMN}"
                                                                                            dataneighbour["eNB"]    = "${eNB}"
                                if (preferences.NeighbourLteMobileNetworkOperator == true)  dataneighbour["MobileNetworkOperatorID"]    = "${identityLte.mobileNetworkOperator}"
                                if (preferences.NeighbourLteMobileNetworkOperator == true)  dataneighbour["MobileNetworkOperatorName"]  = "${identityLte.operatorAlphaLong}"
                                if (preferences.NeighbourLteTac == true)                    dataneighbour["TAC"]    = "${identityLte.tac}"
                                if (preferences.NeighbourLteBandwith == true)               dataneighbour["Bandwidth"] = "${identityLte.bandwidth}"
                                if (preferences.NeighbourLteEarfcn == true){
                                    dataneighbour["EarFCN"] =  "${identityLte.earfcn}"
                                    dataneighbour["Band"] =  "${getBand(identityLte.earfcn)}"
                                }
                            }
                        } catch (ex: Exception) {
                            Log.i("TESTING: ",ex.message)
                        }
                        jsonObject.putopt("Site$i",JSONObject(dataneighbour as Map<*,*>))
                    }
                }
                mNeighbourLteData = jsonObject
                mHandler.postDelayed(this,preferences.periodicity.toLong())
            }
        }
        mHandler.postDelayed(runnable,preferences.periodicity.toLong())
    }

    private fun getBand(arfcn:Int):Int{
        if(arfcn<600) return 1
        else if(arfcn<1200) return 2
        else if(arfcn<1950) return 3
        else if(arfcn<2400) return 4
        else if(arfcn<2650) return 5
        else if(arfcn<2750) return 6
        else if(arfcn<3450) return 7
        else if(arfcn<3800) return 8
        else if(arfcn<4150) return 9
        else if(arfcn<4750) return 10
        else if(arfcn<5010) return 11
        else if(arfcn<5180) return 12
        else if(arfcn<5280) return 13
        else if(arfcn<5380) return 14
        else if(arfcn<5730) return -1
        else if(arfcn<5850) return 17
        else if(arfcn<6000) return 18
        else if(arfcn<6150) return 19
        else if(arfcn<6450) return 20
        else if(arfcn<6600) return 21
        else if(arfcn<7500) return 22
        else if(arfcn<7700) return 23
        else if(arfcn<8040) return 24
        else if(arfcn<8690) return 25
        else if(arfcn<9040) return 26
        else if(arfcn<9210) return 27
        else if(arfcn<9660) return 28
        else if(arfcn<9770) return 29
        else if(arfcn<9870) return 30
        else if(arfcn<9920) return 31
        else return 9999 //Ya paso de seguir haciendo la tabla https://5g-tools.com/4g-lte-earfcn-calculator/
    }


    private fun complete() {
        mDone = true
        try {
            // Stop listening.
            mManager!!.listen(mListener,PhonestateListener.LISTEN_NONE)
            Toast.makeText(applicationContext,"Done",Toast.LENGTH_SHORT).show()
        } catch (e: java.lang.Exception) {
            Log.e(SignalStrengthDetectorActivity.TAG,"ERROR!!!",e)
        }
    }

    override fun onTaskRemoved(rootIntent: Intent?) {
        super.onTaskRemoved(rootIntent)
        stopSelf() //a hard stop for the service (si no queremos que siga en funcionamiento al cerrar)
    }

    override fun onDestroy() {
        Log.d(TAG,"onDestroy")
        super.onDestroy()
    }

    inner class mybinder : Binder() {
        fun getService(): ServiceNeighbourLte? {
            return this@ServiceNeighbourLte
        }
    }
}


我还包括清单,所有权限都被授予

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mobilenet.monitoring">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUetoOTH" />
    <uses-permission android:name="android.permission.BLUetoOTH_ADMIN" />

    <uses-feature android:name="android.hardware.usb.host" />
    <uses-feature android:name="android.hardware.wifi.rtt" />
    <uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="true" />

    <application
        android:name=".app.MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/mobilenet"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/mobilenet_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activities.UsbJavaActivity"></activity>
        <activity android:name=".activities.UsbActivity">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>
        </activity>

        <service
            android:name=".services.ServicePostData"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceLte"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceNeighbourLte"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceBluetoothList"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceWifiList"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceOperator"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceMyWifi"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceNetworkLocation"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.ServiceGps"
            android:enabled="true"
            android:exported="true" />

        <activity android:name=".activities.DetectingMeasurementActivity2" />
        <activity android:name=".activities.ThreadActivity" />
        <activity android:name=".activities.ConfigurationActivity" />
        <activity android:name=".model.ControlActivity" />
        <activity android:name=".activities.BluetoothActivity" />
        <activity android:name=".activities.LteInfoActivity" />
        <activity android:name=".activities.SignalStrengthDetectorActivity" />
        <activity android:name=".activities.GpsActivity" />
        <activity android:name=".activities.WiFiInfoActivity" />
        <activity android:name=".activities.OperatorInfoActivity" />
        <activity android:name=".activities.SelectMeasurementActivity" />
        <activity android:name=".activities.StartMeasurementsActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我希望任何人都可以继续前进,因为我被困住了。

在此先感谢您的帮助 卡洛斯

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)