公司一些Android设备,在交给客户使用时,需要使用sim卡网络,但是有时候网络不好,要给客户证据说明是网络问题,一个办法是网络测速,还有就是获取一下频段信息,根据这个信息可以和中国移动沟通问题来源,比如在某些地方,移动只部署了一两个频段,而有的频段速度快,有的频段速度慢,所以可以通过频段信息判断网络问题。
查询结果示例:TDD B34(36275)
,这里显示频段为 TDD B34
频段,频点为36275
。示例代码如下(注:本代码在5G频段换算时仅写了中国移动N41、N79、N28频段,其它5G频段的没写),tm.serviceState
这个API是Android 8.0 才出的,在低版本的话也可以用反射试一下,看是否只是未公开,且这个API需要READ_PHONE_STATE
、ACCESS_FINE_LOCATION
权限。
fun getBandInfo() {
val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val serviceState = tm.serviceState ?: return
val channelNumber = serviceState.channelNumber
val duplexMode = serviceState.duplexMode
val band = getBand(channelNumber)
val mode = getMode(duplexMode)
Log.i("MainActivity","$mode $band($channelNumber)")
}
fun getMode(mode: Int) : String {
return when (mode) {
1 -> "FDD"
2 -> "TDD"
else -> "未知duplexMode: $mode"
}
}
fun getBand(EARFCN: Int): String {
var band = nrarfcnToBand(EARFCN)
if (band != -1) return "N$band"
band = earfcnToBand(EARFCN)
if (band != -1) return "B$band"
return "$EARFCN"
}
fun nrarfcnToBand(nrarfcn: Int): Int {
val band = when(nrarfcn) {
in 499200 .. 537999 -> 41
in 693334 .. 733334 -> 79
in 272010 ..276660 -> 28
else -> -1
}
return band
}
fun earfcnToBand(EARFCN: Int): Int {
val band = when (EARFCN) {
in 0 until 600, in 18000 until 18600 -> 1
in 600 until 1200, in 18600 until 19200 -> 2
in 1200 until 1950, in 19200 until 19950 -> 3
in 1950 until 2400, in 19950 until 20400 -> 4
in 2400 until 2650, in 20400 until 20650 -> 5
in 2650 until 2750, in 20650 until 20750 -> 6
in 2750 until 3450, in 20750 until 21450 -> 7
in 3450 until 3800, in 21450 until 21800 -> 8
in 3800 until 4149, in 21800 until 22149 -> 9
in 4150 until 4750, in 22150 until 22745 -> 10
in 4750 until 4950, in 22750 until 22950 -> 11
in 5010 until 5180, in 23010 until 23180 -> 12
in 5180 until 5280, in 23180 until 23280 -> 13
in 5280 until 5380, in 23280 until 23379 -> 14
in 5730 until 5850, in 23730 until 23850 -> 17
in 5850 until 6000, in 23850 until 24000 -> 18
in 6000 until 6150, in 24000 until 24150 -> 19
in 6150 until 6450, in 24150 until 24450 -> 20
in 6450 until 6600, in 24450 until 24600 -> 21
in 6600 until 7400, in 24600 until 25399 -> 22
in 7500 until 7700, in 25500 until 25699 -> 23
in 7700 until 8040, in 25700 until 26040 -> 24
in 8040 until 8690, in 26040 until 26690 -> 25
in 8690 until 9040, in 26690 until 27040 -> 26
in 9040 until 9209, in 27040 until 27210 -> 27
in 9210 until 9660, in 27210 until 27660 -> 28
in 36000 until 36200 -> 33
in 36200 until 36350 -> 34
in 36350 until 36950 -> 35
in 36950 until 37550 -> 36
in 37550 until 37750 -> 37
in 37750 until 38250 -> 38
in 38250 until 38650 -> 39
in 38650 until 39650 -> 40
in 39650 until 41590 -> 41
in 41590 until 43590 -> 42
in 43590 until 45590 -> 43
in 45590 until 46590 -> 44
else -> -1
}
return band
}