Managing User Permissions
The Sensor API provides a method you can use to prompt the user for permissions to use a sensor or collection of sensors.
Because sensors can reveal sensitive information, Windows requires that users enable sensors before your program can access any data.
You may want to request permission when you want to use sensors for which the current SensorState is SENSOR_STATE_ACCESS_DENIED.
To request permissions, call the ISensorManager::RequestPermissions method. When you call this method, Windows opens the Enable sensors dialog box to prompt the user to enable the sensors you requested. This dialog box provides the user with the names of the sensors you requested. The user can choose one of the following options:
- Enable these sensors.
- Don't enable these sensors.
- Open Control Panel for more options.
If a user chooses Don't enable these sensors, Windows will not show the Enable sensors dialog box again for those particular sensors, even if your program calls RequestPermissions. If the user chooses any other option, Windows will allow the dialog box to be displayed when requested. If your call to RequestPermissions contains some sensors that the user has previously chosen to keep disabled, the Sensor API will remove these sensors from the list of sensors the user sees.
Modal or Modeless Behavior
The RequestPermissions method takes a Boolean argument that determines whether the Enable sensors dialog box is displayed as a modal or modeless window. This setting also affects whether the behavior of the dialog box return code is synchronous or asynchronous.
When modal, the dialog box has exclusive focus among application windows until the user chooses an option, and the HRESULT return code from your call to RequestPermissions indicates the user choice. When modeless, the dialog box does not have exclusive focus and your call to RequestPermissions returns immediately. In this case, the return code indicates whether the method succeeded, but cannot be used to ascertain the user's choice. You can then determine which sensors have been enabled by handling the OnStateChanged event and checking each sensor for SENSOR_STATE_READY.
For more information about return codes, see the RequestPermissions reference page.
Best Practice: Avoid Multiple Modeless Calls to RequestPermissions
Repeated modeless calls to RequestPermissions will display multiple instances of the Enable these sensors dialog box, and can potentially flood the screen with dialog boxes, resulting in a poor user experience. If you think that after your first call to RequestPermissions, other location sensors might be installed, requiring another call to RequestPermissions, you should either call RequestPermissions modally, or wait until all location sensors are installed to make a modeless call.