Monday, February 12, 2007

Cell Phone development - w300i



History

In November, the two-year contract on my cell phone ran out. This allowed me to upgrade my phone to a new phone. I really only needed to get a new battery, because the one I had was down to about a four-hour recharge cycle. However, it was cheaper to get a new and better phone than to get a new battery, because of the contract extension. I initially looked at the Motorola RAZR, and while researching it, found some hacking information. As a professional developer, this got me thinking about programming the phone in more official ways than hacking, and this led me to the Java 2 Micro Edition (J2ME). I also wanted a phone that had good mp3 playback, so I wouldn't have to carry a separate iPod. What I really wanted was the iPhone, of course, but it hadn't been announced yet. In the end, I selected the Sony Ericsson w300i, a nice Walkman phone packed with features for the price (it was $9.99 with the Cingular contract extension, since reduced to $0.00). As a consolation, a few days after the iPhone announcement, Sony Ericsson announced eight new phones, which have similar development environments to the w300i.

w300i features
The w300i has J2ME support, including the JSR184 3D gaming API as well as a Mascot Capsule micro 3D. It also supports bluetooth for headset use, computer USB connection, FM radio, mobile TV and e-mail, and multiplayer gaming. As you will see, this last bit was something of a sticking point with me, but as of today it has a happy ending! More on that later; first I want to walk you through getting started with development for this device.

Developer signup
Having received the phone just in time for Christmas, I immediately began looking around for software development kits (SDK's). The best place to start, if you are a Cingular customer, is here, at developer.cingular.com. You will have to create an account, but it is free. Within that site is a lot of good information about phones, including Sony Ericsson. If you want to jump right in and get started with an SDK for the SE phones, it is here. The sites seem to follow the pattern of developer.x.com, including Motorola and others. Before you install the SE SDK, though, you will have to click on the release notes link to get your prerequisites set up. This includes the Sun J2SE SDK if you don't already have it. You will also need DirectX 8.1 or later, to run the emulators. With these installed, you are ready for the SE SDK mentioned above.

Sample projects
Once you have installed the SDK, you will have a new set of menu items under Sony Ericsson/Java ME SDK for CLDC. CLDC and MIDP are abstractions/subsets of Java that work for mobile phones. The w300i and most modern mobile phones use CLDC 1.0 and MIDP 2.0, which means your phone emulator will appear under the WTK2 menu under Sony Ericsson/Java ME SDK for CLDC/WTK2. You can also integrate all of this into an IDE, but you don't need to; it runs fairly smoothly as it is. The first program to run is Default Device Selection, which you only need to run once. Choose SonyEricsson_w300_Emu from the dropdown and hit ok. Done. Now you will want to run ktoolbar under the same menu (WTK2). There should be a number of sample projects prepopulated into this program, so you can just pick Open Project and choose one to run.

Bluegammon
There is one sample that is a fairly complete application, called Bluegammon. It plays backgammon as single or multi player, through bluetooth. Or at least it would, if Cingular's security on this phone weren't so draconian. At any rate, without even connecting the phone you can run this program in the emulator, simply by opening the project, selecting the phone you want to emulate, and choosing run. A phone interface will come up, and the important thing to note is that the only way to interact with the application is through the buttons of the phone. You will try clicking on the phone screen itself, just out of habit, but when it doesn't work, you will remember that the buttons of the phone are what you need to click on. When you are ready to build the .jar and .jad files, which are the actual distributable executables, you just go to the ktoolbar Project menu and choose Package/Create Package. Don't worry about obfusacting or signing for now. If you have a Cingular-branded phone, the signing won't do much anyway unless you are already a Cingular Preferred vendor. I'll cover debranding your phone in a later post.

Installing programs
This post will walk you through installing .jar files onto the phone, through the data cable, without even using the SDK. Anyone can do this, without signing up as a developer. Here, I will cover on-device debug, and installing the projects that you have built from sample code or on your own.

Connecting to phone
From ktoolbar, you can open and build a project, run it, and create the .jar files, as I have discussed. Once you have done that, what you really want to do is run the program on the phone. The documentation is not very clear on this, but I will clue you in to the simple steps you need to do. From the "All Programs/Sony Ericsson/Java ME SDK for CLDC" menu on your computer, there is a program called "Connection Proxy Settings". This opens a file in notepad. The only thing you need to do in that file is change the comm: setting to comm: COM4. This should work for most computers, but you may have to go higher. Save that file and exit notepad. With your phone plugged in, and in Phone mode (not File Transfer), then choose the DeviceExplorer from the same CLDC computer menu. (It's OK if you have PC Suite installed and it connects to that too). The DeviceExplorer will automatically start the Connection Proxy. If the Connection Proxy fails to connect to your device, you might have to choose the settings icon (looks like a little green gear) from its menu. This will let you choose a port and baud rate. Again you want COM4.

Connection troubleshooting
In order to get connected, I had to open the PC Suite program (the little cell-phone icon in your system tray) and from its menu choose Tools/Phone Monitor Options... which brings up a dialog box. In that dialog box, there is a tab called COM ports. Here you want to make sure that any real com ports you have on your computer are disabled. That's a little anti-intuitive, but the device proxy drivers set up a fake com port above the ones on your computer. Reserved can be left at No. IMPORTANT NOTE: never shut down your computer with the phone connected. This messes up PC Suite and causes a program called generic.exe to crash when you reboot. It also makes your computer really slow. If this happens, just connect and unconnect the phone again, and restart the computer (this may take a while).

On-device debugging
Contrary to what I thought at first, you don't have to install anything special onto your phone to do on-device debugging. Once DeviceExplorer is connected and running, you should see a menu of stuff on your phone on the left, and a WTK applications folder on the right. If you open up this folder, you will see all of your projects from ktoolbar under it. If you open one of these, such as Bluegammon/bin, you will see your .jad and .jar files. To install to the phone, just right-click the .jad or .jar and choose Install. Note that this lets you skip all the stuff outlined in my other post. This puts the program on your phone, on the left. Then you can select the item on the left, and click the "play" icon in the menu above it. This starts the program on your phone. Now you can see the output from that program running in the window below. This includes System.out.println output, as well as exceptions. If you have Cingular, just go to the menu on the phone and try to play via bluetooth and set up a server. This will cause a security exception unless you sign the program with a Cingular Preferred certificate, and turn on bluetooth manually on the phone. Alternately, you can debrand the phone to overcome this limitation, through something like wotanserver.com or davinciteam.com. This is different from and independent from unlocking. I will walk through this process in a future post.

Final important note:
Once you have DeviceExplorer running, the order of shutdown is important, or it will cause their programs to hang. Start by shutting down the device explorer. This will leave the device proxy running and connected. Disconnect before shutting this down, or it will hang. You can disconnect the phone without exiting the PC Suite program; that is, leave it minimized in the system tray. It will show up with a red X again when you exit it. You can shut down ktoolbar whenever you want; I haven't had issues with it.

2 comments:

jaywalker said...

Very well written!

DanielShapiro said...

thanks. good post!