Search Unity

Trying to connect multiple devices

Discussion in 'Multiplayer' started by robo orlo, Jul 1, 2015.

  1. robo orlo

    robo orlo

    Joined:
    Mar 19, 2015
    Posts:
    4
    Hello,


    I’m working on a multiplayer setup that involves two different projects, one on an android phone and a separate project on a wearable device. The android phone project acts as the server, while the wearable device project acts as a client. Im also using the Lost Polygon Android bluetooth multiplayer app to create a bluetooth connection. However, I cannot get the server to recognize the client connection. I’ve tried with several different Ip addresses and ports, but the outcome remains the same: the client is reporting a connection to the server, while the server does not see the client, and they cannot send RPC’s nor commands back and forth. I’ve included code snippets below, along with code snippets from the previous version. I’ve also received a null reference error, thrown by the client side after connection. However, I can’t find any explanation of it online. Does anyone have an idea if this sort of thing is still possible? Thank you, have a good day!


    Server-side code pre 5.1:

    Code (CSharp):
    1. public ushort multiBluetoothPort = 28000;
    2. kPort = (int)multiBluetoothPort;
    3. serverFlag = AndroidBluetoothMultiplayer.StartServer(multiBluetoothPort);
    4. Network.InitializeServer(2, kPort, false);
    Client-side code pre 5.1

    Code (CSharp):
    1. public ushort multiBluetoothPort = 28000;
    2. public const string kLocalIp = "127.0.0.1";
    3. AndroidBluetoothMultiplayer.Connect(bDevice.Address, multiBluetoothPort);
    4.  
    5. Debug.Log ("Attempting to forge network connection");
    6. Network.Connect(kLocalIp, (int)multiBluetoothPort);
    Server-side post 5.1

    Code (CSharp):
    1. public class BluetoothAccess : NetworkManager
    2. {
    3.  
    4.   public const string kLocalIp = "127.0.0.1";
    5.   public ushort multiBluetoothPort = 28000;
    6.   kPort = (int)multiBluetoothPort;
    7.   serverFlag = AndroidBluetoothMultiplayer.StartServer(multiBluetoothPort);
    8.   this.networkPort = kPort;
    9.   this.networkAddress = kLocalIp;
    10.  
    11.   if(GUI.Button(new Rect(10,10,100,50), "Create server"))
    12.   {
    13.     StartServer();
    14.   }
    15. }
    16.  
    17. public override void OnClientConnect (NetworkConnection conn)
    18.   {
    19.     Debug.Log("Network Client Connected");
    20.     base.OnClientConnect (conn);
    21.     NetworkServer.SetClientReady(conn);
    22.     NetworkServer.SpawnObjects();
    23.   }
    Client-side post 5.1

    Code (CSharp):
    1. public ushort multiBluetoothPort = 28000;
    2. public const string kLocalIp = "127.0.0.1";
    3. AndroidBluetoothMultiplayer.Connect(bDevice.Address, multiBluetoothPort);
    4. networkClient.Connect(kLocalIp, (int)multiBluetoothPort);
    5. networkClient.RegisterHandler(MsgType.Connect, OnConnected);
    6. public void OnConnected(NetworkMessage netMsg)
    7.   {
    8.        Debug.Log("Connected to server");
    9.   }

    Here was the aforementioned error.
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. at UnityEngine.Networking.NetworkClient.OnCRC (UnityEngine.Networking.NetworkMessage netMsg) [0x00000] in <filename unknown>:0
    3. at UnityEngine.Networking.NetworkConnection.HandleMessage (System.Collections.Generic.Dictionary`2 handler, UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) [0x00000] in <filename unknown>:0
    4. at UnityEngine.Networking.NetworkClient.Update () [0x00000] in <filename unknown>:0
    5. at UnityEngine.Networking.NetworkClient.UpdateClients () [0x00000] in <filename unknown>:0
    6. at UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () [0x00000] in <filename unknown>:0
    7.  
     
  2. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    serverFlag = AndroidBluetoothMultiplayer.StartServer(multiBluetoothPort); this function won't work with NetworkManager event system. So yes client will succsefully connected, but NetworkManager won't have any chance to be notified about this.
     
  3. robo orlo

    robo orlo

    Joined:
    Mar 19, 2015
    Posts:
    4
    I looked into that a bit, and you are correct, I wasn't using the right handler to notify me if a client had connected. what I needed to do, if I wanted to set up some kind of debug messaging, was override the OnServerConnect method of the NetworkManager class as opposed to the OnClientConnect method, like so:

    Code (CSharp):
    1. public override void OnServerConnect (NetworkConnection conn)
    2.     {
    3.         Debug.Log("Network Client Connected");
    4.         connectionflag = true;
    5.         base.OnServerConnect (conn);
    6.     }
    Thanks!