diff --git a/BundleTransport/app/src/main/java/com/ddd/bundletransport/MainActivity.java b/BundleTransport/app/src/main/java/com/ddd/bundletransport/MainActivity.java index 65062be267..1e60104615 100644 --- a/BundleTransport/app/src/main/java/com/ddd/bundletransport/MainActivity.java +++ b/BundleTransport/app/src/main/java/com/ddd/bundletransport/MainActivity.java @@ -24,6 +24,7 @@ import androidx.appcompat.app.AppCompatActivity; import com.ddd.wifidirect.WifiDirectManager; +import com.ddd.wifidirect.WifiDirectStateListener; import org.whispersystems.libsignal.ecc.Curve; import org.whispersystems.libsignal.ecc.ECKeyPair; @@ -33,13 +34,14 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Collection; +import java.util.HashSet; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -public class MainActivity extends AppCompatActivity implements RpcServerStateListener { +public class MainActivity extends AppCompatActivity implements RpcServerStateListener, WifiDirectStateListener { - private static final int PORT = 7777; + private static final int PORT = 1778; public static final int PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION = 1001; public static final String TAG = "dddTransport"; @@ -61,6 +63,10 @@ public class MainActivity extends AppCompatActivity implements RpcServerStateLis private ExecutorService executor = Executors.newFixedThreadPool(2); ; private TextView serverConnectStatus; + + private TextView connectedPeersText; + private TextView nearByPeersText; + private Button connectServerBtn; private ConnectivityManager connectivityManager; private ConnectivityManager.NetworkCallback serverConnectNetworkCallback; @@ -252,6 +258,8 @@ protected void onCreate(Bundle savedInstanceState) { portInput = findViewById(R.id.port_input); serverConnectStatus = findViewById(R.id.server_connection_status); connectServerBtn = findViewById(R.id.btn_connect_bundle_server); + connectedPeersText = findViewById(R.id.connected_peers); + nearByPeersText = findViewById(R.id.nearby_peers); // retrieve domain and port from shared preferences // populate text inputs if data is retrieved @@ -261,7 +269,7 @@ protected void onCreate(Bundle savedInstanceState) { String SERVER_BASE_PATH = this.getExternalFilesDir(null) + "/BundleTransmission"; Receive_Directory = SERVER_BASE_PATH + "/client"; Server_Directory = SERVER_BASE_PATH + "/server"; - wifiDirectManager = new WifiDirectManager(this.getApplication(), this.getLifecycle()); + wifiDirectManager = new WifiDirectManager(this.getApplication(), this.getLifecycle(), this); wifiDirectManager.initialize(); grpcServer = new RpcServer(this); @@ -389,4 +397,50 @@ protected void onDestroy() { //connectivityManager.unregisterNetworkCallback(serverConnectNetworkCallback); executor.shutdown(); } + + private void updateConnectedDevices() { + updateNearbyDevices(); + connectedPeersText.setText(""); + if (wifiDirectManager.getConnectedPeers() != null) { + Log.d(TAG, "Connected Devices Updates\n"); + wifiDirectManager.getConnectedPeers().stream().forEach(device -> { + connectedPeersText.append(device.deviceName + "\n"); + }); + } + + } + + private void updateNearbyDevices() { + nearByPeersText.setText(""); + HashSet nearbyDevicesSet = new HashSet<>(); + HashSet connectedDevicesSet = new HashSet<>(); + if (wifiDirectManager.getConnectedPeers() != null && !wifiDirectManager.getConnectedPeers().isEmpty()) { + for (WifiP2pDevice p2pDevice : wifiDirectManager.getConnectedPeers()) { + connectedDevicesSet.add(p2pDevice.deviceName); + } + } + if (wifiDirectManager.getPeerList() != null) { + for (WifiP2pDevice p2pDevice : wifiDirectManager.getPeerList()) { + if (!connectedDevicesSet.contains(p2pDevice.deviceName)) { + nearbyDevicesSet.add(p2pDevice.deviceName); + } + } + } + Log.d(TAG, "Nearby Devices Updates\n"); + nearbyDevicesSet.stream().forEach(deviceName -> { + nearByPeersText.append(deviceName + "\n"); + }); + } + + @Override + public void onReceiveAction(WifiDirectManager.WIFI_DIRECT_ACTIONS action) { + runOnUiThread(() -> { + if (WifiDirectManager.WIFI_DIRECT_ACTIONS.WIFI_DIRECT_MANAGER_PEERS_CHANGED == action) { + updateNearbyDevices(); + } else if (WifiDirectManager.WIFI_DIRECT_ACTIONS.WIFI_DIRECT_MANAGER_FORMED_CONNECTION_SUCCESSFUL == + action) { + updateConnectedDevices(); + } + }); + } } \ No newline at end of file diff --git a/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectBroadcastReceiver.java b/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectBroadcastReceiver.java index c72238d30b..8094ade087 100644 --- a/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectBroadcastReceiver.java +++ b/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectBroadcastReceiver.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.net.NetworkInfo; import android.net.wifi.p2p.WifiP2pDevice; +import android.net.wifi.p2p.WifiP2pGroup; import android.net.wifi.p2p.WifiP2pManager; import android.util.Log; @@ -72,9 +73,15 @@ public void onReceive(Context context, Intent intent) { } // Broadcast intent action indicating that the available peer list has changed. // This can be sent as a result of peers being found, lost or updated. - else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { + if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { Log.d(MainActivity.TAG, "WifiDirectBroadcastReceiver INTENT PEERS_CHANGED"); - ArrayList newPeers = manager.getPeerList(); + if (manager != null) { + try { + manager.getManager().requestPeers(manager.getChannel(), manager); + } catch (SecurityException e) { + e.printStackTrace(); + } + } } // Broadcast intent action indicating that the state of Wi-Fi p2p // connectivity has changed. @@ -93,13 +100,20 @@ else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { //getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP); NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); + WifiP2pGroup wifiP2pGroup = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP); if (networkInfo.isConnected()) { // we are connected, request connection // info to find group owner IP this.manager.setConnected(true); - this.manager.getManager().requestConnectionInfo(this.manager.getChannel(), this.manager); Log.d(MainActivity.TAG, "WIFI_P2P_CONNECTION_CHANGED_ACTION connected"); + + if (wifiP2pGroup != null) { + Log.d(MainActivity.TAG, "WifiP2pGroup client list changed"); + this.manager.setConnectedPeers(wifiP2pGroup.getClientList()); + this.manager.notifyActionToListeners( + WifiDirectManager.WIFI_DIRECT_ACTIONS.WIFI_DIRECT_MANAGER_FORMED_CONNECTION_SUCCESSFUL); + } } else { // It's a disconnect this.manager.setConnected(false); diff --git a/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectManager.java b/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectManager.java index 33c3928e1c..586e429324 100644 --- a/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectManager.java +++ b/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectManager.java @@ -43,10 +43,32 @@ public class WifiDirectManager implements WifiP2pManager.ConnectionInfoListener, private WifiDirectLifeCycleObserver lifeCycleObserver; private WifiDirectBroadcastReceiver receiver; private ArrayList discoveredPeers; + + public Collection getConnectedPeers() { + return connectedPeers; + } + + public void setConnectedPeers(Collection connectedPeers) { + this.connectedPeers = connectedPeers; + } + + private Collection connectedPeers; private String wifiDirectGroupHostIP; private String groupHostInfo; private boolean isConnected; + private List listeners = new ArrayList<>(); + + public void notifyActionToListeners(WIFI_DIRECT_ACTIONS action) { + for (WifiDirectStateListener listener : listeners) { + listener.onReceiveAction(action); + } + } + + public enum WIFI_DIRECT_ACTIONS { + WIFI_DIRECT_MANAGER_PEERS_CHANGED, WIFI_DIRECT_MANAGER_FORMED_CONNECTION_SUCCESSFUL + } + /** * Ctor * @@ -57,9 +79,10 @@ public class WifiDirectManager implements WifiP2pManager.ConnectionInfoListener, * AppCompatActivity.getLifeCycle() call in * your main activity */ - public WifiDirectManager(Context context, Lifecycle lifeCycle) { + public WifiDirectManager(Context context, Lifecycle lifeCycle, WifiDirectStateListener listener) { this.context = context; this.lifeCycle = lifeCycle; + listeners.add(listener); } public void initialize() { @@ -152,13 +175,14 @@ public void onFailure(int reasonCode) { */ @Override public void onPeersAvailable(WifiP2pDeviceList deviceList) { - Log.d(MainActivity.TAG, "Peers available..."); + Log.d(MainActivity.TAG, "Peers available: " + deviceList.getDeviceList().size()); List devices = new ArrayList<>(); Collection foundDevices = deviceList.getDeviceList(); devices.addAll(foundDevices); //this.discoveredPeers = (ArrayList) deviceList.getDeviceList(); this.discoveredPeers = (ArrayList) devices; + notifyActionToListeners(WIFI_DIRECT_ACTIONS.WIFI_DIRECT_MANAGER_PEERS_CHANGED); } /** diff --git a/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectStateListener.java b/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectStateListener.java new file mode 100644 index 0000000000..80b9e53f2f --- /dev/null +++ b/BundleTransport/app/src/main/java/com/ddd/wifidirect/WifiDirectStateListener.java @@ -0,0 +1,5 @@ +package com.ddd.wifidirect; + +public interface WifiDirectStateListener { + void onReceiveAction(WifiDirectManager.WIFI_DIRECT_ACTIONS action); +} \ No newline at end of file diff --git a/BundleTransport/app/src/main/res/layout/activity_main.xml b/BundleTransport/app/src/main/res/layout/activity_main.xml index 96bc43c808..ccb75bcc39 100644 --- a/BundleTransport/app/src/main/res/layout/activity_main.xml +++ b/BundleTransport/app/src/main/res/layout/activity_main.xml @@ -12,97 +12,134 @@ - + android:layout_height="match_parent"> - + + - + - - - - -