Skip to content

Latest commit

 

History

History
110 lines (78 loc) · 3.4 KB

requesting-user-permissions.md

File metadata and controls

110 lines (78 loc) · 3.4 KB
description ms.assetid title ms.topic ms.date
This topic describes how to request permissions from the user to use sensors. For background information about permissions in the Sensor API, see Managing User Permissions.
e43ad497-86f1-4804-a67a-0aeb56b80d7f
Requesting User Permissions
concept-article
05/31/2018

Requesting User Permissions

This topic describes how to request permissions from the user to use sensors. For background information about permissions in the Sensor API, see Managing User Permissions.

The following examples illustrate some of the common scenarios where you can choose to request user permissions.

The following example code simply requests permissions for all sensors retrieved from the sensor manager, by type, using an asynchronous method call. The platform will open a dialog box to prompt the user only to enable sensors that are not already enabled. To determine whether the user enabled any sensors in this case, you must handle the ISensorEvents::OnStateChanged event.

// Get the sensor collection.
hr = pSensorManager->GetSensorsByType(SAMPLE_SENSOR_TYPE_TIME, &pSensorColl);

if(SUCCEEDED(hr))
{
    // Request permissions for all sensors
    // in the collection.
    hr = pSensorManager->RequestPermissions(0, pSensorColl, FALSE);
}

You can choose to test the sensor state synchronously before attempting to retrieve data. The following example code demonstrates this technique.

if(SUCCEEDED(hr))
{
   // Check the current sensor state.
   SensorState state = SENSOR_STATE_NOT_AVAILABLE;

   hr = pSensor->GetState(&state);

   if(SUCCEEDED(hr))
   {
       if(state == SENSOR_STATE_ACCESS_DENIED)
       {
           wprintf_s(L"\nSensor not enabled, requesting permissions...\n");
           hr = pSensorManager->RequestPermissions(0, pSensorColl, TRUE);

           if(hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED) ||
              hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) 
           {
               wprintf_s(L"\nYou have previously denied access to this sensor.\n");
               wprintf_s(L"Please use the Location and Other Sensors control panel\n");
               wprintf_s(L"to enable the WDK Time Sensor and run this program again.\n");
           }
       }
   }
}

if(SUCCEEDED(hr))
{
    // Get the data report.
    hr = pSensor->GetData(&pReport);
}

The following example code prompts the user for sensor permissions if an attempt to retrieve a data report from a particular sensor fails.

if(SUCCEEDED(hr))
{
    // Get the data report.
    hr = pSensor->GetData(&pReport);

    if(E_ACCESSDENIED == hr)
    {
       wprintf_s(L"\nSensor not enabled, requesting permissions...\n");
       hr = pSensorManager->RequestPermissions(0, pSensorColl, TRUE);

       if(hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED) ||
          hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) 
       {
           wprintf_s(L"\nYou have previously denied access to this sensor.\n");
           wprintf_s(L"Please use the Location and Other Sensors control panel\n");
           wprintf_s(L"to enable the WDK Time Sensor and run this program again.\n");
       }
    }
}

Related topics

ISensorManager

Managing User Permissions