2

我使用两个设备 Pixel 2,它们都发布和订阅相同的服务。

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);

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);

在此之前,我已验证 Wifi Aware 受支持且当前可用

    // 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!");
    }

我收到两个设备的发现事件,即双方都调用了 onServiceDiscovered,并且我能够使用 sendMessage 交换转发消息。但是我无法设置网络。我愿意

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);

双方都调用 callback.onUnavailable()。我是否以错误的方式执行请求?就文档而言,远程设备应自动接受连接

“响应者不需要提供 MAC 地址或 PeerHandle。如果未指定 MAC 地址或 PeerHandle,则设备接受所有连接请求。” https://developer.android.com/guide/topics/connectivity/wifi-aware.html

4

1 回答 1

1

如果它将来可能对某人有所帮助,我可以说我确实解决了我的问题。问题是我使用订阅者会话来调用createNetworkSpecifierOpen双方。我必须在一侧使用发布者会话,在另一侧(发起者/响应者)使用订阅者会话才能使其工作。

于 2018-07-07T21:08:11.333 回答