65.9K
CodeProject 正在变化。 阅读更多。
Home

打破常规 – BlackBerry 10 移动传感入门

2012年12月20日

CPOL

2分钟阅读

viewsIcon

5686

打破常规 – BlackBerry 10 移动传感入门

BlackBerry® 10 智能手机内置了多种传感器,用于检测运动、方向、旋转、接近度和磁场。随着移动设备作为计算平台的成熟和功能日益丰富,这些进步通常伴随着新传感器的引入。智能手机上拥有的生理感知能力实在令人惊叹。相机——设备上使用最广泛的传感器——使其能够“看到”外部世界,而麦克风则让设备能够“听到”。事实上,BlackBerry 10 具有双摄像头(前后)和双麦克风,以实现空间分辨率。

让设备“看到”的其他方式包括光线传感器和接近传感器。将 BlackBerry 10 设备视为传感器,自然会引出确定环境上下文的方法,并基于这些上下文提供无与伦比的用户体验。作为开发者,您可以在应用程序中利用以下类型的传感器:加速度计、磁力计、温度传感器、陀螺仪、指南针、接近传感器、腰带传感器、旋转传感器、GPS、麦克风和摄像头。

有关更多信息,请参阅我们的 传感器文档,并立即开始构建。

文档很好,但示例更易于阅读(至少对于开发者而言)。考虑到这一点,我们还提供了一个完整的 示例应用程序,它既实用又有趣。

我们对此感到非常兴奋,并期待看到您如何在应用程序中集成传感器。让我们打破传感器仅用于游戏和地图的错误认知!

示例应用程序示例 – SensorDemo

SensorDemo 示例演示了如何使用 QtSensor 模块来实现 3D 旋转、运动警报、指南针、手电筒、运动警报和碰撞检测器。该示例基于原生层(Cascades/QML)构建。

要求

BlackBerry 10 Native SDK Beta 3

运行示例

  1. 克隆示例仓库 示例应用程序
  2. 启动 BlackBerry 10 Native SDK Beta 3,然后从“文件”菜单中选择“导入”。
  3. 展开“常规”,然后选择“将现有项目导入工作区”。点击“下一步”。
  4. 浏览到示例目录的位置,然后点击“确定”。
  5. 示例项目应显示在“项目”部分中。点击“完成”将项目导入到您的工作区。
  6. 在“项目资源管理器”窗格中,右键单击项目(例如 SensorDemo),然后选择“构建项目”。
  7. 在“项目资源管理器”窗格中,右键单击项目(例如 SensorDemo),然后选择“运行方式”>“BlackBerry C/C++ 应用程序”。
  8. 应用程序现在将安装并启动在您的设备上;如果未启动,您可能需要 设置您的环境

示例代码

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
            }
        }
    }
}
© . All rights reserved.