POST Create Beacon – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Create Beacon client demonstrates adding a Beacon to your Bluedot backend using 'request' node module.
 */
var request = require('request');
var beaconData = {
    security: {
        /* The apiKey is generated when you create an application. */
        apiKey : 'c2c8211f-796f-4eda-b6ce-05467b5263a9',
        /* This key is generated by Bluedot Point Access UI when your account is created. It is also available
         * on the PointAccess interface in the Edit Profile section. */
        customerApiKey: '86577370-7b91-11e4-bcb7-a0481cdc3311'
    },
    content: {
        beacon : {
            name: "Cafe Entrance",
            proximityUUID: "f7826da6-4fa2-4e98-8024-bc5b71e0898f",
            longitude: "123.34455",
            latitude: "47.777888",
            major: 12,
            minor: 13,
            type: "IOS"
        }
    }
};
var options = {
    uri: 'https://api.bluedotinnovation.com/1/beacons',
    method: 'POST',
    json: beaconData
};
request ( options,
    function (error, response, body) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.beacon;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.bluedotinnovation.common.BDCommon;
/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Add Beacon client demonstrates adding a Beacon to your Bluedot backend using Apache HTTP client and JSON Simple libraries.
 */
public class AddBeacon extends BDCommon {
    private static String bdCustomerApiKey    = "7cd1ea80-d40e-11e4-84cb-b8cb3a6b879d"; //This key is generated by Bluedot Point Access UI when your account is created
    private static String bdApplicationApiKey = "afc346a0-de5e-11e4-af33-b8cb3a6b879d"; //This apiKey is generated when you create an application
    private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/beacons";
     
    /**
     * @param args
     * @throws IOException
     * @throws ClientProtocolException
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public static void main(String[] args) throws ParseException, ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
         
        JSONParser parser = new JSONParser();
        JSONObject bdBothBeaconJSONObject = (JSONObject) parser.parse(getJsonBeacon()); //add an Both type Beacon
         
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdBothBeaconJSONObject.toJSONString()));
         
        HttpResponse response = httpRestClient.execute(postRequest);
         
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Beacon was successfully created");
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json result
            JSONObject jsonResult   = (JSONObject)  parser.parse(resultString);
            System.out.println(jsonResult);
        } else {
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json error result
            System.out.println(resultString);
        }
    }
    private static String getJsonBeacon() {
         
        String beaconJson = 
                "{" +
                   "\"security\": {" +
                    "\"apiKey\":"+ "\"" + bdApplicationApiKey +"\","+
                    "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" "+
                "}," +
                "\"content\": {" +
                    "\"beacon\": {" +
                       "\"name\": \"Bluedot building\"," +
                       "\"proximityUUID\": \"f7826da6-4fa2-4e98-8024-bc4b71e0894e\"," +
                       "\"longitude\": \"123.34455\"," +
                       "\"latitude\": \"47.777888\"," +
                       "\"type\":  \"Both\"," +
                       "\"major\": 12," +
                       "\"minor\": 13," +
                       "\"macAddress\": \"01:17:C5:31:84:21\"," +
                       "\"txPower\": -77," +
                       "\"description\": \"Sample description\"" +
                    "}"+
                "}"+
             "}";
                 
        return beaconJson;
    }
}
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Net;
using System.Web.Script.Serialization;
using System.IO;
using System.Security.Cryptography.X509Certificates;
/**
* @author Bluedot Innovation
* Copyright (c) 2016 Bluedot Innovation. All rights reserved.
* Add Beacon client demonstrates adding a Beacon to to an existing the customer's account using net http web api library
*
*/
namespace BluedotPublicApiClient.beaconclient
{
    public class BDAddBeacon
    {
        private static String bdApplicationApiKey = "dc99ae20-9192-11e5-8721-0646bf53d23f"; //This apiKey is generated when you create an application
        private static String bdCustomerApiKey    = "7cd1ea80-d40e-11e4-84cb-b8ca3a6b879d"; //This key is generated by Bluedot Point Access UI when your account is created
        private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/beacons";
 
        public void create()
        {
            WebRequestHandler handler = new WebRequestHandler();
            X509Certificate2 certificate = new X509Certificate2();
            handler.ClientCertificates.Add(certificate);
            HttpClient httpRestClient = new HttpClient(handler);
            //specify to use TLS 1.2 as default connection
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            httpRestClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpContent jsonBeaconContent = new StringContent(getJsonBeacon());
            jsonBeaconContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage serverResponse = httpRestClient.PostAsync(new Uri(bdRestUrl), jsonBeaconContent).Result;
            if (serverResponse.IsSuccessStatusCode)
            {
                var result = serverResponse.Content.ReadAsStringAsync().Result;
                Console.WriteLine("{0}", result.ToString());
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)serverResponse.StatusCode, serverResponse.Content.ReadAsStringAsync().Result);
            }
        }
        /*JSON to create a Beacon*/
        private static String getJsonBeacon()
        {
              return "{" +
                "\"security\": {" +
                 "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                 "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
             "}," +
             "\"content\": {" +
                "\"beacon\": {" +
                    "\"name\": \"Bluedot building \"," +
                    "\"proximityUUUID\": \"f7826da6-4fa2-4e98-8024-bc5b71e0893e\"," +
                    "\"longitude\": \"123.34455\"," +
                    "\"latitude\": \"47.777888\"," +
                    "\"type\": \"Both\"," +
                    "\"major\": 12," +
                    "\"minor\": 13," +
                    "\"txPower\": -77," +
                    "\"macAddress\": \"01:17:C5:31:84:21\"," +
                    "\"description\": \"Sample Description\"" +
                "}" +
            "}" +
          "}";
        }
    }
}
Created by Bluedot DevOps on January 22, 2018

Start the discussion