From 253ff24304cc0b2c0c0ea8f86bf1d21ae3aec734 Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Thu, 13 Jan 2022 13:05:22 +0000 Subject: [PATCH] Use hardcoded network name and passphrase on API 29+. --- .../wifidirect/WifiDirectServiceImpl.java | 79 ++++++++++++++----- 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/briarproject/publicmesh/wifidirect/WifiDirectServiceImpl.java b/app/src/main/java/org/briarproject/publicmesh/wifidirect/WifiDirectServiceImpl.java index 9b0381b..16b9789 100644 --- a/app/src/main/java/org/briarproject/publicmesh/wifidirect/WifiDirectServiceImpl.java +++ b/app/src/main/java/org/briarproject/publicmesh/wifidirect/WifiDirectServiceImpl.java @@ -6,6 +6,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.net.MacAddress; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.WifiLock; import android.net.wifi.WpsInfo; @@ -48,6 +49,9 @@ import static org.briarproject.publicmesh.wifidirect.WifiDirectService.Status.ST @NotNullByDefault class WifiDirectServiceImpl implements WifiDirectService, Service { + private static final String NETWORK_NAME = "DIRECT-00-MESH"; + private static final String PASSPHRASE = "00000000"; + private static final int MAX_GROUP_INFO_ATTEMPTS = 5; private static final Logger LOG = getLogger(WifiDirectServiceImpl.class.getName()); @@ -118,7 +122,18 @@ class WifiDirectServiceImpl implements WifiDirectService, Service { } }; try { - wifiP2pManager.createGroup(channel, listener); + if (SDK_INT >= 29) { + // Use the well-known network name and password so devices running API 29+ + // can connect without user interaction + WifiP2pConfig config = new WifiP2pConfig.Builder() + .setNetworkName(NETWORK_NAME) + .setPassphrase(PASSPHRASE) + .build(); + wifiP2pManager.createGroup(channel, config, listener); + } else { + // Use whatever network name and password the platform gives us + wifiP2pManager.createGroup(channel, listener); + } } catch (SecurityException e) { LOG.info("Failed to create group: " + e); releaseHotspot(); @@ -283,7 +298,7 @@ class WifiDirectServiceImpl implements WifiDirectService, Service { LOG.info("Device name: " + device.deviceName); LOG.info("Device address: " + device.deviceAddress); LOG.info("Device is group owner: " + device.isGroupOwner()); - if (device.isGroupOwner() && !connectionInProgress) connectToPeer(device); + if (device.isGroupOwner()) connectToPeer(device, SDK_INT >= 29); } } }); @@ -292,25 +307,51 @@ class WifiDirectServiceImpl implements WifiDirectService, Service { } } - private void connectToPeer(WifiP2pDevice device) { - if (channel == null) return; + private void connectToPeer(WifiP2pDevice device, boolean useWellKnownNetwork) { + if (channel == null || connectionInProgress) return; connectionInProgress = true; - LOG.info("Connecting to peer " + device.deviceName); - WifiP2pConfig config = new WifiP2pConfig(); - config.deviceAddress = device.deviceAddress; - config.groupOwnerIntent = 0; - config.wps.setup = WpsInfo.PBC; - ActionListener listener = new ActionListener() { - @Override - public void onSuccess() { - LOG.info("Connected to peer " + device.deviceName); - } + WifiP2pConfig config; + ActionListener listener; + if (SDK_INT >= 29 && useWellKnownNetwork) { + // Try the well-known network details - no user interaction required, if it works + LOG.info("Connecting to peer " + device.deviceName + " using well-known network"); + config = new WifiP2pConfig.Builder() + .setDeviceAddress(MacAddress.fromString(device.deviceAddress)) + .setNetworkName(NETWORK_NAME) + .setPassphrase(PASSPHRASE) + .build(); + listener = new ActionListener() { + @Override + public void onSuccess() { + LOG.info("Connected to peer " + device.deviceName); + } - @Override - public void onFailure(int reason) { - LOG.info("Failed to connect to peer " + device.deviceName + ": " + reason); - } - }; + @Override + public void onFailure(int reason) { + LOG.info("Failed to connect to peer " + device.deviceName + ": " + reason); + // Fall back to WPS + connectToPeer(device, false); + } + }; + } else { + // Use WPS - requires user interaction unless the group has been used before and saved + LOG.info("Connecting to peer " + device.deviceName + " using WPS"); + config = new WifiP2pConfig(); + config.deviceAddress = device.deviceAddress; + config.groupOwnerIntent = 0; + config.wps.setup = WpsInfo.PBC; + listener = new ActionListener() { + @Override + public void onSuccess() { + LOG.info("Connected to peer " + device.deviceName); + } + + @Override + public void onFailure(int reason) { + LOG.info("Failed to connect to peer " + device.deviceName + ": " + reason); + } + }; + } try { requireNonNull(wifiP2pManager).connect(channel, config, listener); } catch (SecurityException e) { -- GitLab