123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package de.tudarmstadt.informatik.hostage.sync.wifi_direct;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.net.NetworkInfo;
- import android.net.wifi.WpsInfo;
- import android.net.wifi.p2p.WifiP2pConfig;
- import android.net.wifi.p2p.WifiP2pDevice;
- import android.net.wifi.p2p.WifiP2pDeviceList;
- import android.net.wifi.p2p.WifiP2pInfo;
- import android.net.wifi.p2p.WifiP2pManager;
- import android.util.Log;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Julien on 07.01.2015.
- */
- public class WiFiP2pBroadcastReceiver extends BroadcastReceiver implements WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener {
- /**
- * This listener will inform about any wifi direct change.
- */
- public interface WiFiP2pBroadcastListener {
- public void discoveredDevices(List<WifiP2pDevice> peers);
- public void wifiP2pIsEnabled(boolean enabled);
- public void didConnect(boolean isHost, WifiP2pInfo connectionInfo);
- public void failedToConnect();
- public void didDisconnect();
- public void failedToDisconnect();
- public void ownDeviceInformationIsUpdated(WifiP2pDevice device);
- }
- private WifiP2pManager manager;
- private WifiP2pManager.Channel channel;
- private WifiP2pDevice ownDevice;
- private android.net.NetworkInfo.DetailedState networkState = null;
- static boolean setIsWifiP2pEnabled;
- static boolean isConnected = false;
- private WiFiP2pBroadcastListener eventListener;
- /**
- * @param manager WifiP2pManager system service
- * @param channel Wifi p2p channel
- * @param listener WiFiP2pBroadcastListener
- */
- public WiFiP2pBroadcastReceiver(WifiP2pManager manager,
- WifiP2pManager.Channel channel,
- WiFiP2pBroadcastListener listener) {
- super();
- this.manager = manager;
- this.channel = channel;
- this.eventListener = listener;
- }
- /*
- * (non-Javadoc)
- * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
- * android.content.Intent)
- */
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
- // UI update to indicate wifi p2p status.
- int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
- if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
- // Wifi Direct mode is enabled
- setIsWifiP2pEnabled = (true);
- } else {
- setIsWifiP2pEnabled =(false);
- }
- this.eventListener.wifiP2pIsEnabled(setIsWifiP2pEnabled);
- } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
- // THE DEVICE LIST CHANGED
- // REQUEST THE LIST OF DEVICES
- Log.d("WiFiP2pBroadcastReceiver", "P2P peers changed.");
- if (manager != null) {
- manager.requestPeers(channel, this);
- }
- } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
- if (manager == null) {
- return;
- }
- NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
- if (networkInfo.isConnected()) {
- isConnected = true;
- // we are connected with the other device, request connection
- // info to find group owner IP
- manager.requestConnectionInfo(channel, this);
- if (this.ownDevice == null){
- this.disconnect();
- }
- } else {
- if (networkInfo.getDetailedState() == android.net.NetworkInfo.DetailedState.DISCONNECTED){
- isConnected = false;
- }
- if (this.networkState != null && !this.networkState.equals(networkInfo.getDetailedState()) && networkInfo.getDetailedState() == android.net.NetworkInfo.DetailedState.DISCONNECTED){
- // It's a disconnect
- this.eventListener.didDisconnect();
- }
- }
- if (this.networkState != networkInfo.getDetailedState()){
- Log.d("WiFiP2pBroadcastReceiver", "P2P device network state changed to " + this.getDeviceNetworkStatus(networkInfo.getDetailedState()) + ".");
- }
- this.networkState = networkInfo.getDetailedState();
- } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
- WifiP2pDevice device = intent.getParcelableExtra( WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
- this.ownDevice = device;
- this.eventListener.ownDeviceInformationIsUpdated(device);
- }
- }
- // CONNECTION TO A DEVICE
- // ConnectionInfoListener
- @Override
- public void onConnectionInfoAvailable(final WifiP2pInfo info) {
- //
- // The owner IP is now known.
- //boolean thisDeviceIsHost = info.isGroupOwner;
- // InetAddress from WifiP2pInfo struct.
- //String ownerIP = info.groupOwnerAddress.getHostAddress();
- // After the group negotiation, we assign the group owner as the file
- // server. The file server is single threaded, single connection server
- // socket.
- if (info.groupFormed){
- this.eventListener.didConnect(info.isGroupOwner, info);
- }
- }
- // AVAILABLE DEVICES
- // PEERLISTLISTENER
- @Override
- public void onPeersAvailable(WifiP2pDeviceList peerList) {
- List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
- peers.addAll(peerList.getDeviceList());
- if (peers.size() == 0) {
- Log.d("WiFiP2pBroadcastReceiver", "No devices found");
- }
- this.eventListener.discoveredDevices(peers);
- // DISMISS PROGRESS IF NEEDED
- }
- /**
- * Connects to the given device.
- * @param device
- */
- public void connect(WifiP2pDevice device){
- if (device != null) {
- WifiP2pConfig config = new WifiP2pConfig();
- config.deviceAddress = device.deviceAddress;
- config.wps.setup = WpsInfo.PBC;
- manager.connect(channel, config, new WifiP2pManager.ActionListener() {
- private WiFiP2pBroadcastListener eventListener;
- @Override
- public void onSuccess() {
- isConnected = true;
- // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
- }
- @Override
- public void onFailure(int reason) {
- this.eventListener.failedToConnect();
- }
- public WifiP2pManager.ActionListener init(WiFiP2pBroadcastListener eventListener){
- this.eventListener = eventListener;
- return this;
- }
- }.init(this.eventListener));
- }
- }
- /**
- * Disconnects from the connected wifi direct group if the own device is still connected.
- */
- public void disconnect() {
- if (isConnected){
- manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
- private WiFiP2pBroadcastListener eventListener;
- @Override
- public void onFailure(int reasonCode) {
- this.eventListener.failedToDisconnect();
- }
- @Override
- public void onSuccess() {
- isConnected = false;
- this.eventListener.didDisconnect();
- }
- public WifiP2pManager.ActionListener init(WiFiP2pBroadcastListener eventListener){
- this.eventListener = eventListener;
- return this;
- }
- }.init(this.eventListener));
- }
- }
- /**
- * Discover other devices.
- * The WiFiP2pBroadcastListener will inform about any change.
- */
- public void discoverDevices(){
- manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
- @Override
- public void onSuccess() {
- Log.d("WiFiP2pBroadcastReceiver", " Discovering Peers initiated.");
- }
- @Override
- public void onFailure(int reasonCode) {
- Log.d("WiFiP2pBroadcastReceiver", " Discovering Peers failed. c="+reasonCode);
- }
- });
- }
- /**
- * Returns a string representation of the given network status.
- * @param networkStatus network status
- * @return localised network status string
- */
- private String getDeviceNetworkStatus(android.net.NetworkInfo.DetailedState networkStatus) {
- switch (networkStatus) {
- case DISCONNECTED: {
- return "Disconnected";
- }
- case AUTHENTICATING:
- {
- return "Authenticating";
- }
- case BLOCKED:
- {
- return "Blocked";
- }
- case CONNECTED: {
- return "CONNECTED";
- }
- case CONNECTING:
- {
- return "Connecting";
- }
- case DISCONNECTING:
- {
- return "Disconnecting";
- }
- case FAILED:
- {
- return "Failed";
- }
- case IDLE:
- {
- return "IDLE";
- }
- case OBTAINING_IPADDR:
- {
- return "Obtaining IPADDR";
- }
- case SCANNING:
- {
- return "Scanning";
- }
- case SUSPENDED:
- {
- return "Suspended";
- }
- default: {
- return "Unknown";
- }
- }
- }
- }
|