WiFiP2pBroadcastReceiver.java 7.2 KB

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