Friday, May 27, 2011

What We've Got Here is...Failure to Communicate: Bluetooth, Android, and iPhone

I'm disheartened and exhausted now, after trying to break ground on a new game that would involve peer-to-peer networking among a number of Android and iPhone devices.

What I want is to have an app that would install on tablets (e.g. Xoom, iPad) and act as a server. Players would connect to the server with phone-sized devices and exchange information with the server.

My first inclination was to use bluetooth. Well, just getting two test devices connected to a tablet is something I haven't been able to accomplish. The BluetoothChat example in the Android SDK enables connection and communication between two devices, with a single app that can function as either the server or client. I've tried to modify this app into two distinct apps: a dedicated server and client, and that works fine. But attempting to connect a second device has proven undoable so far.

I made a couple of modifications to the example code so that the AcceptThread continues to listen, even after the first connection is made (the example stops listening after the one connection is made, because it only needs to establish connections between two devices). I created an ArrayList to handle the multiple connections. Each time a new connection is attempted from a remote device, a new ConnectedThread is created and added to the list.

The first client connects just fine.

When the second client attempts to connect, the connection fails. The log output is:

D/BluetoothEventLoop( 140): Device property changed: property: Connected value: true
I/BluetoothService( 140): ACL connected, mAclLinkCount = 2
D/BluetoothEventLoop( 140): Device property changed: property: Connected value: false
I/BluetoothService( 140): ACL disconnected, mAclLinkCount = 1

So it looks like the second connection attempt is successful, but then it is immediately disconnected. This looks like it's happening at a low level, outside anything the code is handling. This happens with a number of different types of test devices, so I don't think it's due to a particular firmware or hardware issue.

At the Lafayette Mobile Developer's meetup last night, we talked about bluetooth communication between Android and iPhone devices. I hadn't even looked into the iPhone side, but I should have known that they basically don't have a bluetooth API for iOS. Or rather, they don't have one that allows Apple devices to talk to non-Apple devices. Bluetooth on iOS can be done through GameKit. This thread on Stack Overflow suggests that it would be possible to spoof the GameKit functionality on Android to allow an Android device to talk to an iOS device, but I can see no indication that anyone has done such a thing.

I noticed in the same thread that the Bump API is mentioned, and I always assumed that it used bluetooth or some other kind of local connectivity. uses a device's data connection, sends information to the cloud and syncs up devices based on their GPS position, which is kind of cool, but it also means that devices without a network connection (e.g. wifi only devices) can't use it. This is a deal breaker, because a lot of tablets are going to be wifi only.

So what about wifi? Well, on that front it sounds like Android does not support ad-hoc peer-to-peer networks, at least according to this post.

I find this ridiculous and depressing that these devices are all equipped with multiple ways to communicate locally, but in both cases (Android and iOS), the SDK functionality doesn't allow a way for them to talk to one another enough to write a simple app that pass something like a string between devices. NFC technology might enable local communication effectively, but it doesn't sound like NFC capabilities are coming to iPhone anytime soon, and they may be locked down if/when they do.

At this point I'm not even sure that a wifi-only tablet and multiple phone client app is going to work. It will likely work fine within a given platform, but not cross-platform, and that is extremely limiting.


1 comment:

  1. I've been wondering the exact same thing. It looks like these guys did it at some point in the past:, but if you go to SGN's website, there's no mention at all of anything Android-related. Please let me know if you find out anything else...