diff --git a/mirai-core-qqandroid/src/androidMain/kotlin/net/mamoe/mirai/qqandroid/utils/SystemDeviceInfo.kt b/mirai-core-qqandroid/src/androidMain/kotlin/net/mamoe/mirai/qqandroid/utils/SystemDeviceInfo.kt index 56013c9db..9c5cbb05f 100644 --- a/mirai-core-qqandroid/src/androidMain/kotlin/net/mamoe/mirai/qqandroid/utils/SystemDeviceInfo.kt +++ b/mirai-core-qqandroid/src/androidMain/kotlin/net/mamoe/mirai/qqandroid/utils/SystemDeviceInfo.kt @@ -1,6 +1,7 @@ package net.mamoe.mirai.qqandroid.utils import android.annotation.SuppressLint +import android.net.wifi.WifiManager import android.os.Build import android.telephony.TelephonyManager import kotlinx.io.core.toByteArray @@ -33,16 +34,15 @@ actual class SystemDeviceInfo actual constructor(context: Context) : DeviceInfo( override val fingerprint: ByteArray get() = Build.FINGERPRINT.toByteArray() override val procVersion: ByteArray - get() = File("/proc/version").useLines { it.firstOrNull() ?: "" }.toByteArray() + get() = kotlin.runCatching { File("/proc/version").useLines { it.firstOrNull() ?: "" }.toByteArray() }.getOrElse { byteArrayOf() } override val bootId: ByteArray get() = File("/proc/sys/kernel/random/boot_id").useLines { it.firstOrNull() ?: "" }.toByteArray() override val version: DeviceInfo.Version get() = Version override val simInfo: ByteArray - @SuppressLint("WrongConstant") get() { return kotlin.runCatching { - val telephonyManager = context.getSystemService("phone") as TelephonyManager + val telephonyManager = context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager if (telephonyManager.simState == 5) { telephonyManager.simOperatorName.toByteArray() } else byteArrayOf() @@ -52,26 +52,21 @@ actual class SystemDeviceInfo actual constructor(context: Context) : DeviceInfo( override val osType: ByteArray = "android".toByteArray() override val macAddress: ByteArray get() = "02:00:00:00:00:00".toByteArray() override val wifiBSSID: ByteArray? - get() = TODO("not implemented") + get() = kotlin.runCatching { + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.bssid.toByteArray() + }.getOrElse { byteArrayOf() } + override val wifiSSID: ByteArray? - get() = TODO("not implemented") - override val imsiMd5: ByteArray // null due to permission READ_PHONE_STATE required - get() = md5(byteArrayOf())/*{ - val telephonyManager = context.getSystemService("phone") as TelephonyManager - if (telephonyManager != null) { - val subscriberId = telephonyManager.subscriberId - if (subscriberId != null) { - return subscriberId.toByteArray() - } - } - return kotlin.runCatching { - val telephonyManager = context.getSystemService("phone") as TelephonyManager - if (telephonyManager != null) { - telephonyManager.subscriberId.toByteArray() - } else byteArrayOf() - }.getOrElse { byteArrayOf() } - }*/ - override val ipAddress: ByteArray get() = localIpAddress().toByteArray() + get() = kotlin.runCatching { + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.ssid.toByteArray() + }.getOrElse { byteArrayOf() } + + override val imsiMd5: ByteArray + @SuppressLint("HardwareIds") + get() = md5(kotlin.runCatching { + (context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).subscriberId.toByteArray() + }.getOrElse { byteArrayOf() }) + override val ipAddress: String get() = localIpAddress() override val androidId: ByteArray get() = Build.ID.toByteArray() override val apn: ByteArray get() = "wifi".toByteArray() diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/NetworkType.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/NetworkType.kt new file mode 100644 index 000000000..5cf54e6d6 --- /dev/null +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/NetworkType.kt @@ -0,0 +1,22 @@ +package net.mamoe.mirai.qqandroid.utils + +/** + * 连接类型 + */ +inline class NetworkType(val value: Int) { + companion object { + /** + * 移动网络 + */ + val MOBILE = NetworkType(1) + /** + * Wifi + */ + val WIFI = NetworkType(2) + + /** + * 其他任何类型 + */ + val OTHER = NetworkType(0) + } +} \ No newline at end of file diff --git a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsAndroid.kt b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsAndroid.kt index 390e92429..c4d80af76 100644 --- a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsAndroid.kt +++ b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsAndroid.kt @@ -28,7 +28,9 @@ actual val Http: HttpClient /** * Localhost 解析 */ -actual fun localIpAddress(): String = InetAddress.getLocalHost().hostAddress +actual fun localIpAddress(): String = runCatching { + InetAddress.getLocalHost().hostAddress +}.getOrElse { "192.168.1.123" } /** * MD5 算法