我们知道传感器在现代手机中非常重要,我们需要使用它做一些有创新的应用。这里我们来显示怎么在Ubuntu上使用它所提供的传感器。
1)显示所有的传感器
为了能够使用sensor的API,我们必须包含如下的库:
import QtSensors 5.0
在这里我们来做一个例子来显示所有传感器的列表。我们知道QML提供了一个便利的方法可以很方便地列车所有已有的传感器。
Component.onCompleted: {
var types = QmlSensors.sensorTypes();
console.log(types.join(", "));
}
为了很方便地显示,我们用一个列表来显示所有被支持的sensor。
ListView {
width: parent.width
height: parent.height/2
delegate: Text {
text: modelData
}
model:QmlSensors.sensorTypes()
}
这里是个非常简单的列表,直接使用QmlSensors的方法sensorTypes()来作为一个model数据。我们以使用一个最简单的delegate来显示数据。
2)使用传感器数据
Accelerometer
我们可以使用如下的方法来得到传感器的数据:
Accelerometer {
id: accel
active: true
dataRate: 20
onReadingChanged: {
accelLabel.text = "Accel " + "x: " + reading.x.toFixed(1) +" y: " + reading.y.toFixed(1) + " z: " + reading.z.toFixed(1)
}
}
TiltSensor
我们可以使用如下的方法来得到传感器的数据:
TiltSensor {
id: tilt
active: false
onReadingChanged: {
tiltLabel.text = "Tilt " + "x " + tilt.reading.xRotation.toFixed(1) + " y " + tilt.reading.yRotation.toFixed(1);
}
}
AmbientLigthSensor
我们可以使用如下的方法来得到传感器的数据:
AmbientLightSensor {
active: true
onReadingChanged: {
if (reading.lightLevel === AmbientLightReading.Dark) {
lightLabel.text = "It is dark"
} else if ( reading.lightLevel === AmbientLightReading.Twilight) {
lightLabel.text = "It is moderately dark";
} else if ( reading.lightLevel === AmbientLightReading.Light) {
lightLabel.text = "It is light (eg. internal lights)";
} else if ( reading.lightLevel === AmbientLightReading.Bright) {
lightLabel.text = "It is bright (eg. shade)";
} else if ( reading.lightLevel === AmbientLightReading.Sunny) {
lightLabel.text = "It is very bright (eg. direct sunlight)";
}else if ( reading.lightLevel === AmbientLightReading.Undefined) {
lightLabel.text = "It is unknown";
}
}
}
OrientationSensor
我们可以使用如下的方法来得到传感器的数据:
OrientationSensor {
active: true
onReadingChanged: {
orientationLabel.text = "something happened"
if ( reading.orientation === OrientationReading.TopUp) {
orientationLabel.text = "TopUp";
} else if ( reading.orientation === OrientationReading.TopDown) {
orientationLabel.text = "TopDown";
} else if ( reading.orientation === OrientationReading.LeftUp) {
orientationLabel.text = "LeftUp";
} else if ( reading.orientation === OrientationReading.RightUp) {
orientationLabel.text= "RightUp";
} else if ( reading.orientation === OrientationReading.FaceDown) {
orientationLabel.text = "FaceDown";
} else if ( reading.orientation === OrientationReading.FaceUp) {
orientationLabel.text = "FaceUp";
}
}
}
RotationSensor
我们可以使用如下的方法来得到传感器的数据:
RotationSensor {
id: rotation
onReadingChanged: {
rotationLabel.text = "Rotation x: " + rotation.reading.x.toFixed(1) + " y: "
+ rotation.reading.y.toFixed(1) + " z: " + rotation.reading.z.toFixed(1);
}
}
SensorGesture
SensorGesture {
id: sensorGesture
Component.onCompleted: {
console.log("The invalid gestures: ");
for ( var gesture in invalidGestures ) {
console.log(" invalid[" + gesture + "]: " + invalidGestures[gesture])
}
console.log("The available gestures: ");
for ( var gesture in availableGestures ) {
console.log(" available[" + gesture + "]: " + availableGestures[gesture])
}
}
gestures: ["QtSensors.SecondCounter",
"QtSensors.cover",
"QtSensors.doubletap",
"QtSensors.hover",
"QtSensors.freefall",
"QtSensors.pickup",
"QtSensors.shake2",
"QtSensors.slam",
"QtSensors.turnover",
"QtSensors.twist",
"QtSensors.whip",
"QtSensors.shake"
]
enabled: true
onDetected: {
console.log("detected gesture: " + gesture);
}
}
3) 源码
整个测试程序的源码可以在如下的网址找到:
在手机上的运行结果如下: