8

我正在尝试使用两个具有对等 WiFi 连接的 Android 设备来构建服务器-客户端架构。我在客户端和服务器之间有明显的区别,所以我试图删掉不必要的代码。使用来自http://developer.android.com/training/connect-devices-wireless/nsd-wifi-direct.html的指示,我有...

点对点服务

public class PeerToPeerService {
    private static final Logger LOG = LoggerFactory.getLogger(PeerToPeerService.class);

    private WifiP2pManager mManager;
    private WifiP2pManager.Channel mChannel;
    private final Context mContext;
    private final WifiP2pDnsSdServiceInfo mService;

    public PeerToPeerService(Context context, String name) {
        mContext = context;

        Map<String, String> record = new HashMap<String, String>();
        record.put(Constants.SERVICE_PROPERTY_NAME, name);
        record.put(Constants.SERVICE_PROPERTY_PORT, "12345");

        mService = WifiP2pDnsSdServiceInfo.newInstance(
                "_test", "_presence._tcp", record);
    }

    public void start() {
        mManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(mContext, mContext.getMainLooper(), null);

        mManager.addLocalService(mChannel, mService, new ActionListener() {
            @Override
            public void onSuccess() {
                LOG.info("Started Service");
            }

            @Override
            public void onFailure(int error) {
                LOG.warn("Failed to Start Service: {}", error);
            }
        });
    }

    public void stop() {
        mManager.removeLocalService(mChannel, mService, null);
        mManager = null;
        LOG.info("Stopped Service");
    }
}

点对点客户端

public class PeerToPeerClient {
    private static final Logger LOG = LoggerFactory.getLogger(PeerToPeerClient.class);

    private WifiP2pManager mManager;
    private WifiP2pManager.Channel mChannel;
    private final Context mContext;

    public PeerToPeerClient(Context context) {
        mContext = context;
    }

    public void findServices() {
        mManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(mContext, mContext.getMainLooper(), null);

        mManager.setDnsSdResponseListeners(mChannel,
                new DnsSdServiceResponseListener() {
                    @Override
                    public void onDnsSdServiceAvailable(String instanceName,
                            String registrationType, WifiP2pDevice device) {
                        LOG.info("A");
                        LOG.info("Service Found: {}:{}", instanceName, registrationType);
                    }
                }, 
                new DnsSdTxtRecordListener() {
                    @Override
                    public void onDnsSdTxtRecordAvailable(String fullDomainName, Map<String, String> record,
                            WifiP2pDevice device) {
                        LOG.info("B");
                        LOG.info("{}:{} is {}", device.deviceName, 
                                record.get(Constants.SERVICE_PROPERTY_PORT), record.get(Constants.SERVICE_PROPERTY_NAME));
                    }
                });

        WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
        mManager.addServiceRequest(mChannel, serviceRequest,
                new ActionListener() {
                    @Override
                    public void onSuccess() {
                        LOG.info("Added service discovery request");
                    }

                    @Override
                    public void onFailure(int error) {
                        LOG.info("Failed adding service discovery request: {}", error);
                    }
                });

        mManager.discoverServices(mChannel, new ActionListener() {
            @Override
            public void onSuccess() {
                LOG.info("Service discovery initiated");
            }

            @Override
            public void onFailure(int arg0) {
                LOG.info("Service discovery failed");
            }
        });
    }
}

每个单独的 ActionListener 都调用了 onSuccess,任何地方都没有失败。但我从来没有收到任何 setDnsSdResponseListeners 回调。有什么想法我可能出错了吗?

4

4 回答 4

6

我有一些在 Android 设备上使用 Wi-Fi Direct 的经验,我正在编写一个应用程序来临时连接设备并启用通过此链接聊天。

要在 Wi-Fi Direct 上被发现,两个设备都需要进行扫描,即。mWifiP2pManager.discoverPeers(). 奇怪的是,这也影响了DnsTxtRecordServices. 因此,我发现如果您scanning( discoverPeers())在两台设备上都启动它然后启动discoverPeers().

希望这可以帮助!:)

于 2015-01-09T07:09:12.587 回答
0

我认为您错过了一些要点:

PeerToPeerService : 服务器代码

1.实现客户发现

PeerToPeerClient :客户端代码

1.实现addint客户端本地服务

将被服务器发现

对等服务:

    // after adding local service
    manager.setDnsSdResponseListeners(channel,
            new DnsSdServiceResponseListener() {
             ...
    }

对等客户端:

mChannel = mManager.initialize(mContext, mContext.getMainLooper(), null);
// add  client local service
mManager.addLocalService(mChannel, mService, new ActionListener() {
            @Override
            public void onSuccess() {
                LOG.info("Started Service");
            }

            @Override
            public void onFailure(int error) {
                LOG.warn("Failed to Start Service: {}", error);
            }
        });

在此代码之后,您将能够获得回调onDnsSdResponseListener并可以连接

连接后,您可以通过 WifiP2pInfo.isGroupOwner 检查您是服务器端还是客户端 - 服务器为 true,客户端为 false

于 2015-04-02T04:58:39.670 回答
0

addLocalService对于客户端和服务器,您必须调用discoverServicesWifiP2p 才能工作。

Map record = new HashMap();
record.put("name","your_app_anme");

WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence.tcp", record);
wifiP2pManager.addLocalService(wifiP2pChannel, serviceInfo, null);


wifiP2pManager.setDnsSdResponseListeners(wifiP2pChannel, new WifiP2pManager.DnsSdServiceResponseListener() {
    @Override
    public void onDnsSdServiceAvailable(String s, String s1, WifiP2pDevice wifiP2pDevice) {
        Log.wtf("WTF", "device "+wifiP2pDevice.deviceName + "/"+wifiP2pDevice.deviceAddress);
    }
}, new WifiP2pManager.DnsSdTxtRecordListener() {
    @Override
    public void onDnsSdTxtRecordAvailable(String s, Map<String, String> map, WifiP2pDevice wifiP2pDevice) {
        Log.wtf("WTF", "got txt "+map.get("name") );
    }
});

WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
wifiP2pManager.addServiceRequest(wifiP2pChannel, serviceRequest, null);
wifiP2pManager.discoverServices(wifiP2pChannel, null);
于 2015-08-27T08:01:47.120 回答
0

经过大量尝试,我得到了如下强大的可行流程:

...
wifiP2pManager.clearLocalServices(wifiP2pChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                HashMap<String, String> 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) {
            }
        });
于 2016-11-04T01:55:52.607 回答