来自不同平台的Wi-Fi P2P服务发现

时间:2019-02-22 20:39:53

标签: android wifi-direct wifip2p

我正在开发一个使用WiFi Direct查找服务的Android应用程序。我正在关注文档,并且一切正常。要创建服务:

private void startRegistration() {
    //  Create a string map containing information about your service.
    Map record = new HashMap();
    record.put("listenport", String.valueOf(SERVER_PORT));
    record.put("buddyname", "John Doe" + (int) (Math.random() * 1000));
    record.put("available", "visible");

    // Service information.  Pass it an instance name, service type
    // _protocol._transportlayer , and the map containing
    // information other devices will want once they connect to this one.
    WifiP2pDnsSdServiceInfo serviceInfo =
            WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record);

    // Add the local service, sending the service info, network channel,
    // and listener that will be used to indicate success or failure of
    // the request.
    manager.addLocalService(channel, serviceInfo, new ActionListener() {
        @Override
        public void onSuccess() {
            // Command successful! Code isn't necessarily needed here,
            // Unless you want to update the UI or add logging statements.
        }

        @Override
        public void onFailure(int arg0) {
            // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
        }
    });
}

然后是在其他设备上发现它的代码:

final HashMap<String, String> buddies = new HashMap<String, String>();
// ...
private void discoverService() {
    DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
    @Override
    /* Callback includes:
     * fullDomain: full domain name: e.g "printer._ipp._tcp.local."
     * record: TXT record dta as a map of key/value pairs.
     * device: The device running the advertised service.
     */

    public void onDnsSdTxtRecordAvailable(
            String fullDomain, Map record, WifiP2pDevice device) {
            Log.d(TAG, "DnsSdTxtRecord available -" + record.toString());
            buddies.put(device.deviceAddress, record.get("buddyname"));
        }
    };
}

一切正常。但是,如果我想发现Windows计算机提供的服务怎么办?还是iOS设备?我认为我的DnsSdTxtRecordListener找不到它们,因此我可以看到该服务的记录/变量。有什么办法可以做到这一点?

我查看了在Windows机器上创建(WiFi Direct)服务的文档,其实现方式略有不同...因此我不确定我要做什么是可以实现的吗?

0 个答案:

没有答案