Handling Device Requests
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.
The KKM B1 makes requests containing the following data:
- 1.Motion detection events
- 2.Current battery voltage
"device_location": "52.208968553304864, 0.08982994602325013",
For a successful request, you must respond to this device's requests with content-type
application/jsonand an empty JSON document:
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.Accept HTTP request on a URL you specify
- 2.Verify authentication of Blecon request
- 3.Determine the type of device and request
- 4.Handle the request, storing data as appropriate for your application
- 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
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.
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.
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.
Configuring a response
The response content type must be
application/jsonand 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.