Export (0) Print
Expand All

Handling Provider Status Changes

This topic demonstrates how to handle status changes for the location report provider. The status reflects the state of the location sensors, which indicates the current ability of the Location API to provide new reports of a specified type. You can either handle status change events as they arrive asynchronously, or check the status of a location report synchronously.

Checking Report Status Using Events

You can receive report status change events asynchronously by implementing the ILocationEvents::OnStatusChanged callback. To receive location events, you must create a callback object that implements ILocationEvents, and register for events, as shown in the following example code.

This example demonstrates creating a callback object for location events. See Responding to Location Report Events for a full implementation of the callback class named CLocationEvents.



HRESULT hr = S_OK;

// This is the main Location interface
CComPtr<ILocation> spLocation; 

// This is our callback object for location reports 
CComObject<CLocationEvents>* pLocationEvents = NULL; 

// Create the Location object
hr = spLocation.CoCreateInstance(CLSID_Location);  

if (SUCCEEDED(hr))
{
  // Create the callback object
  hr = CComObject<CLocationEvents>::CreateInstance(&pLocationEvents); 

  if (NULL != pLocationEvents)
  {
      pLocationEvents->AddRef();
  }
}

This example demonstrates how to register for events. You should register for events to get report status change events as well as location change events.



// The array of report types to subscribe to
IID REPORT_TYPES[] = { IID_ILatLongReport, IID_ICivicAddressReport };

// Tell the Location API that we want to register for reports 
 for (DWORD index = 0; index < ARRAYSIZE(REPORT_TYPES); index++)
 {
     hr = spLocation->RegisterForReport(pLocationEvents,
        REPORT_TYPES[index], // Report type to register for.
        0);                  // 0 means that no minimum interval 
                             // between events is specified.
 }

After you create a callback object, the code in your OnStatusChanged callback function will be invoked on a report status change. The following example code shows an OnStatusChanged callback implementation.


// This is called when the status of a report type changes.
// The LOCATION_REPORT_STATUS enumeration is defined 
// in locationapi.h in the SDK
STDMETHODIMP CLocationEvents::OnStatusChanged(
    REFIID reportType,
    LOCATION_REPORT_STATUS status)
{
    if (IID_ILatLongReport == reportType)
    {
        switch (status)
        {
        case REPORT_NOT_SUPPORTED:
            printf("\nNo devices detected.\n");
            break;
        case REPORT_ERROR:
            printf("\nReport error.\n");
            break;
        case REPORT_ACCESS_DENIED:
            printf("\nAccess denied to reports.\n");
            break;
        case REPORT_INITIALIZING:
            printf("\nReport is initializing.\n");
            break;
        case REPORT_RUNNING:
            printf("\nRunning.\n");
            break;
        }
    }
    return S_OK;
}

Checking Report Status Synchronously

As an alternative to checking report status using events, you can call the ILocation::GetReportStatus method periodically to check the state of the location sensors.

Note  See the GetReportStatus topic for known issues.

The following code calls GetReportStatus and prints a message based on the status returned in the status parameter. If the status is not REPORT_RUNNING, this sample also prints a message indicating that the data that GetReport returns is cached data.


    // Get the status of this report type
    if (SUCCEEDED(spLocation->GetReportStatus(IID_ILatLongReport, &status))) 
    {
        bool fIsNotRunning = true;
        switch (status) 
        {
        case REPORT_RUNNING:
            // If the status for the current report is running,
            // then do not print any additional message.
            // Otherwise, print a message indicating that reports may contain cached data.
            fIsNotRunning = false;
            break;
        case REPORT_NOT_SUPPORTED:
            wprintf(L"\nThere is no sensor installed for this report type.\n");
            break;
        case REPORT_ERROR:
            wprintf(L"\nReport error.\n");
            break;
        case REPORT_ACCESS_DENIED:
            wprintf(L"\nAccess denied to reports.\n");
            break;
        case REPORT_INITIALIZING:
            wprintf(L"\nReport is initializing.\n");
            break;
        }

        if (true == fIsNotRunning)
        {
            wprintf(L"Location reports returned from GetReport contain cached data.\n");
        }
    }


Related topics

Responding to Location Report Events

 

 

Community Additions

ADD
Show:
© 2014 Microsoft