Android SensorManager 中的 getSensorList() 与 getDefaultSensor()

发布于 12-06 23:52 字数 552 浏览 0 评论 0原文

我正在为 Android 编写一个游戏,希望能够使用加速度计进行输入。

我看到两种获取传感器的方法,一种方法是使用 SensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER) 的第一个元素,另一种方法是 SensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)代码>.

getDefaultSensor 文档 说它可以返回一个“复合”传感器,所以如果我想要一个“原始”传感器,我应该使用 getSensorList

你知道复合传感器和原始传感器之间有什么区别吗?这甚至适用于加速度计吗?有人有使用包含多个或复合加速度计的设备的经验吗? (或者其他传感器?)

I'm writing a game for Android and want to be able to use the accelerometer for input.

I see two ways of getting a sensor, one way is to use the first element of SensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER) and the other is SensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER).

The getDefaultSensor doc says it could return a "composite" sensor, so if I want a "raw" sensor I should use getSensorList.

Any idea what the difference between a composite or raw sensor is? Does this even apply to the accelerometer? Anyone have experience with devices that contain multiple or composite accelerometers? (Or some other sensor?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日落2024-12-13 23:52:43

更新:
他们更新了 Lollipop 中的 getDefaultSensor 方法,现在有一个区别:

public Sensor getDefaultSensor(int type) {
    // TODO: need to be smarter, for now, just return the 1st sensor
    List<Sensor> l = getSensorList(type);
    boolean wakeUpSensor = false;
    // For the following sensor types, return a wake-up sensor. These types are by default
    // defined as wake-up sensors. For the rest of the SDK defined sensor types return a
    // non_wake-up version.
    if (type == Sensor.TYPE_PROXIMITY || type == Sensor.TYPE_SIGNIFICANT_MOTION ||
            type == Sensor.TYPE_TILT_DETECTOR || type == Sensor.TYPE_WAKE_GESTURE ||
            type == Sensor.TYPE_GLANCE_GESTURE || type == Sensor.TYPE_PICK_UP_GESTURE) {
        wakeUpSensor = true;
    }

    for (Sensor sensor : l) {
        if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor;
    }
    return null;
}

因此,如果有多个传感器可用于指定类型,getDefaultSensor 将返回非唤醒版本(除非默认类型是上面实际定义为的 6 种类型之一)唤醒传感器)

顺便说一下,Sensor.TYPE_TILT_DETECTOR、Sensor.TYPE_WAKE_GESTURE、Sensor.TYPE_GLANCE_GESTURE 和Sensor.TYPE_PICK_UP_GESTURE 隐藏在 SDK 中,因为它们仅用于系统 UI。 Sensor.java 源代码中有更多关于它们的详细信息

An update:
They have updated the getDefaultSensor method in Lollipop, and there now is a difference:

public Sensor getDefaultSensor(int type) {
    // TODO: need to be smarter, for now, just return the 1st sensor
    List<Sensor> l = getSensorList(type);
    boolean wakeUpSensor = false;
    // For the following sensor types, return a wake-up sensor. These types are by default
    // defined as wake-up sensors. For the rest of the SDK defined sensor types return a
    // non_wake-up version.
    if (type == Sensor.TYPE_PROXIMITY || type == Sensor.TYPE_SIGNIFICANT_MOTION ||
            type == Sensor.TYPE_TILT_DETECTOR || type == Sensor.TYPE_WAKE_GESTURE ||
            type == Sensor.TYPE_GLANCE_GESTURE || type == Sensor.TYPE_PICK_UP_GESTURE) {
        wakeUpSensor = true;
    }

    for (Sensor sensor : l) {
        if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor;
    }
    return null;
}

So if there are multiple sensors available for the specified type, getDefaultSensor will return a non-wakeup version (unless the default type is one of those 6 above actually defined as a wakeup sensor)

By the way, Sensor.TYPE_TILT_DETECTOR, Sensor.TYPE_WAKE_GESTURE, Sensor.TYPE_GLANCE_GESTURE and Sensor.TYPE_PICK_UP_GESTURE are hidden in the SDK, as they are intended to be used only for the system UI. There's more details on them in the Sensor.java source

长梦不多时2024-12-13 23:52:43

谷歌的文档远远领先于他们的实施。我浏览了 代码存储库(这似乎是2.3.1-ish源代码)并发现:

public Sensor getDefaultSensor(int type) {
    // TODO: need to be smarter, for now, just return the 1st sensor
    List<Sensor> l = getSensorList(type);
    return l.isEmpty() ? null : l.get(0);
}

所以从返回的传感器之间没有真正的区别(而且我认为他们以后不能真正添加​​一个) getDefaultSensor()getSensorList()

Google's documentation is way ahead of their implementation here. I browsed through the code repository (which seems to be 2.3.1-ish source) and found:

public Sensor getDefaultSensor(int type) {
    // TODO: need to be smarter, for now, just return the 1st sensor
    List<Sensor> l = getSensorList(type);
    return l.isEmpty() ? null : l.get(0);
}

So there is no real difference (and I don't think they can really add one later) between the sensors returned from getDefaultSensor() and from getSensorList().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文