다음을 통해 공유


나중에 다시 사용하기 위해 마지막으로 사용한 장치를 저장하는 방법(HTML)

[ 이 문서는 Windows 런타임 앱을 작성하는 Windows에서 8.x 및 Windows Phone 8.x 개발자를 대상으로 합니다. Windows 10용으로 개발하는 경우에는 최신 설명서를 참조하세요.]

이 항목에서는 DeviceInformation.CreateFromIdAsync를 호출하여 저장된 장치 ID에서 장치 정보를 가져오는 방법을 설명합니다.

알아야 할 사항

기술

  • Windows Runtime

사전 요구 사항

JavaScript 및 HTML에 대해 잘 알고 있어야 합니다.

지침

createFromIdAsync에 장치 인터페이스 ID 전달

장치를 사용해야 하는 앱을 처음으로 실행할 때 사용 가능한 장치가 열거되면 열거 결과에서 DeviceInformation 개체를 선택하고 DeviceInformation.Id 속성을 장치에 액세스하는 Windows 런타임 API에 전달합니다. 앱에서 이 장치 ID를 저장하도록 하는 것이 좋을 수 있습니다. 그러면 다음에 앱이 실행될 때 다른 열거를 시작하기 전에 마지막으로 사용된 장치를 사용할 수 있는지 여부를 확인하여 앱이 보다 빠르게 시작할 수 있습니다.

이 함수의 savedDeviceID 매개 변수는 마지막으로 사용된 장치의 ID입니다. 다음 코드는 createFromIdAsync를 호출하여 DeviceInformation 개체를 만들고 성공 및 오류 결과를 처리하는 두 개의 익명 함수를 정의합니다. 메서드가 성공하면 앱은 DeviceInformation 개체를 사용하여 장치 인터페이스를 사용할 수 있는지 여부를 확인한 다음 장치를 사용하는 API에 전달합니다.

function getLastUsedDeviceInfo(savedDeviceID) {

    // Create a DeviceInformation object from the ID.
    var Enum = Windows.Devices.Enumeration;
    var DevInf = Enum.DeviceInformation;
    DevInf.createFromIdAsync(savedDeviceId).then(
        function(devinfo) {             
            printMessage("Found last-used device. Name: " + devinfo.name);
            if (devinfo.isEnabled) {
                // Add code to pass devinfo.id to an appropriate API.
                // For instance, if devinfo is a camera, use the id property 
                // to select the camera as the one the Camera Capture API
                // should use, by setting the VideoDeviceId property of 
                // Windows.Media.Capture.MediaCaptureInitializationSettings.
            } else { 
                // Handle the case of the device not being enabled.
            }
        },
        function (e) {
            displayError("Failed to create DeviceInformation: " + e.message);
            // Since the last-used device wasn't found, add code here
            // to call FindAllAsync or CreateWatcher to find other devices.
        });
}