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
- Clone the Sample repository sample application.
- Launch BlackBerry 10 Native SDK Beta 3, and from the File menu, select Import.
- Expand General, and select Existing Projects into Workspace. Click Next.
- Browse to the location of your sample directory, and then click OK.
- The sample project should display in the Projects section. Click Finish to import the project into your workspace.
- In the Project Explorer pane, right-click the project (for example SensorDemo) and select Build Project.
- In the Project Explorer pane, right-click the project (for example SensorDemo and select Run As > BlackBerry C/C++ Application.
- 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;
}
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 {
attachedObjects: [
RotationSensor {
id: rotation
property real x: 0
property real y: 0
property real z: 0
active: true
alwaysOn: true
skipDuplicates: true
onReadingChanged: { x = reading.x
y = reading.y
z = reading.z
}
}
]
layout: DockLayout {}
ImageView {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
imageSource: "images/device.png"
}
Container {
layout: AbsoluteLayout {}
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
}
}
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
}
}
}
}