Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data class TelephonyProvider(
- val name: String,
- val id: String,
- )
- fun Context.getTelephonyProvider(): TelephonyProvider {
- val telephonyManager = getSystemService(TelephonyManager::class.java)
- val operatorName = telephonyManager.networkOperatorName
- val operatorId = telephonyManager.networkOperator
- return TelephonyProvider(operatorName, operatorId)
- }
- suspend fun Context.getDeviceIpAddress(): String? = withContext(Dispatchers.IO) {
- val connectivityManager = getSystemService(ConnectivityManager::class.java)
- val props = connectivityManager.getLinkProperties(connectivityManager.activeNetwork) as LinkProperties
- props.linkAddresses.firstOrNull { linkAddress ->
- linkAddress.address.hostAddress?.contains('.') ?: false
- }?.address?.hostAddress
- }
- suspend fun getPublicIpAddress(): Deferred<String> =
- coroutineScope {
- async(Dispatchers.IO) {
- var result = ""
- result = try {
- val url = URL("https://api.ipify.org")
- val httpsURLConnection = url.openConnection()
- val iStream = httpsURLConnection.getInputStream()
- val buff = ByteArray(1024)
- val read = iStream.read(buff)
- String(buff, 0, read)
- } catch (e: Exception) {
- "error : $e"
- }
- return@async result
- }
- }
- data class Wifi(
- val name: String,
- val id: String,
- )
- fun Context.getWifiInfo(): Wifi {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
- val connectivityManager = getSystemService(ConnectivityManager::class.java)
- val wifiInfo = connectivityManager
- .getNetworkCapabilities(connectivityManager.activeNetwork)
- ?.transportInfo as WifiInfo
- return Wifi(
- name = wifiInfo.ssid,
- id = wifiInfo.networkId.toString(),
- )
- } else {
- val wifiManager =
- getSystemService(WifiManager::class.java)
- val wifiInfo = wifiManager!!.connectionInfo
- return Wifi(
- name = wifiInfo.ssid,
- id = wifiInfo.networkId.toString(),
- )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement