Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Breaking Out of The Box – An Introduction to BlackBerry 10 Mobile Sensing

20 Dec 2012 1  
Breaking Out of The Box – An Introduction to BlackBerry 10 Mobile Sensing

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

BlackBerry® 10 smartphones have a variety of built-in sensors to detect movement, orientation, rotation, proximity, and magnetic fields. As mobile devices have matured as a computing platform and acquired richer functionality, these advancements often have been paired with the introduction of new sensors. It is truly astonishing how many physiological senses are present on a smartphone. The camera – the most widely used sensor on the device – allows it to “see” the outside world, while the microphone lets the device “hear”. In fact, BlackBerry 10 has dual cameras (front and back) and dual microphones for spatial resolution.

Other means to let a device “see” include the light and the proximity sensor. Considering BlackBerry 10 devices as sensors should naturally lead to methods of determining environmental contexts, and providing an unmatched user experience based on those. As a developer you can leverage the following types of sensors in your application: accelerometer, magnetometer, temperature, gyroscope, compass, proximity, holster, rotation sensor, GPS, microphone and camera.

For more information, please take a look at our sensor documentations and start building today.

Documentations are good but samples are readable (well, at least to developers). With that in mind, we also have a full sample application that is both useful and fun to use.

We are very excited about this and look forward to see how you incorporate sensors in your application. Let’s break the false perception that sensors are only meant for games and maps!

Sample Application Example – SensorDemo

The SensorDemo example demonstrates how to use the QtSensor module to implement the Rotation 3D, Motion Alarm, Compass, Flashlight, Motion Alarm and Collision Detector. This sample made on native layer (Cascades/QML).

Requirements

BlackBerry 10 Native SDK Beta 3

Running the example

  1. Clone the Sample repository sample application.
  2. Launch BlackBerry 10 Native SDK Beta 3, and from the File menu, select Import.
  3. Expand General, and select Existing Projects into Workspace. Click Next.
  4. Browse to the location of your sample directory, and then click OK.
  5. The sample project should display in the Projects section. Click Finish to import the project into your workspace.
  6. In the Project Explorer pane, right-click the project (for example SensorDemo) and select Build Project.
  7. In the Project Explorer pane, right-click the project (for example SensorDemo and select Run As > BlackBerry C/C++ Application.
  8. The application will now install and launch on your device; if not, you might have to set up your environment.

Sample Code

main.qml:
import bb.cascades 1.0
import QtMobility.sensors 1.2
TabbedPane {
    id: tabPane
    showTabsOnActionBar: true
    onCreationCompleted: {
        OrientationSupport.supportedDisplayOrientation = SupportedDisplayOrientation.All;
        tabPane.activeTab = compassTab;
    }
/* Block of statements for other tabs such as Motion Alarm, Compass, Flashlight, Motion Alarm and Collision Detector.*/
Tab {
        id: rotation3DTab
        title: qsTr("Rotation 3D")
        imageSource: "images/rotation3d.png"

        Page {
            ControlDelegate {
                source: "rotation3D.qml"
                delegateActive: (tabPane.activeTab == rotation3DTab)
            }
        }
    }
}


rotation3D.qml:
import bb.cascades 1.0
import bb.multimedia 1.0
import QtMobility.sensors 1.2

Container {
//! [0]
    attachedObjects: [
        RotationSensor {
            id: rotation

            // Create variables to hold rotation reading values
            property real x: 0
            property real y: 0
            property real z: 0

            // Turn on the sensor
            active: true

            /* Keep the sensor active when the app isn't visible or the screen is off (requires app permission in bar-descriptor) */
            alwaysOn: true

            /* If the device isn't moving (x&y&z==0), don't send updates, saves power*/
            skipDuplicates: true

            onReadingChanged: { // Called when a new rotation reading is available
                x = reading.x
                y = reading.y
                z = reading.z
            }
        }
    ]
//! [0]

    layout: DockLayout {}

    ImageView {
        horizontalAlignment: HorizontalAlignment.Center
        verticalAlignment: VerticalAlignment.Center

        imageSource: "images/device.png"
    }

    Container {
        layout: AbsoluteLayout {}

//! [1]
        Label {
            layoutProperties: AbsoluteLayoutProperties {
                positionX: 480
                positionY: 685
            }

            text: qsTr("%1\u00B0").arg(rotation.x.toPrecision(4))
            textStyle {
                base: SystemDefaults.TextStyles.BodyText
                color: Color.Yellow
                fontWeight: FontWeight.Bold
            }
        }
//! [1]

        Label {
            layoutProperties: AbsoluteLayoutProperties {
                positionX: 480
                positionY: 460
            }

            text: qsTr("%1\u00B0").arg(rotation.y.toPrecision(4))
            textStyle {
                base: SystemDefaults.TextStyles.BodyText
                color: Color.Yellow
                fontWeight: FontWeight.Bold
            }
        }

        Label {
            layoutProperties: AbsoluteLayoutProperties {
                positionX: 335
                positionY: 390
            }

            text: qsTr("%1\u00B0").arg(rotation.z.toPrecision(4))
            textStyle {
                base: SystemDefaults.TextStyles.BodyText
                color: Color.Yellow
                fontWeight: FontWeight.Bold
            }
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here