DevBlog: A Bluetooth Novice’s Tale

Once upon a time not so long ago there was a not-so-young programmer. He thought, “let’s make a game where people connect their smartphones via Bluetooth to trade with each other”. And so he went on a perilous adventure with many loops and turns where in the end, the simplest solution won out.

Have you ever tried connecting two Android devices with Bluetooth? It can be done, obviously, but it’s not as easy as you may think. First off, Unity does not offer any support for Bluetooth natively, of course. So I started by browsing the asset store. There are a couple of packages for different application scenarios such as coupling all kinds of devices with different kinds of Bluetooth. For of course, there is not just one Bluetooth, but there is classic Bluetooth and Bluetooth Low Energy (BLE).

The Bluetooth Quest

I did not know that Bluetooth LE existed before, so after a little bit of reading I decided to give it a try. BLE has the advantage that devices broadcast their data to anyone in the vicinity and coupling devices is not necessary. On the other hand, bandwidth is very low and security may be an issue. Implementing a first connection using an asset store package was done fairly quickly.

But then I had to realise that bandwidth was just too low for my purposes. A BLE advertising package only has 31 bytes of payload, including the device name and some other data. What was left was not enough for even a single Guid! Establishing a BLE client-server connection and sending several packages worked, kind of, but exchanging any significant amount of data was out of the question. Well, I know there is more to BLE, such as secondary advertising channels, but I consider these the highest levels of the Bluetooth tower of wisdom and never ventured there.

So I decided that BLE would only be used for determining what other players were around, and the real data would be transmitted via an extra Bluetooth classic connection. But here, the real perils were already waiting for me.

Classic Perils

As usual, I tried to save some money and not buy another package for Unity, but to implement the Android Bluetooth adapter myself. A foolish notion, as it turned out.

There is an Android example implementation of a simple Bluetooth chat application you can download from GitHub. Additionally, there is a Unity Bluetooth plugin available on GitHub, too. I used these as a basis for my own interface, tweaking and adapting them for my own needs. It took me a few weeks in August, but I got it running. To a certain point, at least.

The workflow now was as follows: connect two devices via Bluetooth LE by setting up a master-slave connection. Pass information about the other player, such as his ID and name, in several messages. A connect button is shown, which initiates a Bluetooth Classic connection when pressed. This is used to exchange the bulk of data necessary for playing.

This worked, most of the time, for my own devices, but not always. Sometimes, I had to restart my app, sometimes the bluetooth interface. But things got worse the first time I asked a neighbour to try my game. It was impossible to get a connection with his device. I found numerous tales on what could be the reason for devices not connecting, such as that you should only have one paired device at a time. It is, of course, no feasible solution to tell players to unpair all their devices – and that was not the problem anyway. So in the end, I yielded.

The Tower of Bluetooth Wisdom

The Bluetooth Core Specification is a tome of 3000 pages, written in a language only the wise ones can understand. It can do incredible things, but it is not for the untrained novice. I entered this tower of wisdom and ventured up a few levels, but in the end, I decided to just stay in the entrance hall and gawk at its wonders. The lesson is that many times, the simplest solutions are the best.

So what did I do? Now, I only send one advertising package via Bluetooth LE over which devices make themselves known. This package contains an identifier that is sent to a internet server, which identifies nearby players. The connection is then established via this server and data is exchanged over the internet. It’s as simple as that.

By the way, I ended up using a Bluetooth LE Unity asset by Shatalmic, llc. I had a couple of questions which were always answered very quickly by its maintainer. So if you want to start with BLE despite my story, give this library a try.

What are your experiences with Bluetooth, especially in the context of gaming and in Unity? Is there a game out there which actually makes use of Bluetooth? Tell me in the comments below! Or subscribe to my Twitter account or RSS feed for more news on Network Traders.

0 0 votes
Article Rating
Abonnieren
Benachrichtige mich bei
0 Comments
Inline Feedbacks
View all comments
0
Was denkst Du? Bitte hinterlasse einen Kommentar!x
()
x