如何使用SERVICE_TYPE_ALL协议类型在android上发现wifi直接服务?

问题描述:

我试图将我的应用程序设置为使用wifi直接服务发现来发现所有正在广告的wifi直接服务。以下是我的代码片段:如何使用SERVICE_TYPE_ALL协议类型在android上发现wifi直接服务?

void discoverAllServices() { 

    manager.setServiceResponsListener(channel, new WifiP2pManager.ServiceResponseListener() { 

     @Override 
     public void onServiceAvailable(int protocolType, byte[] responseData, 
                WifiP2pDevice srcDevice) { 

      Log.d(TAG, "ProtocolType -> " + protocolType); 
     } 
    }); 

    serviceRequestForAll = WifiP2pServiceRequest.newInstance(WifiP2pServiceInfo.SERVICE_TYPE_ALL); 

    manager.addServiceRequest(channel, serviceRequestForAll, new ActionListener() { 

     @Override 
     public void onSuccess() { 
      Log.d(TAG, "Added service all discovery request"); 
     } 

     @Override 
     public void onFailure(int arg0) { 
      Log.d(TAG, "Failed adding service all discovery request"); 
     }  
    }); 

    manager.discoverServices(channel, new ActionListener() { 

     @Override 
     public void onSuccess() { 
      Log.d(TAG, "ServiceAll discovery initiated"); 
     } 

     @Override 
     public void onFailure(int arg0) { 
      Log.d(TAG, "ServiceAll discovery failed"); 
     }  
    }); 
} 

我在添加服务请求和discoverServices()调用成功时获得成功。但没有发现服务。当我有附近的wifi直接服务广告。我需要做什么才能发现正在广告的wifi直接服务。

我能想到的两件事...

1)确保初始化管理器。 2)确保您拥有在清单中声明的​​适当权限。