Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Android Bluetooth Multiplayer

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

  1. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Just trying to be able to read data from (maybe?) SetRawPackets() - (maybe not via UDP) - is it possible to just receive data other than via Unity networking
     
  2. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Yes, it is possible with that code I've posted. And of course, SetRawPackets() must be set to true.
     
  3. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Also, for UDP or just raw data packets, what are the methods to connect to device using your plugin?
     
  4. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    There are no special methods, just use BluetoothMultiplayerAndroid.Connect.
     
  5. MrTiger

    MrTiger

    Joined:
    Jan 9, 2014
    Posts:
    29
    Hey there
    Thx a lot for the plugin :) ...
    can we connect Android to PC bluetooth with this plugin ?
     
  6. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi, I think it is possible, but this plugin only provides the Android side of communication.
     
  7. MrTiger

    MrTiger

    Joined:
    Jan 9, 2014
    Posts:
    29
    Thx ZimM for the reply .. So is that mean if i want to test. i must have two android device and i can't take one built for PC and connect with Android to Test the game ? :( ... (sorry if you think this is a silly questions but depending only on your answer i have to decide whether i have to buy another android device or not )
     
  8. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Unfortunately, there are no other solutions right now except using two devices. It could've been possible if there were any Android emulators that could both run Unity games and support Bluetooth, but there is no such thing at the. Genymotion (alternative Android emulator) guys told me that they do plan to support Bluetooth, but it was a long time ago and Bluetooth still isn't supported... So yeah, it may become possible someday, but at the moment you'll need to have multiple devices to test.
    But it's not really a big problem, actually, as you can debug everything on PC anyway - just use Unity networking by itself, that's what it is for :)
     
  9. MrTiger

    MrTiger

    Joined:
    Jan 9, 2014
    Posts:
    29
    Thx ZimM :) ..
     
  10. SmartGame

    SmartGame

    Joined:
    Jan 22, 2014
    Posts:
    4
    how can send int or float to other device when click on guiButton?
     
  11. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
  12. SmartGame

    SmartGame

    Joined:
    Jan 22, 2014
    Posts:
    4
    Last edited: Jan 25, 2014
  13. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    I see, but the link to the RPC docs has the examples too. Your question is about Unity networking, not about this plugin. Anyway, to receive:
    Code (csharp):
    1. [RPC]
    2. void SomeCommand(int data) {
    3.     Debug.Log(data);
    4. }
    To send:
    Code (csharp):
    1. networkView.RPC("SomeCommand", RPCMode.All, 5);
     
  14. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    For everyone who prefers JS over C#:

    To make this plugin work with JS, move the directories:
    1) "Assets\BluetoothMultiplayerAndroid\Editor\" to "Assets\Editor\"
    2) "Assets\BluetoothMultiplayerAndroid\" to "Assets\Standard Assets\BluetoothMultiplayerAndroid\" (create "Assets\Standard Assets\" if it doesn't exists)

    Also, I've converted the main demo script to JS, as some people had some troubles with that. Click the link below to download:
    View attachment $BluetoothMultiplayerAndroidDemoGui.js
     
  15. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
  16. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi,

    I looks like a standard Bluetooth 2.0 device, so I see no reason why it couldn't probably work. A number of users already did communications with different hardware Bluetooth devices and it worked fairly well.
    Note, thought, that raw data communication isn't really user friendly at the moment and requires Android Pro license as it needs access to UDP sockets. See pages 2-3 of this thread for discussion and code samples on this topic.
     
  17. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Ok, thanks, I will have a look/
     
  18. bboysil

    bboysil

    Joined:
    Jun 1, 2013
    Posts:
    11
    Hi, when I compile my game for the PC platform, it fails with "Assets/BluetoothMultiplayerAndroid/BluetoothMultiplayerAndroid.cs(9,29): error CS0246: The type or namespace name `AndroidJavaObject' could not be found. Are you missing a using directive or an assembly reference?"

    Is this normal? Should I add code for excluding the bluetooth plugin code based on the platform I target? (i.e. exclude from PC but leave it for Android)
    Thanks

    EDIT: I don't use the bluetooth plugin for pc, but I want to target the game for PC as well.
     
    Last edited: Mar 15, 2014
  19. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    @bboysil

    Hi there, sorry for slow reply.
    Yes, just use conditional defines for non-Android code for now (This is going to be fixed in the nearest update).
     
  20. Anson168

    Anson168

    Joined:
    Mar 24, 2014
    Posts:
    2
    Can I send byte array to other device via you plugin?
     
  21. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi there,

    do you mean some arbitrary Bluetooth device like Arduino? If yes - please see pages 2-3 of this thread, I've given some notes, instructions and examples regarding this.
     
  22. Anson168

    Anson168

    Joined:
    Mar 24, 2014
    Posts:
    2
    I use two android phones. and I have read page 2-3, I have try to write program, but always fail.
    It is difficult for me.( I am beginner ).
    Can you add this function into your plugin?
     
  23. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
  24. BlueFireDev

    BlueFireDev

    Joined:
    Aug 3, 2013
    Posts:
    2
    Curious if Good Ol' sockets would work here in place of the Android Pro license?
     
  25. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Yeap, they totally would work :)
     
  26. BlueFireDev

    BlueFireDev

    Joined:
    Aug 3, 2013
    Posts:
    2
    That was a softball, I know ;)
    I was just making my rounds through the forums to see what Bluetooth options there were and came across your products. I'm looking to connect a microcontroller based system (not Arduino but for the sake of conversation, similar) to Unity3D on windows 7/8 and Android and it seems like your Bluetooth multiplayer + Good Ol' sockets might get me there quickly. I also want to connect to WP8 but I think I can get there using our own framework.
    Might just move this up on my schedule and try it next week.
     
  27. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    Hi ZimM,

    I purchased your plugin and wanted to implement it... However - I have been asked to implement bluetooth in a project which is using the Google Play Game Services Plugin from prime31. the prime31 plugin needs to overwrite the main Unity Activity in order to work and so does your plugin...

    Prime31 offers a way to use several main activities in parallel (read the first sentence here).

    As far as I can see, the java side of the plugins has to be refactored...
    Could you provide a version of your plugin with support for the Activity Sharing System?
     
  28. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Thanks for bringing this up. "Activity Sharing System" looks okay, but to be honest, I'm not really a fan of including a closed-source third-party library just to share Activity methods. In any case, I'll try to implement some solution to this problem for the next update.
    Meanwhile, this still can be solved by extending the prime31 plugin Activity. I am attaching the source code for Android Bluetooth Multiplayer Activity's. You'll have to modify them to extend from prime31 plugin's Activity instead of UnityPlayerActivity, change the package name and compile them into a separate .jar, then modify AndroidManifest.xml to use modified Activity's. You can refer to the "Extending the UnityPlayerActivity Java Code" section of "Building Plugins for Android" Unity documentation
     

    Attached Files:

  29. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    Hi ZimM,

    Thanks for your fast reply and the source code :)

    to build the sourcecode i followed this tutorial (see the three commandds at end of step 2). I renamed the package to "my.bundle.identifier" (com.mycompany.myproject) in all three classes. I also changed the imports accordingly and the classnames[] in BluetoothUnityPlayerProxyActivity.
    I created folders as described in the tutorial in my unity project and executed the commands with BluetoothUnityPlayerNativeActivity as parameter.

    However, I probably did something wrong, because the game crashes on start and ADB tells me:

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{<...>.BluetoothUnityPlayerNativeActivity}: java.lang.ClassNotFoundException: <...>.BluetoothUnityPlayerNativeActivity


    Any Ideas?


    EDIT:
    I think I now got my mistake. I only compiled one of the classes. not all.
    I created a .bat file to compile and pack the files. it looks like this:

    Code (csharp):
    1. javac BluetoothUnityPlayerNativeActivity.java -classpath "C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar" -bootclasspath D:\portable_apps\AndroidSDK\sdk\platforms\android-10\android.jar -d .
    2.  
    3. javac BluetoothUnityPlayerActivity.java -classpath "C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar" -bootclasspath D:\portable_apps\AndroidSDK\sdk\platforms\android-10\android.jar -d .
    4.  
    5. javac BluetoothUnityPlayerProxyActivity.java -classpath "C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar" -bootclasspath D:\portable_apps\AndroidSDK\sdk\platforms\android-10\android.jar -d .
    6.  
    7.  
    8. javap -s com.<mycompany>.<mygame>.BluetoothUnityPlayerActivity com.<mycompany>.<mygame>.BluetoothUnityPlayerProxyActivity com.<mycompany>.<mygame>.BluetoothUnityPlayerNativeActivity
    9.  
    10. jar cvfM ../BluetoothMultiplayer.jar com/
    11.  
    12. pause
    (note: I replaced the bundle-identifier name)

    but now when i try to compile i got the error message that the "BluetoothMediator" cannot be found.
    Probably I need the sourcecode file of that class as well to compile...?
     
    Last edited: Jul 15, 2014
  30. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    I'm not familiar with compiling Java from the command-line (I use Eclipse), but it seems like you are not referencing the BluetoothMultiplayerAndroid.jar file, which is where BluetoothMediator class resides. You don't need the source code, just reference that jar.

    Edit: and btw, if you are targeting Unity 4.3+, the only class you actually need the compile is BluetoothUnityPlayerNativeActivity. Unity 4.3+ doesn't uses the other two.
     
  31. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    Hi ZimM,
    Good news: It worked :) (i compiled with the javac command and added the jar files to the classpath parameter).

    However, I can't get it running with my project.
    What I do:

    One Player selects the amount of players for a match and starts the server. Other clients connect and as soon as all players have connected I create an object via "Network.Instatiate" on the server. Immediately after that I send a byte-array with the created Object (in that mono-behaviour is an RPC-ReceiveMessage-method as well).

    The Object is created on the clients but the RPC-Method is never called there. Do I have to wait with sending something until the Object is instantiated on the clients? (In my many tests one time the message has been delivered successfully... in all other test the message is never arriving).

    Also strange: I can create a server only on one of my two test devices. If I try the other one, the client logs an error:
    Do I have to change any settings on that device?
     
  32. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi there,
    sorry for slow reply.
    It's hard to say what exactly is causing your problem, but that kind of stuff shouldn't ever happen. Probably you've did something wrong with the Java part, or maybe you haven't modified the AndroidManifest.xml to point to your new Activity's. To make things clear, please test the plugin as-is in your project without all the Activity manipulations. Does it works then?
    I've actually tried to reproduce this. For some reason, I couldn't get the Activity working at all if it was compiled with command-line javac, though it magically worked when compiled with Eclipse...
    But there are good news! I've made an optional integration with Prime31's Activity Sharing System. I'll tidy up some stuff and push it to the Asset Store tomorrow. Just wait a little bit until I finish the update, and hopefully your problem will be solved :)
     
  33. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi everyone,

    I've submitted the 1.3 update to the Asset Store yesterday, and it should be live in a few days. The update is based mostly on users requests, so hopefully you'll like it :)
    Changelog:
    • New IsDiscoverable() method that returns the device can be discovered by others devices at the moment of calling
    • Added a parameter to ShowDeviceList() for showing only data-capable devices
    • Added detection of the Bluetooth device class (useful if you only want to show some types of devices to the user in the custom device discovery dialog)
    • Improved integration with other Android plugins (Activity sources are included, pre-compiled .jar files for easy integration with Prime31's plugins and Vuforia are included too)
    • Improved demo scenes
    • Code clean-up
    And of course, until the update is live, you can always PM me with your invoice number and I'll be happy to send you the updated package ;)
     
  34. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    Hello ZimM, I am developing a game and nowadays I'm developing multiplayer. Reading here and there, I see that the plugin generates conflicts with Vuforia. But in your last post you say that the last version is Vuforia-friendly :) So, before to purchase the asset, I would like to know which steps should I take for an easy integration, in order to understand if it will be easy to me or not.
    Thank you
     
  35. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi there,
    it does not really "conflicts", but you had to do some work... The same actually applies for every plugin that extends the main Activity, but I've tried to make integration more user-friendly. I have already updated the documentation for the asset, and there is a section related to that. Check it out here:
    Nothing really complicated, I hope :)
     
    Last edited: Jul 28, 2014
  36. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    That's awesome, thank you :) I really think I will purchase it right now, just one question: on the asset store the version currently on sale is 1.23 ... you will send me the 1.3 via mail after purchase?
     
  37. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Sure, just PM me with your invoice number and I'll send the 1.3 update to you.
     
  38. Vincenzello

    Vincenzello

    Joined:
    Jul 28, 2014
    Posts:
    1
    Perfect! Just purchased the plugin :) Waiting for the v1.3 :D
     
  39. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Aaand the 1.3 update is now live on the Asset Store. Go check it out ;)
     
  40. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    I just finished following your guide and adding the extended activity you provided. The game builds without any error :) I will now start to test on device... thank you very much ZimM, your Asset not only will save me a lot of time, but it also opens new interesting doors to my app!!:D
     
  41. DnninDalke

    DnninDalke

    Joined:
    Oct 17, 2012
    Posts:
    12
    Hey guys, I tested the apk from asset store link on my nexus5 and with a Galaxy s4 and, the apk works well on Galaxy, but not on Nexus, it always receive this error log:

    E/Unity(24817): The connection request to 127.0.0.1:28000 failed. Are you sure the server can be connected to?
     
  42. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    That's strange, I've never heard about this. And I've tested that apk on Nexus too... Could you please attach the logcat output around that error?
     
  43. DnninDalke

    DnninDalke

    Joined:
    Oct 17, 2012
    Posts:
    12
    Here is the log:
    I created the server on GalaxyS4, but the problem also happens creating the server on Nexus.

    From Nexus5:
    08-09 17:20:55.807: D/Unity(824): onDetachedFromWindow
    08-09 17:20:59.877: D/Unity(824): Creating OpenGL ES 1.x context (RGB16 565 16/0)
    08-09 17:21:00.104: I/Unity(824): Event - onBluetoothDevicePicked(): Galaxy S4 [C0:33:33:33:33:33], class: PhoneSmart, connectable: True
    08-09 17:21:00.104: I/Unity(824):
    08-09 17:21:00.104: I/Unity(824): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
    08-09 17:21:00.774: I/Unity(824): Event - onBluetoothConnectedToServer(): Galaxy S4 [C0:33:33:33:33:33], class: PhoneSmart, connectable: True
    08-09 17:21:00.774: I/Unity(824):
    08-09 17:21:00.774: I/Unity(824): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
    08-09 17:21:07.390: E/Unity(824): The connection request to 127.0.0.1:28000 failed. Are you sure the server can be connected to?
    08-09 17:21:07.390: E/Unity(824):
    08-09 17:21:07.390: E/Unity(824): (Filename: ./Runtime/Network/NetworkManager.cpp Line: 647)
    08-09 17:21:07.392: I/Unity(824): Can't connect to the networking server
    08-09 17:21:07.392: I/Unity(824):
    08-09 17:21:07.392: I/Unity(824): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
    08-09 17:21:07.458: I/Unity(824): Event - onBluetoothDisconnectedFromServer(): Galaxy S4 [C0:33:33:33:33:33], class: PhoneSmart, connectable: True
    08-09 17:21:07.458: I/Unity(824):
    08-09 17:21:07.458: I/Unity(824): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)




    From GalaxyS4:
    08-09 17:20:52.963: I/Unity(4979): Event - onBluetoothListeningStarted()
    08-09 17:20:52.963: I/Unity(4979):
    08-09 17:20:52.963: I/Unity(4979): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
    08-09 17:20:52.983: I/Unity(4979): OnServerInitialized
    08-09 17:20:52.983: I/Unity(4979):
    08-09 17:20:52.983: I/Unity(4979): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
    08-09 17:20:59.980: I/Unity(4979): Event - onBluetoothClientConnected(): Nexus 5 [2C:21:34:54:54:54], class: PhoneSmart, connectable: True
    08-09 17:20:59.980: I/Unity(4979):
    08-09 17:20:59.980: I/Unity(4979): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
     
  44. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    This is weird, the Bluetooth connection seems OK, but it can't connect to the network server. Hard to say what's going on here, but Galaxy S4's Bluetooth address [C0:33:33:33:33:33] looks really strange to me - it's usually pretty much random.
    Try this updated APK, probably it'll work for you:
    https://www.dropbox.com/s/yhircie4a5xjdb2/AndroidBluetoothMultiplayer_Demo.apk
    But again, issues like this are supposedly extremely rare, and it usually works on pretty much any devices.
     
  45. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi everyone,
    I want to announce that the plugin is going to be separated into two versions - Basic and Pro. Basic version is the same as Pro in terms of functionality, but the Pro version is going to include the full Java plugin source code (which wasn't available before). Current version (which is equivalent to Basic) will be replaced with Pro, so everyone who bought the plugin before this change will be upgraded to Pro for free.
    The price for Pro version is going to be $60, so go grab the current version for $35 if you want to get the free Pro upgrade ;)
     
  46. kannanisgod

    kannanisgod

    Joined:
    Feb 19, 2014
    Posts:
    2
    Hi,
    I m a rookie in networking. However I bought your plugin and made it to work in my game. Thanks.
    It was working fine and today when I open my game it shows the _initResult variable as false. I could not spot what s going wrong. What all the possibilities of this to go wrong?

    _initResult = BluetoothMultiplayerAndroid.Init("8ce255c0-200a-11e0-ac64-0800200c9a66");

    //returns false
     
  47. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi there,
    first, enable verbose logging and check your logcat output. There is a pretty high chance you'll find something there. Also try reimporting the plugin and see if it helps.
    Possibilities of why Init could fail are:
    1. Java plugin failed to load for some reason.
    2. The AndroidManifest.xml lacks BLUETOOTH or BLUETOOTH_ADMIN permissions.
    3. Getting reference to Bluetooth adapter fails (this should never happen).
    4. The UUID provided is in incorrect format (but it looks fine in your code).
     
  48. kannanisgod

    kannanisgod

    Joined:
    Feb 19, 2014
    Posts:
    2
    Thanks, I reimported the plugin and it worked. :)
     
  49. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Good news everyone!
    If you've bought the plugin before today, you are now upgraded to the Pro version of the asset and have access to the full Java source code :) You'll find everything you need inside the BluetoothMultiplayerAndroid_Source.zip archive after importing the package.
    For new buyers: Basic version is exactly the same except the absence of the plugin core Java source code. Of course, you still get the source code for Activities for easier integration with third-party plugins. So in case you think you won't need the plugin source code - grab the Basic version, after all, it's much cheaper than Pro now ;)
     
    Last edited: Sep 7, 2014
  50. Pajaroide

    Pajaroide

    Joined:
    Sep 19, 2012
    Posts:
    34
    Hello! Quick question:

    If I make a version of a bluetooth multiplayer game for iOS with Gamekit; could I make it compatible with an android version of the same game that uses this plugin ?