If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
nanyaran
Posts: 19
Registered: ‎06-21-2012
My Device: z10
My Carrier: Bell
Accepted Solution

How can I get charging state?

Basically, I am using a sample code to lock a device. Now, I am thinking, if the device can detect the charging state then lock the phone. Therefore, my code is this: (i haven't write the locking code yet)

            if(batteryInfo.chargingState()== BatteryChargingState.Charging){

               }

 

After compiling, it gave me an error that said "error: expected primary-expression before '.' token".

I dont quite know how to fix it. Anyone can help me? Thank you.

Please use plain text.
Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810
My Carrier: Verizon

Re: How can I get charging state?

I believe the issue, looks likely related to a namespace missing one...

 

instead of BatteryChargingState.Charging , could you try bb::device::BatteryChargingState.Charging

 

and ensure you have LIBS += -lbbdevice in your .pro file

Please use plain text.
Contributor
nanyaran
Posts: 19
Registered: ‎06-21-2012
My Device: z10
My Carrier: Bell

Re: How can I get charging state?

Thank you for your replying. I tried. That is not the issue. It still gave me the same error.

 

Please use plain text.
Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810
My Carrier: Verizon

Re: How can I get charging state?

ok... then i believe you have not created an instance of batteryInfo before using it in batteryInfo.chargingState(). If it still doesnt resolve could you paste the entire code...

 

 

Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: How can I get charging state?

Hi,

 

Use ':' instead of a dot:

if(batteryInfo.chargingState() == BatteryChargingState::Charging) {
               }

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
nanyaran
Posts: 19
Registered: ‎06-21-2012
My Device: z10
My Carrier: Bell

Re: How can I get charging state?

Here is the code that i am trying to compile:

_______________________________________________________

#include <bb/cascades/AbstractPane>
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/device/BatteryInfo>
#include <bb/device/BatteryChargingState>

#include <QtCore/QLocale>
#include <QtCore/QTranslator>

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

/**
 * This sample application shows some basic
 * usage of the BatteryInfo API, such as charging level and charging state.
 */
Q_DECL_EXPORT int main(int argc, char **argv)
{
    qmlRegisterUncreatableType<bb::device::BatteryChargingState>("bb.device", 1, 0, "BatteryChargingState", "");

    Application app(argc, argv);

    QTranslator translator;
    const QString locale_string = QLocale().name();
    const QString filename = QString("batterysample_%1").arg(locale_string);
    if (translator.load(filename, "app/native/qm")) {
        app.installTranslator(&translator);
    }

//! [0]
    // Create the battery info object
    BatteryInfo batteryInfo;

    // Load the UI description from main.qml
    QmlDocument *qml = QmlDocument::create("asset:///main.qml");

    // Make the BatteryInfo object available to the UI as context property
    qml->setContextProperty("_battery", &batteryInfo);
    if(batteryInfo.chargingState()== 2){
        printf("Its charging\n");
    }
    else
        printf("Its not charged");
//! [0]

    // Create the application scene
    AbstractPane *appPage = qml->createRootObject<AbstractPane>();
    Application::instance()->setScene(appPage);


    return Application::exec();
}

 

Please use plain text.