Android Bluetooth Multiplayer [Released]

Discussion in 'Assets and Asset Store' started by ZimM, Jul 1, 2013.

  1. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Android Bluetooth Multiplayer 1.3.2
    $logo_only.png

    Asset Store - Pro version
    Asset Store - Basic version

    This plugin allows you to add Bluetooth multiplayer in a very simple way. It is fully compatible with Unity built-in networking. What this means is that you can easily reuse your networking code for Internet and local gaming with minimal changes, or use any of existing tutorials about Unity built-in networking.
    Ever wanted to add Bluetooth multiplayer to your Android game? Then this is the one and only plugin you will ever need.

    Features:
    • Full support for Unity 3.5.7 - 4.6.x versions (Free and Pro).
    • Simple and intuitive API.
    • Event-driven for advanced custom interaction.
    • Full documentation included.
    • Documented demo examples.
    Support:
    For any questions about this plugin, feel free to contact me at contact at lostpolygon.com or post in this thread.​

    Version history:
    1.0:
    - initial release

    1.0.3:
    - minor fixes

    1.1:
    - add device discovery API (with demo usage example)
    - fix manifest generation on Mac OS X
    - minor fixes

    1.2:
    - BluetoothMultiplayerAndroidManager is now instantiated automatically, no need to add prefab
    - new GetCurrentDevice() method to get current Bluetooth device information
    - new SetRawPackets() method to transmit data as raw (for advanced usage)

    1.2.1-1.2.2:
    - minor fixes

    1.2.3:
    - fixed an issue when client wasn’t disconnecting from server

    1.3.0:
    - new IsDiscoverable() method
    - added a parameter to ShowDeviceList() for showing only data-capable devices
    - added detection of the Bluetooth device class
    - improved integration with other Android plugins
    - improved demo scenes
    - code clean-up

    1.3.1:
    - Fixed an issue when GetDiscoveredDevices() always returned empty array before starting the discovery process

    1.3.2:
    - Added onBluetoothDiscoverabilityEnabled and onBluetoothDiscoverabilityEnableFailed events

    Documentation:

    If you like the asset, please review/rate it on the Asset Store, I'll greatly appreciate that :)
    Last edited: Sep 12, 2014
  2. Patico

    Patico

    Member

    Joined:
    May 21, 2013
    Messages:
    619
    You're welcome! :p
    Wonderfull asset, I will buy it as soon as can.
  3. ina

    ina

    Member

    Joined:
    Nov 15, 2010
    Messages:
    329
    Does this work on iOS?
  4. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    I'm sorry, but this is Android only at the moment.
    Last edited: Jul 4, 2013
  5. ina

    ina

    Member

    Joined:
    Nov 15, 2010
    Messages:
    329
    The generate manifest option does not seem to work and patch manifest is silent fail but reads succeed - you might want to include a generic AndroidManifest.xml with bluetooth permissions

    Exception: Can't generate AndroidManifest.xml!
    ManifestGenerator.GenerateManifest () (at Assets/Editor/BluetoothMultiplayerAndroid/ManifestGenerator.cs:31)
  6. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    That's odd... What Unity version and operating system you are using?
    I am investigating the issue, but meanwhile, you can use this AndroidManifest.xml file I've attached (zipped as forum doesn't allow .xml attachments)
    View attachment $AndroidManifest.zip
    Please let me know if you have any more problems with the plugin.
  7. nazbert

    nazbert

    New Member

    Joined:
    May 6, 2013
    Messages:
    2
    First, thank you for the awesome plugin. I had noticed an issue with the Manifest generator as well. It is failing to find the local unity copy of the manifest. I am running on OSX and just needed to edit the filepath in GetManifestOriginal(). I am having an issue initializing the bluetooth server in my project. When the server initialization is attempted it fails.

    Here is a portion of the logcat

    Code (csharp):
    1. D/LostPolygonABT( 6538): Server - Starting
    2. D/BluetoothManagerService( 2185): checkIfCallerIsForegroundUser: valid=true callingUser=0 foregroundUser=0
    3. W/BluetoothAdapter( 6538): getBluetoothService() called with no BluetoothManagerCallback
    4. E/BluetoothServiceJni( 4283): SOCK FLAG = 3 ***********************
    5. D/LostPolygonABT( 6538): Server - Starting AcceptThread
    6. D/LostPolygonABT( 6538): Unity presence: True
    7. D/LostPolygonABT( 6538): Interop - Call event 'onBluetoothListeningStarted'
    8. D/Unity   ( 6538): SendMessage: object BluetoothMultiplayerAndroidManager does not have receiver for function SomeInexistantMethod!
  8. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Thanks for the feedback.
    Actually, as I can see from the 'onBluetoothListeningStarted' event being fired - it looks like server initialization is fine. Have you tried connecting to the server? UPD: What device and Android version do you have?

    The "object BluetoothMultiplayerAndroidManager does not have receiver for function SomeInexistantMethod!" message is not an actual error (it means pretty much nothing), but anyway, I've already fixed that as well as the manifest generation on OS X - the update is on it's way.
    Last edited: Jul 11, 2013
  9. nazbert

    nazbert

    New Member

    Joined:
    May 6, 2013
    Messages:
    2
    I had just made a rookie mistake and now everything is working fine. Thanks again.
  10. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    The 1.1 update is now live on Asset Store!

    Changes are:
    - device discovery API (with demo usage example);
    - fixed manifest generation on Mac OS X;
    - minor fixes.
  11. JamesArndt

    JamesArndt

    Member

    Joined:
    Dec 1, 2009
    Messages:
    1,582
    4.2 support yet?
  12. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    It works on 4.2 just as fine, no changes needed. I'll update the docs soon.
  13. AntonM

    AntonM

    Member

    Joined:
    Feb 15, 2013
    Messages:
    1
    How can I transfer raw data? If this is not possible, could you implement?
  14. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    The plugin was not really intended for that... But it is possible if you have Android Pro. As soon as the Bluetooth connection between client devices and the server is established, you can send your raw data as UDP packets to the 127.0.0.1:<port> and the data will travel over Bluetooth. Basically, you'll have to do all your raw communications via UDP sockets. All that will require some coding work, but it's definitely possible.
  15. thanhle

    thanhle

    Member

    Joined:
    May 2, 2013
    Messages:
    59
  16. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    The 1.2 update is submitted to the Asset Store and is expected to be approved soon.

    Changes are:
    - new GetCurrentDevice() method to get the current device information;
    - BluetoothMultiplayerAndroidManager is now instantiated automatically, no need to add prefab;
    - new SetRawPackets() method to transmit data as raw (for advanced usage).

    If you want to get the update before it is approved - just send me a PM.

    P.S. If you like the asset, please leave a review or rate the asset on the Asset Store, I'll greatly appreciate that :)
    Last edited: Aug 21, 2013
  17. Duiri

    Duiri

    New Member

    Joined:
    Sep 3, 2013
    Messages:
    2
    Hi there ...

    It is developing a Bluetooth multi-play you are developing a game of this time unity itself is very

    I purchased a plug-in called [BluetoothMultiplayerAndroid].

    Development is going over to some extent one problem occurred.

    (Scene is divided gameplay and main menu.) And then exit game play to get even better when you try to connect to the first

    The problem arises to make the (Server) in the back room.

    (OnBluetoothListeningStarted ()) is, for double initialization entering it twice, signal went out of the room initialization signal (onBluetoothDisconnectedFromServer () -> onBluetoothListeningCanceled ()) comes into the final those were exit of the room so as

    Process is.

    I think why, and know that there is a solution where signal turns on twice.
  18. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Hi,
    sorry, I really wan't to help, but I can't understand what do you mean... Could you please explain the problem in very simple English?
  19. Duiri

    Duiri

    New Member

    Joined:
    Sep 3, 2013
    Messages:
    2
    Second connection does not run
    (OnBluetoothListeningStarted ()) event is executed twice in a row
  20. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    I've sent you a PM, please check your Inbox.
  21. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Update:
    Version 1.2 is now available on the Asset Store!
  22. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Update:
    Version 1.21 is now available on the Asset Store!

    No serious changes, just some minor polish and fixes.
  23. kennetv

    kennetv

    New Member

    Joined:
    Jun 10, 2013
    Messages:
    4
    On what versions of Android does this plugin work?
    I have tested the demo on following devices...
    Nexus 7 4.3 Working
    Samsung 4.2.2 Working
    Nexus S 4.1.2 Not working
    Galaxy tab 8.9 4.0.4 Not working
    HTC Desire 2.2.2 Not working

    Thanks!
  24. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    The plugin supports Android 2.1 and higher, and I had no reports previously about it not working because of the Android version. Could you please explain what do you mean under "Not working"? There are two demo scene in the demo, what parts of functionality aren't working?
    Last edited: Sep 28, 2013
  25. kennetv

    kennetv

    New Member

    Joined:
    Jun 10, 2013
    Messages:
    4
    I'm using "Multiplayer demo".
    Sorry, didn't realize that I had to "Request discoverability" to pair devices first, almost all devices can connect now.
    But when my HTC (v. 2.2.2) acts server the other finds it but only one can "connect to server" at a time.
  26. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Yes, with some (old < v2.3) devices it may not work perfectly. But as the plugin uses only standard Bluetooth API, the only reason why that can happen is buggy Bluetooth stack implementation in that device Android build. I can't do anything with it, so it's just something you'll probably have to take in account.
    Last edited: Sep 28, 2013
  27. kennetv

    kennetv

    New Member

    Joined:
    Jun 10, 2013
    Messages:
    4
    OK, that's good enough for me! I'll go and pick it up from the asset store now :)
  28. ilu29

    ilu29

    New Member

    Joined:
    Jan 21, 2013
    Messages:
    4
    i want to communicate with arduino, do you know how can i do this?
  29. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    First of all, you'll need Android Pro, because this will require usage of UDP sockets. Basically, the plugin just tunnels the Bluetooth over UDP sockets.
    So there are two cases: when Android device initiates a Bluetooth connection, and when Arduino initiates a Bluetooth connection.

    When Arduino device initiates a Bluetooth connection:
    1) Select any random port number.
    2) Create a UDP socket and bind it to that port.
    3) Call BluetoothMultiplayerAndroid.Init().
    4) Call BluetoothMultiplayerAndroid.InitializeServer(port).
    5) Wait for connection to happen.
    6) When Arduino sends the data via Bluetooth, it will be received on the UDP socket you've created.
    7) To send data to Arduino, you have to send data via UDP to the same port from which you've received the data from Arduino.

    When Android device initiates a Bluetooth connection:
    1) Select any random port number.
    2) Create a UDP socket.
    3) Call BluetoothMultiplayerAndroid.Init().
    4) Call BluetoothMultiplayerAndroid.Connect(arduinoAddress, port).
    5) Wait for connection to happen.
    7) To send data to Arduino, send data via UDP to the port you've chosen.
    8) Data from Arduino will be received on your UDP port.

    This is probably not easy to grasp, and it's not really a user-friendly way to communicate with Arduino via Bluetooth (as the plugin wasn't initially designed for that), but it is not that hard to implement if you really need it.
    I haven't tested this personally, as I don't have an Arduino Bluetooth Shield, so I really can't help you with setting up the Arduino part, but I know some people did used SetRawPackets() in that manner and it worked for them.
  30. ilu29

    ilu29

    New Member

    Joined:
    Jan 21, 2013
    Messages:
    4
    i dont know how to manage ports in arduino whith a bluetooth module, do you know how can i do this?
  31. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    No, sorry, I can't help you with that, as I don't have an Arduino with a Bluetooth module.
  32. ilu29

    ilu29

    New Member

    Joined:
    Jan 21, 2013
    Messages:
    4
    i am having a problem when i try to deploy the demo on the android. when i try to select a server the screen freezes completely
  33. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Could you please PM me the logcat dump?
  34. kennetv

    kennetv

    New Member

    Joined:
    Jun 10, 2013
    Messages:
    4
    I'm new to networking in Unity and I'm trying to make a simple RPC call using your plugin, but getting this error...

    NullReferenceException: Object reference not set to an instance of an object
    at Game.onBluetoothConnectedToServer (.BluetoothDevice device) [0x00000] in <filename unknown>:0
    at BluetoothMultiplayerAndroidManager.onBluetoothConnectedToServer (System.String deviceInfoString)

    On this line
    networkView.RPC("AddClient", RPCMode.All);

    Trying to call this
    [RPC]
    void AddClient() {
    Debug.Log("Success!");
    }

    Any ideas?
  35. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    It seems like the error isn't connected to making a RPC, as the exception is triggered somewhere in your event handler at Game.onBluetoothConnectedToServer. Does the error still occurs when you comment out that handler?
  36. almasaeid

    almasaeid

    New Member

    Joined:
    Oct 28, 2013
    Messages:
    1
    ZimM, could you please post a code sample on how to use this asset. I purchased the asset a few days back and found no code samples. Thanks

    --
    Hisham
  37. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Hi,

    the demo scenes are actually included and are located in Assets/BluetoothMultiplayerAndroid/Demo folder.
    Last edited: Oct 28, 2013
  38. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Hi everyone,

    forgot to tell that 1.23 update is available on Asset Store for some time. It's mostly just a fix to an issue when client wasn’t disconnecting from server, which may have resulted in some weird bugs. So it's better to update now until you face the issue :)

    Best wishes,
    - Serhii
  39. MajdeObeidat

    MajdeObeidat

    New Member

    Joined:
    Dec 6, 2013
    Messages:
    3
    hi ZimM ,

    i want to ask you that i design a simple tic tac toe game in java using unity3d and i have your plugins and its really wonderfull i want to ask you can i make my tic tac toe game mutipalyer over bluetooth easily using your plugins??
  40. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Hi,
    yes, that would be as easy as with Unity networking. For a tic-tac-toe game, it'd probably be just a few RPCs.
  41. MajdeObeidat

    MajdeObeidat

    New Member

    Joined:
    Dec 6, 2013
    Messages:
    3
    thank you very much,,,
    i am a beginner , now first i build the whole game that is a single player?
    and my game in java and your code in c#
    after build my game then by few RPCs i make my game mutiplayer?
    although my game in java?
    and am so sorry becuse i ask you alot of questions
  42. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    First, Unity doesn't supports Java as a script language, it only supports JavaScript (a subset of it, actually), C#, and Boo. Probably you meant JavaScript? If so, the plugin works with JS without any problems.
    Second, it is up to you if you wan't develop a singleplayer mode first, but usually, if you plan multiplayer game - you should be doing the networking part early in the development, as later it becomes a hell to re-create the game logic with multiplayer in mind.
    Basically, this is a very generic question about Unity and multiplayer, so if you're having some problems implementing, it'd really be easier for you to look for a good tutorial (and there are already dozens of them) on the subject instead of me explaining it on this thread :) Feel free to PM, though.
    From my side, I can assure that if your game works with Unity networking, then my plugin would be an almost drop-in solution to add Bluetooth multiplayer on Android devices.

    Best wishes,
    -Serhii
  43. MajdeObeidat

    MajdeObeidat

    New Member

    Joined:
    Dec 6, 2013
    Messages:
    3
    hahahaha.....thaaaaaaaaaaaank you Serhii am a beginner so am sorry...ok i will do the networking part early after i read some tutorials about networking in unity and if you have time please give me a link for a best tutorial and am sorry about my bad language becuse english its not my language :p

    design a tic tac toe game on bluetooth using unity its a project in my university :(

    finally ...thank you very much...really thank you

    Majde,
  44. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
  45. gillou35

    gillou35

    New Member

    Joined:
    Dec 11, 2013
    Messages:
    1
    Hi ZimM,

    I tried the second solution, to connect directly with my arduino but nothing happen !
    Even when I send or push any button from my arduino :/

    Here is my solution :

    1) Select any random port number.
    Port = 2501; // random port number

    2) Create a UDP socket.
    I suppose tu use this kind of socket :
    IPEndPoint remoteEndPoint;
    UdpClient sender;

    remoteEndPoint = new IPEndPoint(IPAddress.Parse(LocalIp), Port);
    sender = new UdpClient();

    3) Call BluetoothMultiplayerAndroid.Init().
    OK

    4) Call BluetoothMultiplayerAndroid.Connect(arduinoAddress , port).
    OK

    5) Wait for connection to happen.
    how can I detect that the connection is established ? is there a function callback that I should use ?

    7) To send data to Arduino, send data via UDP to the port you've chosen.
    byte[] dataOn = Encoding.UTF8.GetBytes("09ff");
    sender.Send(dataOn, dataOn.Length, remoteEndPoint);

    8) Data from Arduino will be received on your UDP port.
    Thread receiveThread;
    UdpClient client;

    receiveThread = new Thread(new ThreadStart(ReceiveData));
    receiveThread.IsBackground = true;
    receiveThread.Start();
    // ...
    private void ReceiveData()
    {
    client = new UdpClient(Port);
    while (true)
    {
    try
    {
    IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
    byte[] data = client.Receive(ref anyIP);

    string text = Encoding.UTF8.GetString(data);

    Log(">> " + text);

    }
    catch (Exception err)
    {
    print(err.ToString());
    }
    }
    }
  46. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    2) You don't have to send data from that IPEndPoint, you have to send data from it. This also means you have to bind the UdpClient to the chosen port, otherwise it will be a random port, and that won't work. Try something like this
    Code (csharp):
    1.  
    2.  IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(LocalIp),Port);
    3.  sender = new UdpClient(localEndPoint)
    4.  
    3) You also have to add this before trying to connect
    Code (csharp):
    1. BluetoothMultiplayerAndroid.SetRawPackets(true);
    5)
    Yes. See the docs and examples, there are plenty of events.
    ...
    Other than that, it seems OK, but I have to mention that there is currently no way to get the destination port address aforehead. So first you have to send some data from Arduino end, remember the port from that data came, and then send data to that port.
    Hope that helps.

    -Serhii
    Last edited: Dec 12, 2013
  47. Neeko

    Neeko

    Member

    Joined:
    Mar 9, 2012
    Messages:
    16
    Could you post a code example of the plugin, or explain how you integrate it? My game is currently not setup for any type of network play, and I'm curious as to how much work will be needed to get at least Bluetooth multiplayer working via this plug-in. Thanks!
  48. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    The integration is actually a few lines of code for the simplest case, and it isn't much harder if you want to integrate it more nicely with the UI. See the code example that is included with the plugin: http://pastebin.com/4L32Xn9f
    Last edited: Dec 28, 2013
  49. ina

    ina

    Member

    Joined:
    Nov 15, 2010
    Messages:
    329
    Can you provide an example for how this plugin would work with UDP sockets?

  50. ZimM

    ZimM

    Member

    Joined:
    Dec 24, 2012
    Messages:
    265
    Hi, sorry for the long reply.
    Working with UDP sockets is not something I really support, and this function is only left until better support for raw communications is implemented... But it is something like this:
    Sending data
    Code (csharp):
    1.     void SendRawData(byte[] data, int port) {
    2.         using (UdpClient udpClient = new UdpClient()) {
    3.             udpClient.Send(data, data.Length, "127.0.0.1", port);
    4.         }
    5.     }
    Receiving data
    Code (csharp):
    1.          void StartListen(int port) {
    2.              UdpClient udpClient = new UdpClient(port);
    3.  
    4.              udpClient.BeginReceive(OnReceiveUdpClient, udpClient);
    5.          }
    6.          void OnReceiveUdpClient(IAsyncResult asyncResult) {
    7.             UdpClient udpClient = (UdpClient) asyncResult.AsyncState;
    8.             if (udpClient == null)
    9.                 return;
    10.  
    11.             try {
    12.                 IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    13.                 byte[] receivedBuffer = udpClient.EndReceive(asyncResult, ref remoteIpEndPoint);
    14.  
    15.                 string text = Encoding.ASCII.GetString(receivedBuffer);
    16.                 Debug.Log("received: " + receivedBuffer.Length);
    17.                
    18.                 udpClient.BeginReceive(OnReceiveUdpClient, udpClient);
    19.             } catch (SocketException ex) {
    20.                 Debug.LogError(string.Format("<color=red>Error at UDP connection: {0}</color>", ex.Message));
    21.             } catch (ObjectDisposedException) {
    22.                 // The listener was Stop()'d, disposing the underlying socket and
    23.                 // triggering the completion of the callback. We're already exiting,
    24.                 // so just ignore this.
    25.             } catch (Exception ex) {
    26.                 // Some other error occured. this should not happen
    27.                 Debug.LogError(ex);
    28.             }
    29.          }
    It basically should works, but feels very hacky to me... I'll be doing a massive update to actually allow raw Bluetooth interaction (not limited to just Unity Bluetooth), but there are no ETA's yet.