WiFiP2pEventHandler.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package de.tudarmstadt.informatik.hostage.sync.wifi_direct;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.IntentFilter;
  5. import android.net.wifi.WifiManager;
  6. import android.net.wifi.p2p.WifiP2pDevice;
  7. import android.net.wifi.p2p.WifiP2pManager;
  8. /**
  9. * Created by Julien on 07.01.2015.
  10. */
  11. public class WiFiP2pEventHandler implements WifiP2pManager.ChannelListener {
  12. /**
  13. * The WiFiP2pEventListener informs about all wifi direct changes.
  14. */
  15. public interface WiFiP2pEventListener extends WiFiP2pBroadcastReceiver.WiFiP2pBroadcastListener{
  16. public void onConnectionLost();
  17. }
  18. private WifiP2pManager manager;
  19. private boolean retryChannel = false;
  20. private final IntentFilter intentFilter = new IntentFilter();
  21. private WifiP2pManager.Channel channel;
  22. private WiFiP2pBroadcastReceiver receiver = null;
  23. private WiFiP2pEventListener eventListener;
  24. private Activity activity;
  25. public WiFiP2pEventHandler(Activity activity, WiFiP2pEventListener listener){
  26. this.eventListener = listener;
  27. this.activity = activity;
  28. intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
  29. intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
  30. intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
  31. intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
  32. manager = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
  33. channel = manager.initialize(activity, activity.getMainLooper(), this);
  34. }
  35. public void setOnRetryIfTheChannelIsLost(boolean shouldRetry){
  36. this.retryChannel = shouldRetry;
  37. }
  38. public boolean isRetryingIfTheChannelIsLost(){
  39. return this.retryChannel;
  40. }
  41. /**
  42. * Call this method if the app comes back to foreground. E.g. in the onResume from the activity.
  43. */
  44. public void startService() {
  45. if (receiver == null){
  46. receiver = new WiFiP2pBroadcastReceiver(manager, channel, this.eventListener);
  47. activity.registerReceiver(receiver, intentFilter);
  48. }
  49. }
  50. /**
  51. * Call this method to disabled wifi direct while the app is in the background. E.g. in the onPause from the activity.
  52. */
  53. public void stopService() {
  54. if (receiver != null) {
  55. activity.unregisterReceiver(receiver);
  56. receiver = null;
  57. }
  58. }
  59. @Override
  60. public void onChannelDisconnected() {
  61. // we will try once more
  62. if (manager != null && !retryChannel) {
  63. retryChannel = true;
  64. manager.initialize(this.activity, this.activity.getMainLooper(), this);
  65. receiver = new WiFiP2pBroadcastReceiver(manager, channel, this.eventListener);
  66. activity.registerReceiver(receiver, intentFilter);
  67. } else {
  68. this.eventListener.onConnectionLost();
  69. }
  70. }
  71. /**
  72. * Connect your device to the given device.
  73. * @param device
  74. */
  75. public void connect(WifiP2pDevice device){
  76. if (this.receiver != null && device != null)
  77. this.receiver.connect(device);
  78. }
  79. /**
  80. * Disconnect from the connected wifi group.
  81. */
  82. public void disconnect() {
  83. if (this.receiver != null)
  84. this.receiver.disconnect();
  85. }
  86. /**
  87. * Discover all available wifi direct device in the current environment.
  88. */
  89. public void discoverDevices(){
  90. if (this.receiver != null)
  91. this.receiver.discoverDevices();
  92. }
  93. }