views:

585

answers:

3

Hello, I'm doing a college project on Bluetooth for Android, and I'm trying to understand how to manage communication between multiple connected devices. Eventually I'm going to develop a multiplayer Bluetooth Game.

Currently I've adapted Android's sample app BluetoothChat to connect my three Nexxus One phones. 1 connects to 2 who connects to 3 1 sends its messages successfully to 2. 3 sends its messages successfully to 2 as well. 2 can send its messages successfully to 1 and 3, as it shares a ConnectedThread with both. But I can't figure out how to handle getting communication from 1 to 3.

Does anyone have any examples of communication between multiple devices or has done this themselves? Thanks

+1  A: 

One way is to annotate your messages with the sender and receiver, so that when 2 gets the message, it knows to deliver it on to 3. When 3 gets a message, it checks the receiver attribute to know it is from device 1. This extra layer allows you to send and receive messages through other devices and still be able to know who it is from.

First, though, you'll need to figure out how to make every device know about every other device on the network. If you're just connecting in a line, like 1-2-3-4, then every time a device enters the network, you could send an updated list through the network, but what happens if 2 drops out? Do you just quit the game? Wait for it to be re-paired? In this case, it may be better to look at a peer-to-peer network, or the typical client server architecture where you let one device be the host, especially if this is intended to later be a multiplayer game.
Hope that helps!

Ryan Hayes
What a marvellous answer!
HXCaine
A: 

Isn't it possible to connect 1 to 3 directly (via bluetooth)?

s7orm
A: 

It is possible to have multiple Bluetooth sockets in use simultaneously so you may also consider configuring to act as a server, like an IRC or XMPP server, which brokers all communication from any client to another.

If you're going for fault-tollerance (for example server socket goes down) then upon connecting to the server, it might provide a list of recently-seen Bluetooth devices which you could fall-back on in the event the server goes down.

Brad Hein