Android Open Source - trifork-ibeacon-demo Beacon Data Fragment






From Project

Back to project page trifork-ibeacon-demo.

License

The source code is released under:

Apache License

If you think the Android project trifork-ibeacon-demo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.trifork.ibeacon.ui;
//from ww w  .  ja v  a2  s .  co  m
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.InjectView;
import com.echo.holographlibrary.*;
import com.squareup.otto.Subscribe;
import com.trifork.ibeacon.BaseFragment;
import com.trifork.ibeacon.R;
import com.trifork.ibeacon.eventbus.RangeScanCompleteEvent;
import com.trifork.ibeacon.eventbus.NewBeaconSelectedEvent;
import com.trifork.ibeacon.eventbus.RequestBeaconScanEvent;
import com.trifork.ibeacon.eventbus.RequestFullScanEvent;
import com.trifork.ibeacon.eventbus.StopScanEvent;
import com.trifork.ibeacon.util.*;
import com.trifork.ibeacon.util.Utils;

import org.altbeacon.beacon.Beacon;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;

import static com.trifork.ibeacon.R.*;

public class BeaconDataFragment extends BaseFragment {

    @InjectView(id.value_uuid) TextView uuidView;
    @InjectView(id.value_major) TextView majorView;
    @InjectView(id.value_minor) TextView minorView;
    @InjectView(id.value_txpower) TextView txPowerView;
    @InjectView(id.value_lastreading) TextView lastReadingView;
    @InjectView(id.value_timestamp) TextView timestampView;
    @InjectView(id.graph_rssi) LineGraph rssiGraphView;
    @InjectView(id.graph_distance) BarGraph distanceGraphView;

    private SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss.S");
    private CircularBuffer<Integer> readings = new CircularBuffer<>(100);
    private CircularBuffer<com.trifork.ibeacon.util.Utils.Proximity> distances = new CircularBuffer<>(100);

    public static BeaconDataFragment newInstance() {
        Bundle args = new Bundle();
        BeaconDataFragment fragment = new BeaconDataFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(layout.fragment_ibeacondata, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        resetViews();
    }

    private void resetViews() {
        uuidView.setText(string.empty_placeholder);
        majorView.setText(string.empty_placeholder);
        minorView.setText(string.empty_placeholder);
        txPowerView.setText(string.empty_placeholder);
        lastReadingView.setText(string.empty_placeholder);
        timestampView.setText(string.empty_placeholder);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isResumed()) {
            startScan();
        }
    }

    private void startScan() {
        bus.post(new RequestBeaconScanEvent(persistentState.getSelectedRegion()));
    }

    @Subscribe
    public void beaconFound(RangeScanCompleteEvent event) {
        Beacon beacon = event.getBeacons().get(0);
        Calendar time = event.getTimestamp();
        readings.add(beacon.getRssi());
        distances.add(Utils.proximityFromDistance(beacon.getDistance()));

        // Update beacon info and reading
        if (uuidView == null) return;
        uuidView.setText(beacon.getId1().toString());
        majorView.setText(beacon.getId2().toString());
        minorView.setText(beacon.getId3().toString());
        txPowerView.setText(String.format("%s dBm", beacon.getTxPower()));
        lastReadingView.setText(String.format("%s dBm (%s m)", beacon.getRssi(), Utils.formatRange(beacon.getDistance())));
        timestampView.setText(timeFormatter.format(time.getTime()));

        updateRSSIGraph();
        updateDistanceGraph();
    }

    @Subscribe
    public void onNewBeaconSelected(NewBeaconSelectedEvent event) {
        readings.clear();
        distances.clear();
    }

    private void updateRSSIGraph() {
        rssiGraphView.removeAllLines();

        Line l = new Line();
        l.setShowingPoints(false);
        l.setUsingDips(false);
        l.setColor(getResources().getColor(color.yellow));
        Iterator<Integer> iterator = readings.iterator();
        int i  = 0;
        while(iterator.hasNext()) {
            int reading = iterator.next();
            LinePoint p = new LinePoint(i, reading * - 1);
            l.addPoint(p);
            i++;
        }

        rssiGraphView.setGraphBackgroundColor(getResources().getColor(R.color.graph_background));
        rssiGraphView.addLine(l);
        rssiGraphView.setRangeY(0, 100);
        rssiGraphView.setRangeX(0, 100);
        rssiGraphView.setClickable(false);
        rssiGraphView.setLineToFill(0);
    }

    private void updateDistanceGraph() {
        ArrayList<Bar> points = new ArrayList<>();
        Iterator<Utils.Proximity> iterator = distances.iterator();
        for (int i = 0; i < 100; i++) {
            Bar b = new Bar();
            int value;
            int color;
            Utils.Proximity distance = (iterator.hasNext()) ? iterator.next() : Utils.Proximity.UNKNOWN;
            switch(distance) {
                case IMMEDIATE:
                    value = 1;
                    color = getResources().getColor(R.color.green);
                    break;
                case NEAR:
                    value = 2;
                    color = getResources().getColor(R.color.yellow);
                    break;
                case FAR:
                    value = 3;
                    color = getResources().getColor(R.color.red);
                    break;
                default:
                    value = 0;
                    color = Color.TRANSPARENT;
                    break;
            }
            b.setValue(value);
            b.setColor(color);
            b.setName("");
            points.add(b);
        }

        distanceGraphView.setMaxValue(3);
        distanceGraphView.setClickable(false);
        distanceGraphView.setBars(points);
        distanceGraphView.setShowBarText(false);
    }
}




Java Source Code List

com.echo.holographlibrary.BarGraph.java
com.echo.holographlibrary.Bar.java
com.echo.holographlibrary.LineGraph.java
com.echo.holographlibrary.LinePoint.java
com.echo.holographlibrary.Line.java
com.echo.holographlibrary.PieGraph.java
com.echo.holographlibrary.PieSlice.java
com.echo.holographlibrary.Utils.java
com.trifork.ibeacon.ApplicationContext.java
com.trifork.ibeacon.ApplicationModule.java
com.trifork.ibeacon.BaseActivity.java
com.trifork.ibeacon.BaseApplication.java
com.trifork.ibeacon.BaseFragment.java
com.trifork.ibeacon.BootBroadcastReceiver.java
com.trifork.ibeacon.MainActivity.java
com.trifork.ibeacon.database.Dao.java
com.trifork.ibeacon.database.Database.java
com.trifork.ibeacon.database.RegionHistoryCursorLoader.java
com.trifork.ibeacon.database.RegionHistoryEntry.java
com.trifork.ibeacon.detectors.BeaconController.java
com.trifork.ibeacon.detectors.IBeaconParser.java
com.trifork.ibeacon.eventbus.FullScanCompleteEvent.java
com.trifork.ibeacon.eventbus.NewBeaconSelectedEvent.java
com.trifork.ibeacon.eventbus.OttoEvent.java
com.trifork.ibeacon.eventbus.RangeScanCompleteEvent.java
com.trifork.ibeacon.eventbus.RequestBeaconScanEvent.java
com.trifork.ibeacon.eventbus.RequestBeaconTransmit.java
com.trifork.ibeacon.eventbus.RequestFullScanEvent.java
com.trifork.ibeacon.eventbus.StopFullScanEvent.java
com.trifork.ibeacon.eventbus.StopScanEvent.java
com.trifork.ibeacon.eventbus.StopTransmitEvent.java
com.trifork.ibeacon.ui.BeaconDataFragment.java
com.trifork.ibeacon.ui.LocationFragment.java
com.trifork.ibeacon.ui.NotificationFragment.java
com.trifork.ibeacon.ui.RangingFragment.java
com.trifork.ibeacon.ui.RegionLogFragment.java
com.trifork.ibeacon.ui.ScanFragment.java
com.trifork.ibeacon.util.CircularBuffer.java
com.trifork.ibeacon.util.PersistentState.java
com.trifork.ibeacon.util.Utils.java
com.trifork.ibeacon.widgets.BeaconView.java
com.trifork.ibeacon.widgets.LocationTrackerView.java