lunedì 8 gennaio 2018

Estimote Eddystone telemetry bridge in Android

Un esempio per ricevere i dati di telemetria dei pacchetti Eddystone (il protocollo di Google per i beacons) e trasmetterli poi ad un server remoto (ho scoperto solo oggi che il nome Eddystone deriva dal faro di Eddystone)



compile 'com.estimote:sdk:1.0.12'

---------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="innocenti.luca.com.eddyacc">

    <uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION">
</uses-permission>
    <uses-permission android:name="android.permisssion.INTERNET">
</uses-permission>


    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

---------------------------------------------


package innocenti.luca.com.eddyacc;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.estimote.coresdk.common.config.EstimoteSDK;
import com.estimote.coresdk.recognition.packets.Eddystone;
import com.estimote.coresdk.recognition.packets.EstimoteTelemetry;
import com.estimote.coresdk.service.BeaconManager;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;





public class MainActivity extends AppCompatActivity {

    private BeaconManager beaconManager;
    private String scanId;




    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EstimoteSDK.initialize(getApplicationContext(),"eddyacc-ev0","f73efb85963579672c048fc92de64039");

        beaconManager = new BeaconManager(this);

        beaconManager.setEddystoneListener(new BeaconManager.EddystoneListener() {
            @Override public void onEddystonesFound(List<Eddystone> eddystones) {
                for (Eddystone e : eddystones){
                    if (e.telemetry != null)
                    {
                        String aa = "http://150.217.73.108/luca/test.php?temperatura="+Double.toString(e.telemetry.temperature);
                        Double myTaskParameters = e.telemetry.temperature;
                        new invia().execute(myTaskParameters);

                        Log.d("Eddy", e.macAddress + " temperature :" + e.telemetry.temperature + " ");
                    }
                }

            }
        });


        beaconManager.setTelemetryListener(new BeaconManager.TelemetryListener() {
            @Override            public void onTelemetriesFound(List<EstimoteTelemetry> telemetries) {
                for (EstimoteTelemetry tlm : telemetries) {
                    Log.d("TELEMETRY", "beaconID: " + tlm.deviceId +
                            ", temperature: " + tlm.temperature + " °C" + tlm.accelerometer.x);
                }
            }
        });
    }




    @Override protected void onStart() {
        super.onStart();
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override            public void onServiceReady() {
                beaconManager.startEddystoneDiscovery();
                beaconManager.startTelemetryDiscovery();
            }
        });
    }

    @Override protected void onStop() {
        super.onStop();
        beaconManager.stopEddystoneDiscovery();

        beaconManager.stopTelemetryDiscovery();
    }

    @Override protected void onDestroy(){
        super.onDestroy();
        beaconManager.disconnect();
    }

    private class invia extends AsyncTask<Double, Void, String> {

        @Override        protected String doInBackground(Double... params) {
            // These two need to be declared outside the try/catch            // so that they can be closed in the finally block.            HttpURLConnection urlConnection = null;
            Double t = params[0];

            try {

                URL url = new URL("http://150.217.73.108/luca/test.php?temperatura="+Double.toString(t));
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                urlConnection.getInputStream();


            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }

            }
            return null;
        }

            @Override            protected void onPostExecute (String s)
            {
                super.onPostExecute(s);
            }

        }
}

Nessun commento:

Posta un commento