WiFiP2pBroadcastReceiver.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package de.tudarmstadt.informatik.hostage.sync.wifi_direct;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.NetworkInfo;
  6. import android.net.wifi.WpsInfo;
  7. import android.net.wifi.p2p.WifiP2pConfig;
  8. import android.net.wifi.p2p.WifiP2pDevice;
  9. import android.net.wifi.p2p.WifiP2pDeviceList;
  10. import android.net.wifi.p2p.WifiP2pInfo;
  11. import android.net.wifi.p2p.WifiP2pManager;
  12. import android.util.Log;
  13. import com.example.android.wifidirect.WiFiDirectActivity;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. /**
  17. * Created by Julien on 07.01.2015.
  18. */
  19. public class WiFiP2pBroadcastReceiver extends BroadcastReceiver implements WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener {
  20. public interface WiFiP2pBroadcastListener {
  21. public void discoveredDevices(List<WifiP2pDevice> peers);
  22. public void wifiP2pIsEnabled(boolean enabled);
  23. public void didConnect(boolean isHost, WifiP2pInfo connectionInfo);
  24. public void failedToConnect();
  25. public void didDisconnect();
  26. public void failedToDisconnect();
  27. public void deviceIsUpdated(WifiP2pDevice device);
  28. }
  29. private WifiP2pManager manager;
  30. private WifiP2pManager.Channel channel;
  31. //private WifiP2pManager.PeerListListener peerListListener;
  32. //private WifiP2pManager.ConnectionInfoListener connectionInfoListener;
  33. static boolean setIsWifiP2pEnabled;
  34. private WiFiP2pBroadcastListener eventListener;
  35. /**
  36. * @param manager WifiP2pManager system service
  37. * @param channel Wifi p2p channel
  38. * @param listener WiFiP2pBroadcastListener
  39. */
  40. public WiFiP2pBroadcastReceiver(WifiP2pManager manager,
  41. WifiP2pManager.Channel channel,
  42. WiFiP2pBroadcastListener listener) {
  43. super();
  44. this.manager = manager;
  45. this.channel = channel;
  46. this.eventListener = listener;
  47. }
  48. /*
  49. * (non-Javadoc)
  50. * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
  51. * android.content.Intent)
  52. */
  53. @Override
  54. public void onReceive(Context context, Intent intent) {
  55. String action = intent.getAction();
  56. if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
  57. // UI update to indicate wifi p2p status.
  58. int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
  59. if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
  60. // Wifi Direct mode is enabled
  61. setIsWifiP2pEnabled = (true);
  62. } else {
  63. setIsWifiP2pEnabled =(false);
  64. }
  65. this.eventListener.wifiP2pIsEnabled(setIsWifiP2pEnabled);
  66. Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
  67. } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
  68. // THE DEVICE LIST CHANGED
  69. // REQUEST THE LIST OF DEVICES
  70. Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
  71. if (manager != null) {
  72. manager.requestPeers(channel, this);
  73. }
  74. } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
  75. if (manager == null) {
  76. return;
  77. }
  78. NetworkInfo networkInfo = (NetworkInfo) intent
  79. .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
  80. if (networkInfo.isConnected()) {
  81. // we are connected with the other device, request connection
  82. // info to find group owner IP
  83. manager.requestConnectionInfo(channel, this);
  84. } else {
  85. // It's a disconnect
  86. this.eventListener.didDisconnect();
  87. }
  88. } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
  89. WifiP2pDevice device = intent.getParcelableExtra( WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
  90. this.eventListener.deviceIsUpdated(device);
  91. }
  92. }
  93. // CONNECTION TO A DEVICE
  94. // ConnectionInfoListener
  95. @Override
  96. public void onConnectionInfoAvailable(final WifiP2pInfo info) {
  97. //
  98. // The owner IP is now known.
  99. //boolean thisDeviceIsHost = info.isGroupOwner;
  100. // InetAddress from WifiP2pInfo struct.
  101. //String ownerIP = info.groupOwnerAddress.getHostAddress();
  102. // After the group negotiation, we assign the group owner as the file
  103. // server. The file server is single threaded, single connection server
  104. // socket.
  105. if (info.groupFormed){
  106. if (info.isGroupOwner) {
  107. //new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text)).execute();
  108. } else {
  109. // The other device acts as the client. In this case, we enable the
  110. // get file button.
  111. }
  112. this.eventListener.didConnect(info.isGroupOwner, info);
  113. }
  114. }
  115. // AVAILABLE DEVICES
  116. // PEERLISTLISTENER
  117. @Override
  118. public void onPeersAvailable(WifiP2pDeviceList peerList) {
  119. List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
  120. peers.addAll(peerList.getDeviceList());
  121. if (peers.size() == 0) {
  122. Log.d(WiFiDirectActivity.TAG, "No devices found");
  123. }
  124. this.eventListener.discoveredDevices(peers);
  125. // DISMISS PROGRESS IF NEEDED
  126. }
  127. public void connect(WifiP2pDevice device){
  128. WifiP2pConfig config = new WifiP2pConfig();
  129. config.deviceAddress = device.deviceAddress;
  130. config.wps.setup = WpsInfo.PBC;
  131. manager.connect(channel, config, new WifiP2pManager.ActionListener() {
  132. private WiFiP2pBroadcastListener eventListener;
  133. @Override
  134. public void onSuccess() {
  135. // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
  136. }
  137. @Override
  138. public void onFailure(int reason) {
  139. this.eventListener.failedToConnect();
  140. }
  141. public WifiP2pManager.ActionListener init(WiFiP2pBroadcastListener eventListener){
  142. this.eventListener = eventListener;
  143. return this;
  144. }
  145. }.init(this.eventListener));
  146. }
  147. public void disconnect() {
  148. manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
  149. private WiFiP2pBroadcastListener eventListener;
  150. @Override
  151. public void onFailure(int reasonCode) {
  152. //Log.d(TAG, "Disconnect failed. Reason :" + reasonCode);
  153. this.eventListener.failedToDisconnect();
  154. }
  155. @Override
  156. public void onSuccess() {
  157. this.eventListener.didDisconnect();
  158. }
  159. public WifiP2pManager.ActionListener init(WiFiP2pBroadcastListener eventListener){
  160. this.eventListener = eventListener;
  161. return this;
  162. }
  163. }.init(this.eventListener));
  164. }
  165. public void discoverDevices(){
  166. manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
  167. @Override
  168. public void onSuccess() {
  169. }
  170. @Override
  171. public void onFailure(int reasonCode) {
  172. }
  173. });
  174. }
  175. }