瀏覽代碼

call discover peers;
more logs;
changing network state just if it really changes;
a disconnect just if it is a disconnect;

Julien Clauter 9 年之前
父節點
當前提交
640269d3e2

+ 11 - 4
src/de/tudarmstadt/informatik/hostage/sync/wifi_direct/WiFiP2pBroadcastReceiver.java

@@ -33,6 +33,8 @@ public class WiFiP2pBroadcastReceiver extends BroadcastReceiver implements WifiP
 
     private WifiP2pManager manager;
     private WifiP2pManager.Channel channel;
+
+    private android.net.NetworkInfo.DetailedState networkState = null;
     //private WifiP2pManager.PeerListListener peerListListener;
     //private WifiP2pManager.ConnectionInfoListener connectionInfoListener;
 
@@ -89,8 +91,7 @@ public class WiFiP2pBroadcastReceiver extends BroadcastReceiver implements WifiP
                 return;
             }
 
-            NetworkInfo networkInfo = (NetworkInfo) intent
-                    .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
+            NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
 
             if (networkInfo.isConnected()) {
 
@@ -98,9 +99,13 @@ public class WiFiP2pBroadcastReceiver extends BroadcastReceiver implements WifiP
                 // info to find group owner IP
                 manager.requestConnectionInfo(channel, this);
             } else {
-                // It's a disconnect
-                this.eventListener.didDisconnect();
+                if (this.networkState != null && !this.networkState.equals(networkInfo.getDetailedState()) && networkInfo.getDetailedState() == android.net.NetworkInfo.DetailedState.DISCONNECTED){
+                    // It's a disconnect
+                    this.eventListener.didDisconnect();
+                }
+
             }
+            this.networkState = networkInfo.getDetailedState();
         } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
             WifiP2pDevice device = intent.getParcelableExtra( WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
             this.eventListener.deviceIsUpdated(device);
@@ -201,9 +206,11 @@ public class WiFiP2pBroadcastReceiver extends BroadcastReceiver implements WifiP
         manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
             @Override
             public void onSuccess() {
+                Log.d("WiFiP2pBroadcastReceiver", " Discovering Peers initiated.");
             }
             @Override
             public void onFailure(int reasonCode) {
+                Log.d("WiFiP2pBroadcastReceiver", " Discovering Peers failed. c="+reasonCode);
             }
         });
     }

+ 14 - 5
src/de/tudarmstadt/informatik/hostage/sync/wifi_direct/WiFiP2pEventHandler.java

@@ -38,15 +38,21 @@ public class WiFiP2pEventHandler implements WifiP2pManager.ChannelListener {
 
         manager = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
         channel = manager.initialize(activity, activity.getMainLooper(), this);
+
+        //receiver = new WiFiP2pBroadcastReceiver(manager, channel, this.eventListener);
+        //activity.registerReceiver(receiver, intentFilter);
     }
 
     public void onResume() {
-        receiver = new WiFiP2pBroadcastReceiver(manager, channel, this.eventListener);
-        activity.registerReceiver(receiver, intentFilter);
+        if (receiver == null){
+            receiver = new WiFiP2pBroadcastReceiver(manager, channel, this.eventListener);
+            activity.registerReceiver(receiver, intentFilter);
+        }
     }
 
     public void onPause() {
         activity.unregisterReceiver(receiver);
+        receiver = null;
     }
 
     @Override
@@ -61,14 +67,17 @@ public class WiFiP2pEventHandler implements WifiP2pManager.ChannelListener {
     }
 
     public void connect(WifiP2pDevice device){
-        this.receiver.connect(device);
+        if (this.receiver != null)
+            this.receiver.connect(device);
     }
 
     public void disconnect() {
-        this.receiver.disconnect();
+        if (this.receiver != null)
+            this.receiver.disconnect();
     }
 
     public void discoverDevices(){
-        this.receiver.discoverDevices();
+        if (this.receiver != null)
+            this.receiver.discoverDevices();
     }
 }

+ 16 - 0
src/de/tudarmstadt/informatik/hostage/sync/wifi_direct/ui/WiFiP2pSyncActivity.java

@@ -90,8 +90,22 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
 
     @Override
     public void onPause() {
+        this.wifiEventHandler().onPause();
         super.onPause();
+    }
+
+    @Override
+    protected void onStart()
+    {
+        super.onStart();
+        this.wifiEventHandler().onResume();
+    }
+
+    @Override
+    protected void onStop()
+    {
         this.wifiEventHandler().onPause();
+        super.onStop();
     }
 
     private WiFiP2pEventHandler.WiFiP2pEventListener eventListener(){
@@ -151,6 +165,8 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
                 public void deviceIsUpdated(WifiP2pDevice device) {
                     Log.d("WiFiP2pSyncActivity", "Updated device " + device.deviceName + " " + device.deviceAddress + ".");
                     this.activity.updateDevice(device);
+                    this.activity.wifiEventHandler().discoverDevices();
+
                 }
             }.init(this);
         }