Saturday, August 10, 2013

Using VibrationController API wit BB10 Cascades QML

I tried to add vibration support to my BB10 application Counter. Application is simple, which just increment the count when you press add button and decrements count on minus button. Simple but useful, I use it quite often to count the stuffs.

But you need to see at screen to see if it button is really pressed, so I thought to add vibration support, so I know button is pressed even without looking at screen. This is first time I am adding Vibration support to any of BB10 application. So I thought to share code.

Code is much simple, only trick is that you need to register VibrationController c++ class to QML system.

Here is how it goes.

First we need to link device lib which has VibrationController API. Add following line to your .pro file and you are good to go.

LIBS += -lbbdevice

Now in main.cpp you need to add proper header to locate VibrarionController API and the register it to QML registry.
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <bb/device/VibrationController>

using namespace bb::cascades;
using namespace bb::device;

int main(int argc, char **argv)
{
    Application app(argc, argv);

    qmlRegisterType<vibrationcontroller>
        ("bb.vibrationController", 1, 0, "VibrationController");

    new Counter(&app);

    return Application::exec();
}

Now we are ready to use VibrarionController with QML code. First you need to import VibraionController QML by import statement. Then we are creating it by defining it in attachedObjects. Once this is done, we can use it's start API to vibrate phone with certain intensity for certain durarion.

import bb.cascades 1.0
import bb.vibrationController 1.0

Page {
    id: page
    Container {
        id: root
        layout: DockLayout {}
        preferredHeight: 1280
        preferredWidth: 768
        touchPropagationMode: TouchPropagationMode.Full;
        
        attachedObjects: [
            VibrationController {
                id: vib
            }
        ]

        function vibrate() {
            //first parameter intensity, second parameter duration for vibration
            vib.start(80, 100);
        }

        Button{
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center 
            onClicked: {
                root.vibrate();
            }
        }
    }
}

Thant's all, hope this will be helpful.

1 comment: