Bluetooth Checkers!!!

My bluetooth checkers game is ready to go!!!

If your MIDP 2 handset supports JSR 82 (Bluetooth API), you can download and install it (free) from my usual checkers wml page or my games html page!!!

I promise this is the last Checkers version (for a long time anyway 😉 ), and next I’ll move on to some graphics discussions. But I wanted to take one standard game and program it using a few different protocols to compare and contrast how each one works in a multiplayer game.

I think Bluetooth is a little more challenging and complicated to program than SMS. Part of the reason for that is that SMS uses the operators’ standard way of routing communications to a handset. You find the right destination by typing in the right phone number. And it’s the same phone number you’d use to route other comunications to that device. So you don’t have to reinvent the wheel to find, identify, and authenticate the device you’re looking for.

Bluetooth allows you to find all active, bluetooth-enabled devices within range and see what kind of services they offer. Each server offering a bluetooth service publishes a set of detailed service records describing the services available. So you have a fair amount of work on the server side creating useful descriptions and on the client side reading the descriptions and choosing the right service on the right device. Add to that the fact that you have the option of choosing a plain socket type connection or discrete packets, different authentication and/or encryption schemes, and even object-oriented communications using Java OBEX, you have practically unlimited potential to customize (and complicate 😉 ) your application according to your needs.

In my case, I decided I wanted to make my game’s communications as simple as possible. So I started with the assumption — since bluetooth is a protocol for local communications — that you and your friend are in the same room together and have verbally agreed to start up a game of checkers on your two handsets. My checkers game is fundamentally a peer-to-peer type game, but bluetooth is a client-server protocol, so I had my game start with a screen where the user can choose whether his handset will be acting as the client or as the server (the first player selects server and the second client).

Then there was the question of writing the code so that the two devices will find each other instead of finding other random bluetooth devices and services. Fortunately — with a simple services like this one — all you really need is a UUID. You can create your own UUID (in both Linux and Windows) using the uuidgen command. Then on the server side, all you need to do is construct your bluetooth URL using your UUID and open the connection and wait for clients to call you up. My game’s server URL looks like this:

btspp://localhost:2BBC2D287C8C11DBA1500040F45842EF;name=Checkers;authorize=false

The “btspp” indicates that I’d like to use the bluetooth streaming (socket-like) protocol, the “localhost” makes me a server; then I add on my UUID (what the “uuidgen -t” with the hyphens removed), then some attributes (the application name and the fact that I don’t want to require authorization). Opening this connection creates the corresponding service record, which you can get a handle to and add attributes to if you like. The WTK’s BluetoothDemo shows a good example of how to add additional descriptive attributes. The BluetoothDemo has a server that makes images available for bluetooth clients to download, and there you can see the programmer has added a custom attribute to give the name of the image being offered. As you can see in the ServiceRecord JavaDoc, there are a number of common standard attributes that you can use as well. The attributes aren’t too complicated to use — you just need to keep in mind that different attributes values can be of different types so the values are wrapped in DataElements that allow you to handle all of the different types of values in a consistent way.

Once your server is up and running, the next thing is to program the client to find it and connect to it. Basically, all you need to do is grab a handle to the local DiscoveryAgent and send it off in search of devices and services. It toodles around on its own thread looking for them, then it calls the assigned DiscoveryListener when it’s done.

Discovery is a two-step procedure: First you gather a list of devices, then you search the devices for the service you want. (Since I didn’t bother with any special service record attributes on the server side, when I started searching my device list for services I sent a null attrSet to the searchServices method.) Note that if you’re writing a program where the client frequently reconnects to the same set of known servers, you can optimize discovery by caching a list of known devices.

It might seem like you could simplify the discovery procedure by calling the method DiscoveryAgent.selectService() which finds all of the available connections that provide the desired service (without first getting all the devices and searching each one). Or that you could optimize by searching each device for services as you find it rather than first finding all devices and then searching them one by one. However, over on developer.sun.com I found this interesting little powerpoint presentation full of bluetooth programming best practices, and this little slideshow specifically says not to do either one of those things. Because they seem like good ideas, but really the lead to nothing but badness. And I figure that if anyone would know the people over at developer.sun.com would, so I decided to follow their advice.

Once the client side of the program and the server side find each other, the rest is a breeze! Whew! From then on it’s just standard Java socket programming. The server’s acceptAndOpen command stops blocking and returns a handle to the connection. Both the client and the server open the InputStream and OutputStream of their respective connections, and start sending each other data. All you have to do is send the right data down the stream and read it and interpret it correctly on the other side. What could be easier? 😉

For the bluetooth version of the checkers game, I eliminated the “taunt” feature I’d added for SMS Checkers. With SMS checkers, you can play against your friend wherever he may be. When you start up the game and enter your friend’s phone number — thanks to the push registry — the invitation SMS will buzz his handset to invite him to play whether he’s on the other side of the world or in a meeting or whatever. So it’s useful to be able to send a text message along with every move. In the bluetooth version you essentially have to have already agreed together that you want to play, so the taunt feature is less useful. It would have been possible to hook up the bluetooth version to the push registry as well, but I figured it was already complicated enough as it is.

So please try it out and tell me what you think!

Advertisements

18 comments so far

  1. […] I can really say is that I’m working on improving this in my technical writing. I think my bluetooth article the other day shows improvement on the points the reviewer mentions. I started with an overview of […]

  2. Nick on

    hi, i already try download the bluetooth checkers game, but sadly it got error, here the error said

    “com.sun.kvem.midletsuite.InvalidJadException: Reason = 36
    Error in opening jar file: BluetoothCheckers.jar”

    i really hope i can view the source code, well is will easier for me to understand the game running, if you dont mind, thank you

  3. edi on

    i’v installed blutooth checkers on two moto v3’s .
    they keep saying “waitng for another player.
    i restart both devices then client says “erro finding another player”. i cant get it to work please rectify it.

  4. carolhamer on

    Hi Edi,

    I’d really like to correct this problem, but I can’t troubleshoot on a specific device unless I have the device in hand. I’ve worked on Motorola V3’s, and (like all devices) I’ve gotten some unexpected behavior from them.

    The first thing I would recommend would be to make sure you start the game on one device first and be sure that you select “server” and make sure it is waiting for another player before starting the game on the other device and selecting “client.” I assume you’ve done that, though, so the next thing I’d recommend is to try to install and run the basic bluetooth example that is bundled with the WTK. If that one works and my game doesn’t, then the next step is the troubleshooting that comes from comparing the differences between the two. Again, I’d be happy to do that myself if I had the device in hand…

    Either way, for both you and Nick, you can download all of the source code on the publisher’s site: here.

  5. nabihah on

    Hi Carol,

    (I send the same message earlier on About just in case)

    I tried your codes from your new book using Netbeans. When I compiled them, there are no errors. But when I runned them, it does started in emulator but until the selection of server/client mode only. But then there’s no connection. I’m not sure whether emulator does support bluetooth connection or not. So I tried install them in my phone, and this worst. The game could not be started at all. I don’t know what is wrong.

    I really need your help. How did you run or what did you do to make the game works? (I tried the BluetoothCheckers.jar and it worked perfectly) How did you do that? Please could you help me by giving maybe a step by step on how to make the game run on my phone, please? If it is ok for you, can you send the answer to nabihah_a4@yahoo.com as soon as possible. Please!

    Thanks a lot..

    Nabihah

  6. nabihah on

    Sorry..

    Just in case if you’re wondering what codes are they. They are for the Bluetooth Checkers.

    Thanks for your time!

  7. carolhamer on

    That’s very strange. I haven’t tried compiling this program with Netbeans. I just used the WTK from Sun and the emulator with KToolbar. Normally, if you install the WTK and put the BluetoothCheckers folder in the WTK’s apps folder, then “BluetoothCheckers” will come up on the list of projects you can open when you launch the WTK’s KToolbar application. I developed it with WTK 2.2, but it should be compatible with later versions.

  8. damia on

    Hi,

    i’ve tried running the BluetoothCheckers. On the server side the board did appear but on the client side the board could not be started. The error is Failed:Null. Why’s that happening? Do you have any idea about that..

    Thanks

  9. nabihah on

    Hi Carol,

    I used the WTK and it worked! Thanks..

    But I wondered why does the connection sometimes failed. Sometimes it connects but sometimes not. The error is failed:security block when it is not connected.. Hope you can help!

    Nabihah

  10. Chris on

    Hi Carol, i read your book and needed some advice about the Bluetooth connectivity.

    Is it possible to write the Bluetooth Client code, in the void clientRun() to be seperated to deviceDiscovered, inquirtCompleted, servicesDiscovered and serviceSearchCompleted, instead of writing all in viod ClientRun().

    I am new to j2me and bluetooth, so woud like to hear your advice on this.

  11. carolhamer on

    Nabihah — Is this error on the WTK emulator or on a device? I would start by checking whether the device has some sort of Bluetooth preferences menu where you can modify security parameters.

    Also note that in this game, when creating the Bluetooth connection, I haven’t used any authentication or encryption (look at the places where the service URL is created in both the server and the client run methods). It’s possible that the device doesn’t allow non-authenticating services, but might allow a service that requires authentication or encryption. Otherwise it’s also possible that it’s a Java permission problem.

    Chris — Yes, you can break up this method into sub-methods. You just need to be a little careful with the threading. The search for the service takes time, so it can’t be performed on a thread that needs to return quickly (that is, you can’t call the startInquiry or other search methods directly from startApp or from commandAction — if discovery is triggered from startApp or from a commandAction, then you need to create a new thread and call the discovery methods from within the run method of the new thread). Plus, you need to be careful about waiting for one step in the discovery process is done before moving on to the next step (using wait).

  12. Chris on

    Thank for your prompt response Carol, I tried putting them into sub-methods, but i still fail to do so. I am able to compile but i whenever i run it, it will just show waiting for another player. I think i must have put those codes wrongly.

    Is it possible if you show me how to put them into sub-methods?

    Your advice is very important to me.

  13. Chris on

    Hi Carol, can you list down which phone have you tested the bluetooth checkers? I can run it in WTK but it didn work on nokia 6600 and n81 phones on real lift.n Thank u!

  14. Joe on

    Hi Carol, I read your book and i find it very useful. I’m thinking of adding some functionality to this application. I’m interested in making this application support both bluetooth and internet connection. How and from where do i start to do that? Your advice is very much appreciated. Thanks

  15. nagsen on

    server side coding to send answer to mobile vaya bluetooth

  16. Ashwin on

    Hi,

    I develop mobile games myself on java. However I am having trouble connecting multiple clients to a single server to play a game. I’ve looked it up on many forums but havent been able to find what i need. Would greatly appreciate it if you could help.
    thanks.

  17. carolhamer on

    Hi Ashwin — Sorry it has taken me so long to reply to this.

    The thing is that it depends on what technology you want to use. You can do it with Bluetooth if all of the players are in the same room, but I think it would be more common to use http over the Internet. I don’t have an example game handy, but if you do a search for “client/server multiplayer games” that should probably turn something up.

  18. dj on

    Has someone able to make this work on a real device(nokia phones)?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: