Windows传感器开发之使用传感器管理器对象

为了使一个应用可以使用传感器,Microsoft Sensor Framework 需要通过一种方式将对象“绑定”到真实的硬件上。它采取了“即插即用”的方式,使用的是一种称为Sensor Manager Object(传感器管理器对象)的特殊工具。

通过类型询问

一款应用可以寻找特定类型的传感器,如 Gyrometer3D。传感器管理器询问电脑上显示的传感器硬件列表,然后返回绑定至该硬件的匹配对象的集合。虽然传感器集合可能有 0 个、1 个或多个对象,但通常只有 1 个。以下的 C++ 示例代码显示了使用传感器管理器对象的 GetSensorsByType 方法搜索 3 轴陀螺仪,并在传感器集合中返回搜索结果。请注意必须首先为传感器管理器对象创建一个 ::CoCreateInstance()

// Additional includes for sensors
#include <InitGuid.h>
#include <SensorsApi.h>
#include <Sensors.h>
// Create a COM interface to the SensorManager object.
ISensorManager* pSensorManager = NULL;
HRESULT hr = ::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, 
    IID_PPV_ARGS(&pSensorManager));
if (FAILED(hr))
{
    ::MessageBox(NULL, _T("Unable to CoCreateInstance() the SensorManager."), 
        _T("Sensor C++ Sample"), MB_OK | MB_ICONERROR);
    return -1;
}
// Get a collection of all 3-axis Gyros on the computer.
ISensorCollection* pSensorCollection = NULL;
hr = pSensorManager->GetSensorsByType(SENSOR_TYPE_GYROMETER_3D, &pSensorCollection);
if (FAILED(hr))
{
    ::MessageBox(NULL, _T("Unable to find any Gyros on the computer."), 
        _T("Sensor C++ Sample"), MB_OK | MB_ICONERROR);
    return -1;
}
 


通过类别询问

一款应用可以通过类别寻找传感器,比如运动传感器。传感器管理器询问电脑上显示的传感器硬件列表,然后返回绑定至该硬件的运动对象的集合。SensorCollection 中可能有 0 个、1 个或多个对象。在大多数电脑上,集合都具有 2 个运动对象。Accelerometer3D 和 Gyrometer3D。

以下的 C++ 示例代码显示了使用传感器管理器对象的 GetSensorsByCategory 方法搜索运动传感器,并在传感器集合中返回搜索结果。

// Additional includes for sensors
#include <InitGuid.h>
#include <SensorsApi.h>
#include <Sensors.h>
// Create a COM interface to the SensorManager object.
ISensorManager* pSensorManager = NULL;
HRESULT hr = ::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, 
    IID_PPV_ARGS(&pSensorManager));
if (FAILED(hr))
{
    ::MessageBox(NULL, _T("Unable to CoCreateInstance() the SensorManager."), 
        _T("Sensor C++ Sample"), MB_OK | MB_ICONERROR);
    return -1;
}
// Get a collection of all 3-axis Gyros on the computer.
ISensorCollection* pSensorCollection = NULL;
hr = pSensorManager->GetSensorsByCategory(SENSOR_CATEGORY_MOTION, &pSensorCollection);
if (FAILED(hr))
{
    ::MessageBox(NULL, _T("Unable to find any Motion sensors on the computer."), 
        _T("Sensor C++ Sample"), MB_OK | MB_ICONERROR);
    return -1;
}
 

通过“全部”类别询问

实际上,一款应用如果能在电脑上同时寻找所有的传感器将会实现最高的效率。传感器管理器询问电脑上显示的传感器硬件列表,然后返回绑定至该硬件的所有对象的集合。传感器集合中可能有 0 个、1 个或多个对象。在大多数电脑上,集合都具有 7 个或以上的对象。

由于 C++ 不能进行 GetAllSensors 调用 ,因此您必须使用GetSensorsByCategory(SENSOR_CATEGORY_ALL, …),如以下示例代码所示。

C++ does not have a GetAllSensors call, so you must use GetSensorsByCategory(SENSOR_CATEGORY_ALL, …) instead as shown in the sample code below.
// Additional includes for sensors
#include <InitGuid.h>
#include <SensorsApi.h>
#include <Sensors.h>
// Create a COM interface to the SensorManager object.
ISensorManager* pSensorManager = NULL;
HRESULT hr = ::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, 
    IID_PPV_ARGS(&pSensorManager));
if (FAILED(hr))
{
    ::MessageBox(NULL, _T("Unable to CoCreateInstance() the SensorManager."), 
        _T("Sensor C++ Sample"), MB_OK | MB_ICONERROR);
    return -1;
}
// Get a collection of all 3sensors on the computer.
ISensorCollection* pSensorCollection = NULL;
hr = pSensorManager->GetSensorsByCategory(SENSOR_CATEGORY_ALL, &pSensorCollection);
if (FAILED(hr))
{
    ::MessageBox(NULL, _T("Unable to find any sensors on the computer."), 
        _T("Sensor C++ Sample"), MB_OK | MB_ICONERROR);
    return -1;
}
 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值