-
-
Save sc0rp10n-py/636ecc1d12cbaefea1b67f70074fd2b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package space.sc0rp10n.wifidirect | |
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import android.net.NetworkInfo | |
import android.net.wifi.p2p.WifiP2pManager | |
class WiFiDirectBroadcastReceiver( | |
private var manager: WifiP2pManager, | |
private var channel: WifiP2pManager.Channel, | |
private var activity: MainActivity | |
) : BroadcastReceiver() { | |
init { | |
this.manager = manager | |
this.channel = channel | |
this.activity = activity | |
} | |
override fun onReceive(context: Context?, intent: Intent?) { | |
val action: String? = intent!!.action | |
when (action) { | |
WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> { | |
// check to see if WiFi is enabled and notify appropriate activity | |
// val state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1) | |
// when (state) { | |
// WifiP2pManager.WIFI_P2P_STATE_ENABLED -> { | |
// Toast.makeText(context, "Wifi is ON", Toast.LENGTH_SHORT).show() | |
// } | |
// else -> { | |
// Toast.makeText(context, "Wifi is OFF", Toast.LENGTH_SHORT).show() | |
// } | |
// } | |
} | |
WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION -> { | |
// call WifiP2pManager.requestPeers() to get a list of current peers | |
if (manager != null) { | |
manager.requestPeers(channel, activity.peerListListener) | |
} | |
} | |
WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> { | |
// respond to new connection or disconnection | |
if (manager != null) { | |
val networkInfo: NetworkInfo? = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO) | |
if (networkInfo!!.isConnected()){ | |
manager.requestConnectionInfo(channel, activity.connectionInfoListener) | |
} else { | |
activity.connectionStatus.setText("Not Connected") | |
} | |
} | |
} | |
// WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> { | |
// (mActivity.supportFragmentManager.findFragmentById(R.id.frag_list) as DeviceListFragment) | |
// .apply { | |
// updateThisDevice( | |
// intent.getParcelableExtra( | |
// WifiP2pManager.EXTRA_WIFI_P2P_DEVICE) as WifiP2pDevice? | |
// ) | |
// } | |
// } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment