Skip to content

Commit

Permalink
Display connected and nearby devices in Transport App (#101)
Browse files Browse the repository at this point in the history
* Display connected and nearby devices in Transport App
  • Loading branch information
JAReddy authored Jun 17, 2024
1 parent fa75b1c commit 15412c2
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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<String> nearbyDevicesSet = new HashSet<>();
HashSet<String> 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();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<WifiP2pDevice> 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.
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,32 @@ public class WifiDirectManager implements WifiP2pManager.ConnectionInfoListener,
private WifiDirectLifeCycleObserver lifeCycleObserver;
private WifiDirectBroadcastReceiver receiver;
private ArrayList<WifiP2pDevice> discoveredPeers;

public Collection<WifiP2pDevice> getConnectedPeers() {
return connectedPeers;
}

public void setConnectedPeers(Collection<WifiP2pDevice> connectedPeers) {
this.connectedPeers = connectedPeers;
}

private Collection<WifiP2pDevice> connectedPeers;
private String wifiDirectGroupHostIP;
private String groupHostInfo;
private boolean isConnected;

private List<WifiDirectStateListener> 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
*
Expand All @@ -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() {
Expand Down Expand Up @@ -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<WifiP2pDevice> devices = new ArrayList<>();
Collection<WifiP2pDevice> foundDevices = deviceList.getDeviceList();
devices.addAll(foundDevices);
//this.discoveredPeers = (ArrayList<WifiP2pDevice>) deviceList.getDeviceList();

this.discoveredPeers = (ArrayList<WifiP2pDevice>) devices;
notifyActionToListeners(WIFI_DIRECT_ACTIONS.WIFI_DIRECT_MANAGER_PEERS_CHANGED);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ddd.wifidirect;

public interface WifiDirectStateListener {
void onReceiveAction(WifiDirectManager.WIFI_DIRECT_ACTIONS action);
}
Loading

0 comments on commit 15412c2

Please sign in to comment.