Links

Handling Device Requests

This is Part Two of the Getting Started guide, so if you haven't done Part One, head over there now.
Introduction to Device Requests
Blecon routes requests from Bluetooth devices to your web application. You will see device requests appear as HTTP POST requests containing the JSON data from the device.
Your application will need to respond with an HTTP success code, which will tell the device its request has been successful.
The most common type of device request does not require a specific response, however, depending on the request type and the device, your application may need to respond with data in JSON format - for example, if a device makes a request to ask for configuration.

Requests

The KKM B1 makes requests containing the following data:
  1. 1.
    Motion detection events
  2. 2.
    Current battery voltage

Example POST request

{
"network_headers": {
"message_type": "DEVICE_REQUEST",
"account_id": "24NqMAXYUaDGBNETrFMmvic8jsr",
"network_id": "2AcTSh5Avs3pv0iQYYZxi39i5gS",
"device_id": "63f00d6c-a8ac-4882-81f1-71aae00b5af8",
"device_location": "52.208968553304864, 0.08982994602325013",
"device_model_id": "00010001"
},
"request_data": {
"uptime": 8011,
"records": [
{
"id": 56,
"timestamp": "2022-06-15T15:43:53.811698",
"event_type": "MOTION"
}
],
"battery_mv": 4189,
"battery_condition": "NORMAL"
}
}

Creating a response

For a successful request, you must respond to this device's requests with content-type application/json and an empty JSON document:
{}
It's a good idea to check your network logs to make sure your response is correctly decoded by the Blecon service.

Prepare your application

Introduction
Blecon is designed for web developers like you, so we won't spend much time explaining how to handle a POST request.
The basic flow your application needs to follow for a Blecon request is as follows:
  1. 1.
    Accept HTTP request on a URL you specify
  2. 2.
    Verify authentication of Blecon request
  3. 3.
    Determine the type of device and request
  4. 4.
    Handle the request, storing data as appropriate for your application
  5. 5.
    Send the request response back to Blecon
Blecon supports any cloud, any web framework and any language, so how you perform the above is up to you.
Creating a request handler
For this tutorial, we will use a simple service designed to show incoming requests called https://webhook.site/. It will create a unique URL for handling device requests.
Head over to https://webhook.site/ now and get your own URL. It should look something like this:
https://webhook.site/#!/a14c4803-8523-46a8-a067-350cf2b52n6f

Configure your network

In the previous step, we created a demonstration URL we can use to accept device requests. The next step is to route device requests to your new URL.
First, open the Blecon Console and navigate to the network you created in the previous Connecting a Device Getting Started section.
Now, open the Handlers tab. Here's where we will set your request handler.
Configuring a handler
Click edit then paste the webhook.site URL you created in the previous step.
For now, we will not use any authentication so leave the Headers section blank.
Hit Save and your network is now configured.

Check for requests

You will recall from the previous section of the Getting Started guide that your device was connected and making requests.
Check on your URL handler at webhook.site
If you open the webhook.site URL that was generated for you, you should start to see requests now being routed to the demonstration request handler you created.
Check your network activity
However, we're not quite finished. If you look at your network you should see something like the following:
Empty response error
Note that requests are still showing an error. This is because although requests are reaching the external endpoint, the URL handler is not returning any content. Blecon requires responses to be valid JSON. Let's fix that now.

Configure Response

Head over to webhook.site again and you should see the configuration screen for your test destination. Click Edit in the top right and the change the response as per the following:
Configuring a response
The response content type must be application/json and the content of the body is an empty JSON document.
Hit save, and now let's look at your network activity again. You should see something like the below, indicating that your device is successfully making requests to your example handler.
Successful requests
Last modified 4mo ago