다음을 통해 공유


사용자 권한 요청

이 항목에서는 센서를 사용하도록 사용자에게 권한을 요청하는 방법을 설명합니다. 센서 API의 권한에 대한 배경 정보는 사용자 권한 관리를 참조 하세요.

다음 예제에서는 사용자 권한을 요청하도록 선택할 수 있는 몇 가지 일반적인 시나리오를 보여 줍니다.

다음 예제 코드는 비동기 메서드 호출을 사용하여 센서 관리자에서 검색된 모든 센서에 대한 권한을 유형별로 요청합니다. 플랫폼이 대화 상자를 열어 사용자에게 아직 사용하도록 설정되지 않은 센서만 사용하도록 설정하라는 메시지를 표시합니다. 이 경우 사용자가 센서를 사용하도록 설정했는지 여부를 확인하려면 ISensorEvents::OnStateChanged 이벤트를 처리해야 합니다.

// 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);
}

데이터를 검색하기 전에 센서 상태를 동기적으로 테스트하도록 선택할 수 있습니다. 다음 예제 코드에서는 이 기술을 보여 줍니다.

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);
}

다음 예제 코드는 특정 센서에서 데이터 보고서를 검색하는 시도가 실패할 경우 사용자에게 센서 권한을 묻는 메시지를 표시합니다.

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");
       }
    }
}

ISensorManager

사용자 권한 관리