Why is discovering peers for Android Wi-Fi Direct so unreliable?

前端 未结 4 599
礼貌的吻别
礼貌的吻别 2020-12-25 10:24

I am experimenting with Android\'s Wi-Fi Direct (or P2P if you prefer). I thought it was working very nicely between my two phones, but I realized I am encountering issues w

4条回答
  •  盖世英雄少女心
    2020-12-25 10:44

    From my experience it's reliable. After tons of trying, I got the robust workable flow like this:

    ...
    wifiP2pManager.clearLocalServices(wifiP2pChannel, new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                    HashMap record = new HashMap<>();
                    record.put("name", "Amos");
                    WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(AppConfig.DNS_SD_SERVICE_NAME, AppConfig.DNS_SD_SERVICE_TYPE, record);
                    wifiP2pManager.addLocalService(wifiP2pChannel, serviceInfo, new WifiP2pManager.ActionListener() {
                        @Override
                        public void onSuccess() {
                            wifiP2pManager.setDnsSdResponseListeners(wifiP2pChannel, WifiDirectFragment.this, WifiDirectFragment.this);
                            wifiP2pManager.clearServiceRequests(wifiP2pChannel, new WifiP2pManager.ActionListener() {
                                @Override
                                public void onSuccess() {
                                    wifiP2pManager.addServiceRequest(wifiP2pChannel, WifiP2pDnsSdServiceRequest.newInstance(), new WifiP2pManager.ActionListener() {
                                        @Override
                                        public void onSuccess() {
                                            wifiP2pManager.discoverPeers(wifiP2pChannel, new WifiP2pManager.ActionListener() {
                                                @Override
                                                public void onSuccess() {
                                                    wifiP2pManager.discoverServices(wifiP2pChannel, new WifiP2pManager.ActionListener() {
                                                        @Override
                                                        public void onSuccess() {
                                                            // this is my recursive discovery approach                                                            
                                                            handler.postDelayed(discoveryRunnable, AppConfig.DNS_SD_SERVICE_DISCOVERABLE_DURATION_S * 1000);
                                                        }
    
                                                        @Override
                                                        public void onFailure(int code) {
                                                        }
                                                    });
                                                }
    
                                                @Override
                                                public void onFailure(int code) {
                                                }
                                            });
                                        }
    
                                        @Override
                                        public void onFailure(int code) {
                                        }
                                    });
                                }
    
                                @Override
                                public void onFailure(int code) {
                                }
                            });
                        }
    
                        @Override
                        public void onFailure(int code) {
                        }
                    });
                }
    
                @Override
                public void onFailure(int code) {
                }
            });
    

提交回复
热议问题