QML and BBM. Together at last.

QML and BBM. Together at last.

QML and BBM together at last

QML and BBM together at last

When it comes to QML almost everyone is starting from scratch, so I thought that I’d write a few articles (I dare not say tutorials) on how I’ve managed to put things working with my app. For some background, I’m not only new to QML but also to BlackBerry development. So please feel free to comment and correct as I go along.

Sometimes sample code is just a sample

All samples aren’t made equal, and when it comes to the case of BB10 sample code, this couldn’t be more true. All props to the folks at BlackBerry but since I have a background in .NET and it’s quite a mature platform, I have the luxury of finding any amount of great samples (and bad ones). But this is besides the point – sometimes sample code is just a sample – meaning you should “copy and paste with a grain of salt”

Today, it’s about QML and BBM, you can find community sample code in here. When I first started to add this to my code I attempted the copy/paste method, didn’t work. And why should it? I didn’t even run the sample! But more importantly, you don’t need to copy/paste the entire sample – you need to “integrate” the code into your application. Here’s what I did…

  1. Make sure the sample actually works (it’s community code running on beta code – do the math)
  2. Start small. I started with “invite to download” – mistake. Start with the registration sample
  3. Understand what the sample is doing
  4. If you don’t know C++ you don’t know jack! (sort of)
From there, I took the sample code and integrated it into my application. This was probably the hardest part because you need to figure out what you do and don’t require for your app. But like I said, if you start small you’ll be fine – after all if your app is going to use BBM, it must register first.

If you don’t know C++, you don’t know jack!

This isn’t completely true, but if you know or understand a little C++ (or any programming language) you’ll be better off when it comes to integrating QML and BBM. For me, I knew C# and a little PHP but C++ is still killing me ,so much so, that I actually started using a lot more Qt than C++ when trying to deal with strings and arrays but that’s another story for another time.

Lets look at what the plan…

  • Forget the “landing page” and continue button in the sample
  • My app will launch as normal and during this process it will register with BBM
  • Succeed or fail, the app will continue to it’s main page/tab/etc and notify the user if necessary
  • Once completed (hopefully with success) the user can then do BBM “stuff”
So that’s it for the registration and lets look at some code…
  • Remove the QML related code from RegistrationHandler.cpp/hpp (all we want are the method calls)
RegistrationHandler::RegistrationHandler()
void RegistrationHandler::appRegister()
void RegistrationHandler::checkRegistrationAccess()
void RegistrationHandler::registrationStatus(bb::platform::bbm::RegistrationState::Type state)
void RegistrationHandler::registrationFailed()
  • In your main cpp class (not main.cpp but your appName.cpp file) create an instance of RegistrationHandler and call appRegister

For my app, I decided since I’ll be doing a number of BBM calls that I’ll define some  stuff and use them in a switch statement like so…

        
#define REGISTERBBM 1 //placed at the top of the class along with other stuff
case REGISTERBBM: {
regBBM = new RegistrationHandler();
regBBM->appRegister();
break;
}

This makes the call and your app goes through the registrationStatus() method to determine the state of the process – unregistered, pending, success, etc. If everything works out, you’re done! Easy right? But most likely things didn’t work out your first time (it didn’t for me) so what do you do when your code doesn’t work???

Your code doesn’t work, what do you do…What Do You Do?

So there are a few things you need to know before you attempt to register your app – I know what you’re saying. Shouldn’t you have told me this at the beginning? And the answer is yes, yes I should have :) Anyways, a couple of things and explanations…

Your app needs permission:

In your bar descriptor file you need to add

<permission>bbm_connect</permission>

and in your .pro file add

LIBS += -lbbplatformbbm

this should be your first step.

Device or Simulator:

I don’t know if this works on the simulator but from my past experience, a lot of things don’t work “as expected” on the sim, so if you want to do BBM testing I’d recommend using the device – and obviously you need a WiFi connect.

UPDATE: BBM does not work on the sim. After trying to debug the issue for a few hours I found this confirmation on the BlackBerry support forum post – now I’m looking into how to comment out BBM and still do dev work on the sim if I don’t have the device handy.

Remove QML in sample:

The reason why I did this is because 1) I already had an almost complete working app and only needed to integrate BBM. 2) I found there to be no need to have a dedicated page to telling the user if connection was successful (or not) and then to have a button which allows the user to continue to the main page. Whether connection passes or fails you still want the user to access your app (depending on your app functionality of course). So getting rid of the QML was obvious.

Also doing it this way allows (in my opinion) for better control and integration into your app. Using the switch statement made it alot easier for me to deal with BBM and debug problems later on. This also lets me change in/out any new BBM methods when updates occur or to reuse the functionality in future apps

switch (state) {
case REGISTERBBM: {
regBBM = new RegistrationHandler();
regBBM->appRegister();
break;
}
case PRESONALMESSAGE: {
UpdateProfilePage *updateProfilePage = new UpdateProfilePage(m_userProfile);
updateProfilePage->savePersonalMessage();
break;
}
case STATUSMESSAGE: {
UpdateProfilePage *updateProfilePage = new UpdateProfilePage(m_userProfile);
updateProfilePage->saveStatus();
break;
}
}

So that’s it

Integrating Invite to Download, ProfileBox and ProfileUpdates are just as easy. But if you’d like a little more details, extra code or another article on QML and BBM please  jot it down in the comment section and I’ll try to oblige.