Guest User

Untitled

a guest
Dec 10th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. /**
  2. * Connects to the wifi access point at specified [ssid] with specified [networkId]
  3. * And returns the [WifiInfo] of the network that has been connected to
  4. */
  5. private fun connect(context: Context,
  6. wifiManager: WifiManager,
  7. ssid: String,
  8. networkId: Int) = Single.create<WifiInfo> { emitter ->
  9.  
  10. val wifiConnectionReceiver = object : BroadcastReceiver() {
  11. var oldSupplicantState: SupplicantState? = null
  12.  
  13. override fun onReceive(context: Context, intent: Intent) {
  14. if (intent.action == WifiManager.NETWORK_STATE_CHANGED_ACTION) {
  15. val networkInfo = intent.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO) ?: return
  16.  
  17. if (networkInfo.detailedState == NetworkInfo.DetailedState.DISCONNECTED) {
  18. context.applicationContext.unregisterReceiver(this)
  19. emitter.onError(WiFiException("Failed to connect to wifi network"))
  20. }
  21. else if (networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
  22. val wifiInfo = intent.getParcelableExtra<WifiInfo>(WifiManager.EXTRA_WIFI_INFO) ?: return
  23. if (ssid == wifiInfo.ssid.unescape()) {
  24. context.applicationContext.unregisterReceiver(this)
  25. emitter.onSuccess(wifiInfo)
  26. }
  27. }
  28. } else if (intent.action == WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) {
  29. val supplicantState = intent.getParcelableExtra<SupplicantState>(WifiManager.EXTRA_NEW_STATE)
  30. val oldSupplicantState = this.oldSupplicantState
  31. this.oldSupplicantState = supplicantState
  32.  
  33. if (supplicantState == SupplicantState.DISCONNECTED) {
  34. if (oldSupplicantState == null || oldSupplicantState == SupplicantState.COMPLETED) {
  35. return
  36. }
  37. val possibleError = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)
  38. if (possibleError == WifiManager.ERROR_AUTHENTICATING) {
  39. context.applicationContext.unregisterReceiver(this)
  40. emitter.onError(WiFiException("Wifi authentication failed"))
  41. }
  42. } else if (supplicantState == SupplicantState.SCANNING && oldSupplicantState == SupplicantState.DISCONNECTED) {
  43. context.applicationContext.unregisterReceiver(this)
  44. emitter.onError(WiFiException("Failed to connect to wifi network"))
  45. }
  46. }
  47. }
  48. }
  49.  
  50. val networkStateChangedFilter = IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)
  51. networkStateChangedFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)
  52.  
  53. context.applicationContext.registerReceiver(wifiConnectionReceiver, networkStateChangedFilter)
  54.  
  55. emitter.setCancellable {
  56. if (!emitter.isDisposed)
  57. context.applicationContext.unregisterReceiver(wifiConnectionReceiver)
  58. }
  59.  
  60. wifiManager.enableNetwork(networkId, true)
  61. }
Add Comment
Please, Sign In to add comment