Пример #1
0
    /// <summary>
    /// Join a discovered network.
    /// </summary>
    ///
    /// <remarks>
    /// Use CountNumNetworkMembers() afterwards to confirm whether or not you are connected to
    /// the network.
    /// </remarks>
    ///
    /// <param name="network">Network to join. This can be obtained from GetDiscoveredNetworks().</param>
    ///
    /// <returns>
    /// Call GetJoinNetworkStatus() afterwards to retrieve the status of this callback function:
    ///  -2 if not ready yet.
    ///  -1 if success.
    ///  otherwise, failure.
    /// </returns>
    public void JoinNetwork(NetworkP2PInfo network)
    {
        string macAddress = network.mMacAddress;

                #if UNITY_ANDROID && !UNITY_EDITOR
        mServiceDiscovery.Call("connectToService", macAddress);
        #else
        Debug.Log("WARNING: WifiP2P object is disabled when not running on Android device.");
        Debug.Log("Ignore: " + macAddress);
        #endif
    }
Пример #2
0
    /// <summary>
    /// Retrieve a list of discovered networks.
    /// </summary>
    ///
    /// <remarks>
    /// Precond :: DiscoverNetworks() was called beforehand and successful.
    /// </remarks>
    ///
    /// <returns>
    /// An empty list will be returned if there currently isn't any discovered networks yet. You can call this
    /// repeatedly until the returned list is non-empty.
    ///
    /// You can access each NetworkP2PInfo class members from the list to gain info about the discovered network
    /// such as its name. Pass the NetworkP2PInfo to JoinNetwork() to join the network.
    /// </returns>
    public List <NetworkP2PInfo> GetDiscoveredNetworks()
    {
                #if UNITY_ANDROID && !UNITY_EDITOR
        AndroidJavaObject jNetworks = mServiceDiscovery.Call <AndroidJavaObject>("getDiscoveredServices");
        int numNetworks             = jNetworks.Call <int>("size");

        List <NetworkP2PInfo> networkList = new List <NetworkP2PInfo>();
        for (int i = 0; i < numNetworks; i++)
        {
            AndroidJavaObject jService = jNetworks.Call <AndroidJavaObject>("get", i);
            NetworkP2PInfo    nwInfo   = new NetworkP2PInfo(jService);
            networkList.Add(nwInfo);
        }

        return(networkList);
        #else
        Debug.Log("WARNING: WifiP2P object is disabled when not running on Android device.");
        return(null);
        #endif
    }
    public void GetServerIP(string roomCode)
    {
        // If host is wanting its own local IP address
        if (roomCode == null)
        {
            mResult = mWifiP2P.GetNetworkOwnerIP();
            return;
        }

        mResult = null;

        mRoomCode = roomCode;

        mNextSubCall = Time.time;
        mEndTime     = Time.time + mGetServerIPTimeoutSec;

        mGetServerIPSubStatus = GetServerIPSubStatus.Discovering;

        mGetDiscoveredNetworksAttempts = 0;
        mDiscoveredNetwork             = null;

        // Tell Update() to do the work.
        mGetServerIPTriggered = true;
    }
 protected void GetServerIP_Update()
 {
     if (mGetServerIPTriggered)
     {
         // Stop trying if exceeded time allocated.
         if (Time.time > mEndTime)
         {
             if (mIsDebug)
             {
                 Debug.Log("ServerLookupWifiDirect: GetServerIPUpdate() timeout.");
             }
             mResult = "failure";
             mGetServerIPTriggered = false;
         }
         else if (Time.time > mNextSubCall)
         {
             // Turn on network discovery.
             if (mGetServerIPSubStatus == GetServerIPSubStatus.Discovering)
             {
                 if (mIsDebug)
                 {
                     Debug.Log("ServerLookupWifiDirect: Calling DiscoverNetworks().");
                 }
                 mWifiP2P.DiscoverNetworks(mWifiDirectServiceName);
                 mGetServerIPSubStatus = GetServerIPSubStatus.Discovered;
                 mNextSubCall          = Time.time + mSubCallCoolDown;
             }
             else if (mGetServerIPSubStatus == GetServerIPSubStatus.Discovered)
             {
                 if (mWifiP2P.GetDiscoverNetworksStatus_part1of2() == -1 &&
                     mWifiP2P.GetDiscoverNetworksStatus_part2of2() == -1)
                 {
                     // Success.
                     if (mIsDebug)
                     {
                         Debug.Log("ServerLookupWifiDirect: DiscoverNetworks() success.");
                     }
                     mGetServerIPSubStatus = GetServerIPSubStatus.DiscoveryFetching;
                     mNextSubCall          = Time.time + mSubCallCoolDown;
                 }
                 else
                 {
                     // Try again.
                     if (mIsDebug)
                     {
                         Debug.Log("ServerLookupWifiDirect: DiscoverNetworks() failure.");
                     }
                     mGetServerIPSubStatus = GetServerIPSubStatus.Discovering;
                     mNextSubCall          = Time.time + mSubErrCoolDown;
                 }
             }
             // Fetch discovered networks.
             else if (mGetServerIPSubStatus == GetServerIPSubStatus.DiscoveryFetching)
             {
                 if (mIsDebug)
                 {
                     Debug.Log("ServerLookupWifiDirect: Calling GetDiscoveredNetworks().");
                 }
                 List <NetworkP2PInfo> nws = mWifiP2P.GetDiscoveredNetworks();
                 mDiscoveredNetwork = SearchNetworkP2PInfo(mRoomCode, nws);
                 if (mDiscoveredNetwork != null)
                 {
                     if (mIsDebug)
                     {
                         Debug.Log("ServerLookupWifiDirect: GetDiscoveredNetworks() success.");
                     }
                     mGetServerIPSubStatus = GetServerIPSubStatus.Joining;
                     mNextSubCall          = Time.time;
                 }
                 else
                 {
                     mGetDiscoveredNetworksAttempts++;
                     if (mIsDebug)
                     {
                         Debug.Log("ServerLookupWifiDirect: GetDiscoveredNetworks() failure ("
                                   + mGetDiscoveredNetworksAttempts + "/" + mMaxGetDiscoveredNetworksAttempts + ")");
                     }
                     if (mGetDiscoveredNetworksAttempts < mMaxGetDiscoveredNetworksAttempts)
                     {
                         // Try fetching again.
                         mNextSubCall = Time.time + mSubCallCoolDown;
                     }
                     else                         // Exhausted fetching attempts.
                     {
                         // Try discovering again.
                         mGetServerIPSubStatus          = GetServerIPSubStatus.Discovering;
                         mNextSubCall                   = Time.time;
                         mGetDiscoveredNetworksAttempts = 0;
                     }
                 }
             }
             // Join fetched network.
             else if (mGetServerIPSubStatus == GetServerIPSubStatus.Joining)
             {
                 if (mIsDebug)
                 {
                     Debug.Log("ServerLookupWifiDirect: Calling JoinNetwork().");
                 }
                 mWifiP2P.JoinNetwork(mDiscoveredNetwork);
                 mGetServerIPSubStatus = GetServerIPSubStatus.Joined;
                 mNextSubCall          = Time.time + mSubJoinCallCoolDown;
             }
             else if (mGetServerIPSubStatus == GetServerIPSubStatus.Joined)
             {
                 // Successful to join, but ensure can retrieve server host's IP.
                 if (mWifiP2P.GetJoinNetworkStatus() == -1)
                 {
                     if (mIsDebug)
                     {
                         Debug.Log("ServerLookupWifiDirect: JoinNetwork() success.");
                     }
                     string hostIP = mWifiP2P.GetNetworkOwnerIP();
                     if (hostIP != "")
                     {
                         if (mIsDebug)
                         {
                             Debug.Log("ServerLookupWifiDirect: GetNetworkOwnerIP() success - Host IP: " +
                                       hostIP + ".");
                         }
                         // Success on retrieve host IP
                         mWifiP2P.StopDiscoverNetworks();
                         mResult = hostIP;
                         mGetServerIPTriggered = false;
                     }
                     else
                     {
                         if (mIsDebug)
                         {
                             Debug.Log("ServerLookupWifiDirect: GetNetworkOwnerIP() failure.");
                         }
                         // Failure to retrieve host IP. Try joining again.
                         mWifiP2P.CancelJoinNetwork();
                         mGetServerIPSubStatus = GetServerIPSubStatus.Joining;
                         mNextSubCall          = Time.time + mSubErrCoolDown;
                     }
                 }
                 // Failure to join.
                 else
                 {
                     if (mIsDebug)
                     {
                         Debug.Log("ServerLookupWifiDirect: JoinNetwork() failure.");
                     }
                     mWifiP2P.CancelJoinNetwork();
                     mGetServerIPSubStatus = GetServerIPSubStatus.Joining;
                     mNextSubCall          = Time.time + mSubErrCoolDown;
                 }
             }
         }
     }
 }