2

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
KristianR
  • 420
  • 2
  • 6

1 Answers1

1

If it may help someone in the future, I can tell that I did solve my issue. The problem was that I used the subscriber session to invoke createNetworkSpecifierOpen on both sides. I had to use the publisher session on one side and the subscriber session on the other side (initiator/responder) to make it work.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
KristianR
  • 420
  • 2
  • 6
  • Thanks! Did you manage to connect more than 2 devices though? – kar Jun 10 '19 at 14:20
  • 1
    I don’t remember fully, but I think there was a such a limitation in 8.0. The data link capability was increased to two concurrent data connections in 8.1 (through a combination of Android SDK and wifi firmware update) . Connecting/disconnecting and managing links was a pain though and was quite unstable :( – KristianR Jun 11 '19 at 15:19