/* Code Musings */

Working with the Blackberry Platform Services (BPS), Part 1

I’ve received many questions about the best way to use BPS. The Blackberry Platform Services, or BPS for short, is an API that allows your application to receive events form the system on the Blackberry 10/Playbook OS. It is similar to the Win32 APIs that allow you to create and control a window. The API is relatively well documented and there are many examples available. However, depending on what tutorial you read, it only showcases a specific functionality of the API. So, this blog entry will detail the way to properly initialize BPS and have it do whatever you like.

Initialization of the screen is rather simple. You start with a screen context, which defines your application. It is similar to registering a WNDCLASSEX in Win32.

Initialization of the context is first and the destruction of it must occur last. It is good practice to put these in a constructor and destructor. Next, the initialization of BPS must happen.

This allows the application to receive events. In Win32, you would provide a WNDPROC, a callback that is called when events come in. Unlike the even callback mechanism in Windows, BPS is a poll mechanism. It stores (or caches) events until they are used. It is now safe to create a window to draw on.

There are many options for window initialization. The window properties in your application may vary, depending on what you want to display. Please refer to this documentation for details on window properties.

Finally, you need event handling. The event handling mechanism is based on event IDs. You need to register for them explicitly.

It should be noted that the accelerometer events are not actual events. Rather, they need to be polled for in addition to window events. The checking of events is fairly straight forward. I would advise that event IDs are not hard coded. Poll for the Event IDs once, and cache them in an array. This will keep your code forwards compatible in the event that the IDs are changed.

Here is my message loop. It resolves the event domain ID using the mapping created above for fast look up. Then call your event handlers. The getNextEvent() method is also capable of waiting until an event is posted.

That’s pretty much it, in terms of setup. The next part will contain event handling for each of the event domains.

Leave a Reply