How to send Telemetry Device information from WP 8.1 background task.
Sending real time temperature details from background task
We developed a Windows Phone based IoT solution which synced with Bluetooth Low Energy (BLE) Sensors to monitor container temperatures to give real-time data. This helped us monitor containers’ data for predictive analysis.
In Windows Phone application you can use Background Task, This is helpful in gathering information in real time without affecting the foreground app is what everyone will be looking for.
What is Windows Phone Background?
Background tasks are lightweight classes that OS runs in the background. You can use background tasks to provide functionality when your app is suspended or not running. You can also use background tasks for real-time communication.
We started capturing current sensor readings using a background task. Further to this, we can send this temperature (event) data from the background task using HttpClient.
Why HttpClient Object?
While working with Background Task we found that we cannot interact with Event hub directly using Event Hub API. Due to unavailability of Service bus for Event hub API in background task process, we bridged this data using HTTPClient for transmitting temperature data.
Here is the code snippet for sending Events:
In this we use following local variables:
- string uri = Service bus URL;
- string keyName = Access Policy name
- string key = Shared Access Key;
Using the above information we created token for Authorization. We created a new Helper Class which is dedicated to handle Event related activity as a generic class. In this class, we perform operations such as ‘Create Token’, ‘Create HttpClient’ object, ‘Serialize JSON object’ and ‘Post Event’ to Event Hub.
Below is the code/function for creating Token and Post Event using HttpClient object.
Creating token:
After getting the token, now we are ready to send the information. Now we can send the temperature Event information with the help of following code.
Create HttpClient object and post information:
After sending the data successfully you can view processes on Event Hub Dashboard in Azure portal.
In case, you have any queries or you would like to know more about Event Hub or IoT solutions then please reach out to me.
Technology lead | Full Stack | Net Web Developer
8yHow do I Use IoT Hub to send telemetry data through WP 8.1 .
Microsoft Cloud AI partner APAC, Middle East, Panelist, Keynote speaker, MCT, Trainer, Digital Strategic consultant, Certified SAFe® 5 Product Owner
9ygood amount of observation. Nice article