为什么我无法在Pixel 2

时间:2017-11-15 10:33:28

标签: java android wifi pixel

我使用两个设备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

1 个答案:

答案 0 :(得分:0)

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