I use two devices Pixel 2 which both publish and subscribe to the same service.
android.net.wifi.aware.PublishConfig cfg = new android.net.wifi.aware.PublishConfig.Builder()
.setServiceName(name).setPublishType(android.net.wifi.aware.PublishConfig.PUBLISH_TYPE_UNSOLICITED)
.setServiceSpecificInfo(serviceInfo)
.setTerminateNotificationEnabled(true)
.setTtlSec(0)
.build();
WiFiAwarePublishContext cbk = new WiFiAwarePublishContext(mConnectivityListener, isConnect);
id = cbk.id;
mSession.publish(cfg, cbk, mEventHandler);
and
android.net.wifi.aware.SubscribeConfig cfg = new android.net.wifi.aware.SubscribeConfig.Builder()
.setServiceName(name)
.setSubscribeType(android.net.wifi.aware.SubscribeConfig.SUBSCRIBE_TYPE_PASSIVE)
.setServiceSpecificInfo(serviceInfo)
.setTerminateNotificationEnabled(true)
.setTtlSec(0)
.build();
WiFiAwareSubscribeContext cbk = new WiFiAwareSubscribeContext(mContext, mConnectivityListener, isConnect);
id = cbk.id;
mSession.subscribe(cfg, cbk, mEventHandler);
Prior to this I have verified that Wifi Aware is supported and currently available
// Initialize Wi-Fi Aware
mWifiAwareManager = (WifiAwareManager) mContext.getSystemService(Context.WIFI_AWARE_SERVICE);
mContext.registerReceiver(mReceiver, mIntentFilter);
/* This broadcast is not sticky, using the isAvailable()
* API after registering the broadcast to check the current
* state of Wi-Fi Aware. */
if (mWifiAwareManager.isAvailable()) {
Log.i(TAG, "Wi-Fi Aware is available");
} else {
Log.e(TAG, "Wi-Fi Aware NOT available!");
}
I receive discovery events of both devices, i.e. onServiceDiscovered is called on both sides and I am able to exchange forward messages using sendMessage. However I am not able to setup a network. I do
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI_AWARE)
.setNetworkSpecifier(subscribeSession.createNetworkSpecifierOpen(peerHandle))
.build();
ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.i(TAG, "onAvailable: " + network);
// TODO: send onDiscovery event here with IP
}
@Override
public void onLosing(Network network, int maxMsToLive) {
Log.i(TAG, "onLosing: " + network);
}
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
Log.i(TAG, "onLinkPropertiesChanged: " + network + '\n' + linkProperties);
}
@Override
public void onLost(Network network) {
Log.i(TAG, "onLost: " + network);
// TODO: onDiscoveryTeardown?
}
@Override
public void onUnavailable() {
Log.e(TAG, "onUnavailable");
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.i(TAG, "onCapabilitiesChanged: " + network);
}
};
ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
connMgr.requestNetwork(networkRequest, callback, 5000);
Both sides invoke callback.onUnavailable(). Am I performing the request in the wrong way? As far as the documentation goes the connection should be accepted automatically by the remote devices
"The responder isn't required to provide a MAC address or a PeerHandle. If no MAC address or PeerHandle is specified, the device accepts all connection requests." https://developer.android.com/guide/topics/connectivity/wifi-aware.html