WiFiP2pBroadcastReceiver.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /**
  20. * This listener will inform about any wifi direct change.
  21. */
  22. public interface WiFiP2pBroadcastListener {
  23. public void discoveredDevices(List<WifiP2pDevice> peers);
  24. public void wifiP2pIsEnabled(boolean enabled);
  25. public void didConnect(boolean isHost, WifiP2pInfo connectionInfo);
  26. public void failedToConnect();
  27. public void didDisconnect();
  28. public void failedToDisconnect();
  29. public void ownDeviceInformationIsUpdated(WifiP2pDevice device);
  30. }
  31. private WifiP2pManager manager;
  32. private WifiP2pManager.Channel channel;
  33. private WifiP2pDevice ownDevice;
  34. private android.net.NetworkInfo.DetailedState networkState = null;
  35. static boolean setIsWifiP2pEnabled;
  36. static boolean isConnected = false;
  37. private WiFiP2pBroadcastListener eventListener;
  38. /**
  39. * @param manager WifiP2pManager system service
  40. * @param channel Wifi p2p channel
  41. * @param listener WiFiP2pBroadcastListener
  42. */
  43. public WiFiP2pBroadcastReceiver(WifiP2pManager manager,
  44. WifiP2pManager.Channel channel,
  45. WiFiP2pBroadcastListener listener) {
  46. super();
  47. this.manager = manager;
  48. this.channel = channel;
  49. this.eventListener = listener;
  50. }
  51. /*
  52. * (non-Javadoc)
  53. * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
  54. * android.content.Intent)
  55. */
  56. @Override
  57. public void onReceive(Context context, Intent intent) {
  58. String action = intent.getAction();
  59. if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
  60. // UI update to indicate wifi p2p status.
  61. int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
  62. if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
  63. // Wifi Direct mode is enabled
  64. setIsWifiP2pEnabled = (true);
  65. } else {
  66. setIsWifiP2pEnabled =(false);
  67. }
  68. this.eventListener.wifiP2pIsEnabled(setIsWifiP2pEnabled);
  69. } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
  70. // THE DEVICE LIST CHANGED
  71. // REQUEST THE LIST OF DEVICES
  72. Log.d("WiFiP2pBroadcastReceiver", "P2P peers changed.");
  73. if (manager != null) {
  74. manager.requestPeers(channel, this);
  75. }
  76. } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
  77. if (manager == null) {
  78. return;
  79. }
  80. NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
  81. if (networkInfo.isConnected()) {
  82. isConnected = true;
  83. // we are connected with the other device, request connection
  84. // info to find group owner IP
  85. manager.requestConnectionInfo(channel, this);
  86. if (this.ownDevice == null){
  87. this.disconnect();
  88. }
  89. } else {
  90. if (networkInfo.getDetailedState() == android.net.NetworkInfo.DetailedState.DISCONNECTED){
  91. isConnected = false;
  92. }
  93. if (this.networkState != null && !this.networkState.equals(networkInfo.getDetailedState()) && networkInfo.getDetailedState() == android.net.NetworkInfo.DetailedState.DISCONNECTED){
  94. // It's a disconnect
  95. this.eventListener.didDisconnect();
  96. }
  97. }
  98. if (this.networkState != networkInfo.getDetailedState()){
  99. Log.d("WiFiP2pBroadcastReceiver", "P2P device network state changed to " + this.getDeviceNetworkStatus(networkInfo.getDetailedState()) + ".");
  100. }
  101. this.networkState = networkInfo.getDetailedState();
  102. } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
  103. WifiP2pDevice device = intent.getParcelableExtra( WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
  104. this.ownDevice = device;
  105. this.eventListener.ownDeviceInformationIsUpdated(device);
  106. }
  107. }
  108. // CONNECTION TO A DEVICE
  109. // ConnectionInfoListener
  110. @Override
  111. public void onConnectionInfoAvailable(final WifiP2pInfo info) {
  112. //
  113. // The owner IP is now known.
  114. //boolean thisDeviceIsHost = info.isGroupOwner;
  115. // InetAddress from WifiP2pInfo struct.
  116. //String ownerIP = info.groupOwnerAddress.getHostAddress();
  117. // After the group negotiation, we assign the group owner as the file
  118. // server. The file server is single threaded, single connection server
  119. // socket.
  120. if (info.groupFormed){
  121. this.eventListener.didConnect(info.isGroupOwner, info);
  122. }
  123. }
  124. // AVAILABLE DEVICES
  125. // PEERLISTLISTENER
  126. @Override
  127. public void onPeersAvailable(WifiP2pDeviceList peerList) {
  128. List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
  129. peers.addAll(peerList.getDeviceList());
  130. if (peers.size() == 0) {
  131. Log.d("WiFiP2pBroadcastReceiver", "No devices found");
  132. }
  133. this.eventListener.discoveredDevices(peers);
  134. // DISMISS PROGRESS IF NEEDED
  135. }
  136. /**
  137. * Connects to the given device.
  138. * @param device
  139. */
  140. public void connect(WifiP2pDevice device){
  141. if (device != null) {
  142. WifiP2pConfig config = new WifiP2pConfig();
  143. config.deviceAddress = device.deviceAddress;
  144. config.wps.setup = WpsInfo.PBC;
  145. manager.connect(channel, config, new WifiP2pManager.ActionListener() {
  146. private WiFiP2pBroadcastListener eventListener;
  147. @Override
  148. public void onSuccess() {
  149. isConnected = true;
  150. // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
  151. }
  152. @Override
  153. public void onFailure(int reason) {
  154. this.eventListener.failedToConnect();
  155. }
  156. public WifiP2pManager.ActionListener init(WiFiP2pBroadcastListener eventListener){
  157. this.eventListener = eventListener;
  158. return this;
  159. }
  160. }.init(this.eventListener));
  161. }
  162. }
  163. /**
  164. * Disconnects from the connected wifi direct group if the own device is still connected.
  165. */
  166. public void disconnect() {
  167. if (isConnected){
  168. manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
  169. private WiFiP2pBroadcastListener eventListener;
  170. @Override
  171. public void onFailure(int reasonCode) {
  172. this.eventListener.failedToDisconnect();
  173. }
  174. @Override
  175. public void onSuccess() {
  176. isConnected = false;
  177. this.eventListener.didDisconnect();
  178. }
  179. public WifiP2pManager.ActionListener init(WiFiP2pBroadcastListener eventListener){
  180. this.eventListener = eventListener;
  181. return this;
  182. }
  183. }.init(this.eventListener));
  184. }
  185. }
  186. /**
  187. * Discover other devices.
  188. * The WiFiP2pBroadcastListener will inform about any change.
  189. */
  190. public void discoverDevices(){
  191. manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
  192. @Override
  193. public void onSuccess() {
  194. Log.d("WiFiP2pBroadcastReceiver", " Discovering Peers initiated.");
  195. }
  196. @Override
  197. public void onFailure(int reasonCode) {
  198. Log.d("WiFiP2pBroadcastReceiver", " Discovering Peers failed. c="+reasonCode);
  199. }
  200. });
  201. }
  202. /**
  203. * Returns a string representation of the given network status.
  204. * @param networkStatus network status
  205. * @return localised network status string
  206. */
  207. private String getDeviceNetworkStatus(android.net.NetworkInfo.DetailedState networkStatus) {
  208. switch (networkStatus) {
  209. case DISCONNECTED: {
  210. return "Disconnected";
  211. }
  212. case AUTHENTICATING:
  213. {
  214. return "Authenticating";
  215. }
  216. case BLOCKED:
  217. {
  218. return "Blocked";
  219. }
  220. case CONNECTED: {
  221. return "CONNECTED";
  222. }
  223. case CONNECTING:
  224. {
  225. return "Connecting";
  226. }
  227. case DISCONNECTING:
  228. {
  229. return "Disconnecting";
  230. }
  231. case FAILED:
  232. {
  233. return "Failed";
  234. }
  235. case IDLE:
  236. {
  237. return "IDLE";
  238. }
  239. case OBTAINING_IPADDR:
  240. {
  241. return "Obtaining IPADDR";
  242. }
  243. case SCANNING:
  244. {
  245. return "Scanning";
  246. }
  247. case SUSPENDED:
  248. {
  249. return "Suspended";
  250. }
  251. default: {
  252. return "Unknown";
  253. }
  254. }
  255. }
  256. }