android bluetooth or wifi connection multiplayer

Started by
6 comments, last by frob 9 years, 8 months ago

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

Advertisement
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.

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

Developer with a bit of Kickstarter and business experience.

YouTube Channel: Hostile Viking Studio
Twitter: @Precursors_Dawn

@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.

Blog: http://www.julianloehr.de

Twitter: https://twitter.com/jloehr_gamedev

HID Wiimote - Windows Device Driver for Wii Remote & Wii U Pro Controller: http://julianloehr.de/educational-work/hid-wiimote/

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

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)

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

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.

This topic is closed to new replies.

Advertisement