|
@@ -0,0 +1,211 @@
|
|
|
|
+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 com.example.android.wifidirect.WiFiDirectActivity;
|
|
|
|
+
|
|
|
|
+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 {
|
|
|
|
+
|
|
|
|
+ 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 deviceIsUpdated(WifiP2pDevice device);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private WifiP2pManager manager;
|
|
|
|
+ private WifiP2pManager.Channel channel;
|
|
|
|
+ //private WifiP2pManager.PeerListListener peerListListener;
|
|
|
|
+ //private WifiP2pManager.ConnectionInfoListener connectionInfoListener;
|
|
|
|
+
|
|
|
|
+ static boolean setIsWifiP2pEnabled;
|
|
|
|
+
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
|
|
|
|
+
|
|
|
|
+ } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
|
|
|
|
+ // THE DEVICE LIST CHANGED
|
|
|
|
+ // REQUEST THE LIST OF DEVICES
|
|
|
|
+ Log.d(WiFiDirectActivity.TAG, "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()) {
|
|
|
|
+
|
|
|
|
+ // we are connected with the other device, request connection
|
|
|
|
+ // info to find group owner IP
|
|
|
|
+ manager.requestConnectionInfo(channel, this);
|
|
|
|
+ } else {
|
|
|
|
+ // It's a disconnect
|
|
|
|
+ this.eventListener.didDisconnect();
|
|
|
|
+ }
|
|
|
|
+ } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
|
|
|
|
+ WifiP2pDevice device = intent.getParcelableExtra( WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
|
|
|
|
+ this.eventListener.deviceIsUpdated(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){
|
|
|
|
+ if (info.isGroupOwner) {
|
|
|
|
+ //new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text)).execute();
|
|
|
|
+ } else {
|
|
|
|
+ // The other device acts as the client. In this case, we enable the
|
|
|
|
+ // get file button.
|
|
|
|
+ }
|
|
|
|
+ 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(WiFiDirectActivity.TAG, "No devices found");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.eventListener.discoveredDevices(peers);
|
|
|
|
+ // DISMISS PROGRESS IF NEEDED
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public void connect(WifiP2pDevice device){
|
|
|
|
+ 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() {
|
|
|
|
+ // 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));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void disconnect() {
|
|
|
|
+
|
|
|
|
+ manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
|
|
|
|
+ private WiFiP2pBroadcastListener eventListener;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onFailure(int reasonCode) {
|
|
|
|
+ //Log.d(TAG, "Disconnect failed. Reason :" + reasonCode);
|
|
|
|
+ this.eventListener.failedToDisconnect();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onSuccess() {
|
|
|
|
+ this.eventListener.didDisconnect();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public WifiP2pManager.ActionListener init(WiFiP2pBroadcastListener eventListener){
|
|
|
|
+ this.eventListener = eventListener;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ }.init(this.eventListener));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void discoverDevices(){
|
|
|
|
+ manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onSuccess() {
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public void onFailure(int reasonCode) {
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+}
|