Skip to content
Snippets Groups Projects
Commit 452bc5d3 authored by Sebastian Kürten's avatar Sebastian Kürten
Browse files

On API < 29 wait for Wi-Fi p2p broadcast before creating group

parent c7a83cd3
No related branches found
No related tags found
1 merge request!15Try starting the WifiP2p framework multiple times with delay if BUSY
package org.briarproject.hotspot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pGroup;
......@@ -23,8 +26,12 @@ import static android.net.wifi.WifiManager.WIFI_MODE_FULL_HIGH_PERF;
import static android.net.wifi.p2p.WifiP2pConfig.GROUP_OWNER_BAND_2GHZ;
import static android.net.wifi.p2p.WifiP2pManager.BUSY;
import static android.net.wifi.p2p.WifiP2pManager.ERROR;
import static android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE;
import static android.net.wifi.p2p.WifiP2pManager.NO_SERVICE_REQUESTS;
import static android.net.wifi.p2p.WifiP2pManager.P2P_UNSUPPORTED;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_DISABLED;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED;
import static android.os.Build.VERSION.SDK_INT;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;
......@@ -103,8 +110,10 @@ class HotspotManager implements ActionListener {
.setPassphrase(passphrase)
.build();
wifiP2pManager.createGroup(channel, config, this);
} else {
wifiP2pManager.createGroup(channel, this);
} else if (!receiverRegistered) {
ctx.registerReceiver(receiver, new IntentFilter(
WIFI_P2P_STATE_CHANGED_ACTION));
receiverRegistered = true;
}
} catch (SecurityException e) {
// this should never happen, because we request permissions before
......@@ -112,6 +121,46 @@ class HotspotManager implements ActionListener {
}
}
// Only used on API < 29
private boolean receiverRegistered = false;
final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
@UiThread
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null ||
!intent.getAction().equals(WIFI_P2P_STATE_CHANGED_ACTION)) {
return;
}
int state = intent.getIntExtra(EXTRA_WIFI_STATE,
WIFI_P2P_STATE_DISABLED);
if (state == WIFI_P2P_STATE_ENABLED) {
try {
wifiP2pManager.createGroup(channel, HotspotManager.this);
} catch (SecurityException e) {
// this should never happen, because we request permissions before
throw new AssertionError(e);
}
unregisterReceiver();
}
}
};
private void unregisterReceiver() {
if (!receiverRegistered)
return;
try {
ctx.unregisterReceiver(receiver);
receiverRegistered = false;
} catch (IllegalArgumentException e) {
// This gets thrown when we try to unregister an already unregistered
// receiver. We do keep a flag whether the receiver is actually
// registered, so it should not happen.
throw new AssertionError(e);
}
}
@RequiresApi(29)
private String getNetworkName() {
return "DIRECT-" + getRandomString(2) + "-" +
......@@ -122,7 +171,10 @@ class HotspotManager implements ActionListener {
return getRandomString(8);
}
@UiThread
void stopWifiP2pHotspot() {
if (SDK_INT < 29)
unregisterReceiver();
if (channel == null) return;
wifiP2pManager.removeGroup(channel, new ActionListener() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment