Skip to content

Instantly share code, notes, and snippets.

@kdravolin
Created October 5, 2015 13:50
Show Gist options
  • Save kdravolin/6c2bdd4bf031fc94dcb8 to your computer and use it in GitHub Desktop.
Save kdravolin/6c2bdd4bf031fc94dcb8 to your computer and use it in GitHub Desktop.
Example of using Windows Sensor API with location sensors. See (https://msdn.microsoft.com/en-us/library/windows/desktop/dd318953(v=vs.85).aspx)
#include "stdafx.h"
#include <sensorsapi.h>
#include <sensors.h>
#pragma comment(lib,"sensorsapi.lib")
int main()
{
ISensorManager* sensorManager = 0;
HRESULT hr = 0;
CoInitializeEx(0, COINIT_APARTMENTTHREADED);
hr = CoCreateInstance(CLSID_SensorManager, 0, CLSCTX_ALL, __uuidof(ISensorManager), (void**)&sensorManager);
if (hr != 0 || !sensorManager)
{
printf("SensorManager is not initializated.\n");
return -1;
}
printf("SensorManager is initializated.\n");
ISensorCollection* sensorList = 0;
hr = sensorManager->GetSensorsByCategory(SENSOR_CATEGORY_LOCATION, &sensorList);
if (hr != 0 || !sensorList)
{
printf("Location sensors not exist.\n");
return -1;
}
hr = sensorManager->RequestPermissions(0, sensorList, TRUE);
if (hr != 0)
{
printf("No permission for using location sensors.\n");
return -1;
}
ULONG sensorsCount = 0;
hr = sensorList->GetCount(&sensorsCount);
if (hr != 0 || !sensorsCount)
{
printf("Location sensors not exist.\n");
sensorList->Release();
return -1;
}
printf("%i sensors found:\n", sensorsCount);
for (unsigned int i = 0; i < sensorsCount; i++)
{
printf("\n");
ISensor* sensor = 0;
hr = sensorList->GetAt(i, &sensor);
if (!sensor)
continue;
BSTR name = 0;
hr = sensor->GetFriendlyName(&name);
if (hr != 0 || !name)
{
continue;
}
wprintf(L"%i) %s\n", i, name);
printf("State: ");
SensorState state;
hr = sensor->GetState(&state);
if (hr != 0)
{
continue;
}
switch (state)
{
case SENSOR_STATE_READY || SENSOR_STATE_MIN:
wprintf(L"Ready");
break;
case SENSOR_STATE_NOT_AVAILABLE:
wprintf(L"Not Available");
break;
case SENSOR_STATE_INITIALIZING:
wprintf(L"Initializing");
break;
case SENSOR_STATE_ERROR:
wprintf(L"Error");
break;
case SENSOR_STATE_ACCESS_DENIED:
wprintf(L"Access Denied");
break;
case SENSOR_STATE_NO_DATA:
wprintf(L"No Data");
break;
default:
wprintf(L"Unknown");
break;
}
printf("\n");
if (state != SENSOR_STATE_READY && state != SENSOR_STATE_MIN) {
continue;
}
ISensorDataReport* sensorData = 0;
hr = sensor->GetData(&sensorData);
if (hr != 0 || !sensorData)
{
continue;
}
IPortableDeviceKeyCollection* keyList = 0;
hr = sensor->GetSupportedDataFields(&keyList);
if (hr != 0 || !keyList)
{
continue;
}
ULONG keysCount = 0;
keyList->GetCount(&keysCount);
if (hr != 0 || !keysCount)
{
continue;
}
for (unsigned int j = 0; j < keysCount; j++)
{
PROPERTYKEY sensorDataKey;
keyList->GetAt(j, &sensorDataKey);
if (sensorDataKey.fmtid != SENSOR_DATA_TYPE_LOCATION_GUID)
{
continue;
}
// Get the value
PROPVARIANT sensorDataValue;
sensorData->GetSensorValue(sensorDataKey, &sensorDataValue);
if (sensorDataKey == SENSOR_DATA_TYPE_LATITUDE_DEGREES)
{
printf("Latitude");
}
else if (sensorDataKey == SENSOR_DATA_TYPE_LONGITUDE_DEGREES)
{
printf("Longitude");
}
else if (sensorDataKey == SENSOR_DATA_TYPE_ALTITUDE_ELLIPSOID_METERS)
{
printf("Altitude");
}
else if (sensorDataKey == SENSOR_DATA_TYPE_SPEED_KNOTS)
{
printf("Speed");
}
else if (sensorDataKey == SENSOR_DATA_TYPE_ERROR_RADIUS_METERS)
{
printf("Accuracy");
}
else if (sensorDataKey == SENSOR_DATA_TYPE_MAGNETIC_HEADING_DEGREES)
{
printf("Bearing");
}
else
{
continue;
}
printf(" : %f\n", sensorDataValue.dblVal);
}
keyList->Release();
}
system("PAUSE");
return 0;
}
@beargi
Copy link

beargi commented Aug 12, 2017

Hey, do you have an idea why my sensor (Sierra Wireless Location Sensor) is detected properly but sending absolutely no data when accessed via your example?
Cheers,
Philipp

@Salunkhesneh
Copy link

Hey , Using the same code to read data from the "u-bolx 7 GPS/GNSS Location Sensor " . But everytime I am getting the sensor state as initialising. So is it the problem with sensor initialisation ? If yes how to initialise this sensor ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment