Jump to content

  • Log In with Google      Sign In   
  • Create Account


android bluetooth or wifi connection multiplayer

  • You cannot reply to this topic
7 replies to this topic

#1 moeen k   Members   -  Reputation: 401

Like
1Likes
Like

Posted 27 July 2014 - 08:37 PM

hi. i made a simple game for android by eclipse but i want to make it a multiplayer. i dont want internet connection. just sending and recieving data through bluetooth or wifi. first i have to ask which one is better for this? does it work like server client system? all i search for it is just about pairing files for bluetooth and wifi. how can i do it for sending data like number or string?

thank you for helping



Sponsor:

#2 frob   Moderators   -  Reputation: 20302

Like
3Likes
Like

Posted 28 July 2014 - 03:08 AM

You wrote that you don't want internet but you do want wifi, which says you really don't understand what you are looking for.


Bluetooth allows your device to talk to other nearby bluetooth devices within a range of about 10 meters (30 feet).

Wifi, or wireless Ethernet, is basically an Internet connection.

In both cases you need to build a way to discover another player, connect with them, and then read and send data. The processes are fairly similar but the libraries are different.


Android's bluetooth libraries can be set up for long term connections and for an ad-hoc bluetooth pairing. Basically you tell two devices that they are looking for each other, they turn on their radios, and then authenticate. Once they are talking to each other you send and read data. Documentation for that is at http://developer.android.com/guide/topics/connectivity/bluetooth.html

If you are looking for wifi, you need the user to connect to a wifi network on their own in the network settings. Alternatively if they are on a data-enabled cell phone you may not need a wifi connection, the data connection connects you to the Internet. Yet another option is an ad hoc wifi network, where devices act as though they are on their own private LAN. No matter which option you go for, you can then use Java's networking classes (java.net.*) to do the work. There are hundreds of tutorials on the web for that, including several linked to on this site's Networking forum FAQ.

Edited by frob, 28 July 2014 - 03:13 AM.

Check out my personal indie blog at bryanwagstaff.com.

#3 Navyman   Crossbones+   -  Reputation: 4045

Like
1Likes
Like

Posted 28 July 2014 - 03:59 AM

Frob you did a great job of explaining how to setup a bluetooth connection.


Developer with a bit of Kickstarter and business experience.

34410.png

#4 JulianLoehr   Members   -  Reputation: 171

Like
2Likes
Like

Posted 28 July 2014 - 05:24 AM

@Frob: *Insert Morpheus Meme* What if i told you ... that wifi is not just internet */Insert Morpheus Meme*

I think he meant with Wifi just Wireless LAN, which as the name states is just an LAN, where you can establish socket connections.

 

I didn't made anything with Android yet, but used both methods on Windows Phone, so i'm referencing to how it is made on Windows Phone. But i think it will be similiar on Android. However i will list you the features and cons/pros of both methods so you can decide on your own, wich fits best.

 

  • Bluetooth: As said you have a range up to 10m, so your players have to sit next to each other. Both smartphones will publish something like a service to find each other and then establish a Bidirectional Stream Socket like connection. This means you can send data in both directions asynchron. The connection will be peer to peer, without anything in between.
  • Wifi/WLAN: For WLAN both devices have to be in the same network. So an access point is required, or one smartphone has to provide a wireless network. However this has to be done before they get into you game. Then it acts like a normal LAN, so you have to use sockets, connect via IPs and so on (There might be some API that provides automatic connection like this one http://developer.android.com/training/connect-devices-wirelessly/nsd.html). But the difference to Bluetooth is, you will have something like a server and a client. The server has to listen for incomming connection and the client has to connect to you server. Your range depends on the network, so both player could sit in different rooms. 

I think in the end either way you will get just an data socket, where you can read write you data form and to. I didn't mentioned the data rate, but for a game that shouldn't matter at all. Only if you have to transfer very big amout of data, like images or sounds.

 

Best way for a customer would be to have both ways. So either they can choose what they want to use or you make automatic system, which decides what to use. Something like, first check if both user are on the same Wireless network, if so, use it. Then try to establish a Bluetooth connection, if it fails prompt the user to create a local wireless network.

 

And for wifi, there is a new feature called Wifi Direct, which lets you connect two devices via WLAN but without a network (like Bluetooth but uses WLAN adapter). But that is not supported on all devices, mostly only on newer ones.

 

For the "how do i programm it" you can look into the android documentation. Start at Bluetooth or Wifi and move along the links through connecting until sockets, where you can finally send data. 



#5 moeen k   Members   -  Reputation: 401

Like
1Likes
Like

Posted 28 July 2014 - 01:07 PM

thank you my friends im working on links and info you gave. thank you



#6 Bestben   Members   -  Reputation: 111

Like
2Likes
Like

Posted 28 July 2014 - 03:41 PM

Since android 4.0 Google introduced the wifip2p API.
http://developer.android.com/guide/topics/connectivity/wifip2p.html

Since android 4.1, it even allows you to use wifi service discovery :
http://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html

Unfortunatly not all devices support it. (Nexus4&5 support it, Samsung galaxy > s2 support it also, don't know about other phones)

#7 moeen k   Members   -  Reputation: 401

Like
0Likes
Like

Posted 14 August 2014 - 10:18 AM

thank you all. i made most part of my multiplayer game and now im on debugging. maybe its compeletely diffrent question but i think its better to ask it here. i know many basic features of network games like using peer to peer system or client server and waht is the diffrence of tcp and udp but i want to start to learning it practically. i want to know what is the best library to start learning it? i know for windows and xbox best choice is directx and networking libraries is some part of it. and windows has it own networking socket programing libraries. but what is best cross platform library that i an use in linux platforms? thank you for helping



#8 frob   Moderators   -  Reputation: 20302

Like
1Likes
Like

Posted 14 August 2014 - 10:48 AM

There are several libraries available, but I'm not sure what is currently the most recommended.

 

You would be best by asking that question in a new thread in the Networking forum


Check out my personal indie blog at bryanwagstaff.com.





PARTNERS