Tools

Namespaces

Variants

Views

Actions

Search

Contents

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Remove the "8" in the title - we use Categories to indicate the platform version and only specify in title if the article will only apply to that version (ie for Bing Maps article that is only for WP7 then the "7" makes sense)

Use the standard styles - bold for filenames, Icode for inline code

Update the introduction to say more about what the article explains and covers. I'd probably reproduce your screenshots here too

Wiki style is dry - covers what works more than how you found out about it (so its not like a blog or a story). For example readers generally don't care that " Finally I found a message on a forum from a developer of the API etc" - the only reason to mention another developer is if you're explicitly acknowledging their contribution.

Minor restructure.

I'll be away next week, so any changes would be after that. Of course it is your article, so feel free to modify or update in the meantime.

Hamishwillee - Subedited/Reviewed

Hi Olivier

I've subedited as discussed, mostly to improve some of the wording and style. Please check I didn't change any "meaning" - in particular around the "example messages" which I tried to present in a more compact and clear format. You can see changes in the history

Is there are particular version of the Sphero SDK we should include in the "dependencies" section of the Article Metadata (top right of page). This will help if they publish a new version and break something in your code.

Thanks again for this. It is completely original and still IMO very cool!

Introduction

Sphero is a ball than can be controlled by Bluetooth on Windows Phone 8 (and other mobile OS, using their respective SDKs). The SDK allows you to control the lights, move the ball, manage collision detection, create macros, and perform other tasks.

This code example shows how to control lights and move the ball. There is a fully working Visual Studio solution attached, and screenshots of the demo app are shown below.

About the APIs

Warning: The documented API is not exactly the same as in the SDK. Problems with the GetVersioning command are discussed later.

Command packet

The format of the command packet is shown below.

For example, the command to get Bluetooth information is: 0x00 (DID) 0x11 (CID) (See API p13). I put SEQ = 1 and DLEN is always 1 and I set the CHK byte to 1 too. So the full command packet message will be : 0xFF,0xFF,0x00,0x11,0x01,0x01,0xF2

An example of a command packet with some data would be one to set the back light. The command to set the backlight is 0x02, 0x21. The data is a byte giving the brightness value from 0 to 255 (0xFF in hex). We set DLEN = 0x02, and 0x01 for CHK. So in order to set the brightness to 255 the command packet would be: 0xFF,0xFF,0X02,0x21,0x01,0x02,0xFF,0xDA

Response packet

The response format is as shown below:

SEQ is the same SEQ byte as in the command sent, so you know to which command it is in the response. Often there is no <data> part as it is just an acknowledgement of the command. Such answer is called SIMPLE ANSWER in the documentation.

For the MSRP, check the APIO document appendix for the values (0x00 = OK)

Stream mode

You can set the message to stream which is faster. Note however that in this case responses are not sent and messages can be lost.

To do this mode set the SOP2 to 0xFE. Example with the set brightness command : 0xFF,0xFE,0X02,0x21,0x01,0x02,0xFF,0xDA

I get the first 5 bytes of the answer. I get the message response code (the third byte) and I get the rest of the message .If it is success, it should be 9 bytes. If not, just one (the CHK). Anyway, I get the remaining length using the 5th byte of the answer (DLEN).

Now I check if the response is successful or not. If not, I thrown an exception. If success, I create a VersioningInfo.

Roll

This is probably the main command.

This is straightforward - just set a heading and a speed. Heading is 0 to 359. 0 being straight line on positive Y axis.

I limit orientation to 90 degrees max (pi/2). To get the angle, I use the Atan to get an angle (in radian) from a tangent (y/x). I add 90 degrees (pi/2) as for Sphero, heading 0 (angle 0) is on the positive Y axis, not on the positive X as it is by default for math. Then I multiply by 180/Pi so I have degrees and not radians.

For the speed, the norm is simply square root of (x*x + y*y). I divide by Pi/2 (as Pi/2 is the maximum) in order to have a number between 0 and 1. Then, multiply it by 255 to have a number in the range 0..255.