Android Open Source - trifork-ibeacon-demo Ranging 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;
//  www .  j  av  a  2 s .  c o m

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.InjectView;
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.RequestBeaconScanEvent;
import com.trifork.ibeacon.util.Utils;

import org.altbeacon.beacon.Beacon;

public class RangingFragment extends BaseFragment {

    private static final int MIN_PAUSE_MS = 125; // Minimum pause between beeps at minimum distance
    private static final int MAX_PAUSE_MS = 2000; // Pause between beeps at maximum distance

    @InjectView(R.id.range) TextView rangeView;
    @InjectView(R.id.proximity) TextView proximityView;

    private Handler handler = new Handler();
    private ToneGenerator toneG;
    private int pause = MAX_PAUSE_MS;
    private Runnable toneRunner = new Runnable() {
        @Override
        public void run() {
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 125);
            handler.postDelayed(this, pause);
        }
    };

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
    }

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rangeView.setText(R.string.empty_placeholder);
        proximityView.setText(Utils.Proximity.UNKNOWN.toString());
    }

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

    @Override
    public void onPause() {
        super.onPause();
        handler.removeCallbacks(toneRunner);
    }

    @Subscribe
    public void onBeaconDataReceived(RangeScanCompleteEvent event) {
        Beacon beacon = event.getBeacons().get(0);
        double distanceM = beacon.getDistance();
        Utils.Proximity proximity = Utils.proximityFromDistance(beacon.getDistance());

        rangeView.setText(com.trifork.ibeacon.util.Utils.formatRange(distanceM) + "m");
        proximityView.setText(proximity.toString());

        // Calculate new pause based on current RSSI
        // Convert to positive numbers to not make my head hurt.
        float txPower = -beacon.getTxPower();
        float rssi = -beacon.getRssi();
        if (rssi <= txPower) {
            pause = MIN_PAUSE_MS;
        } else if (rssi >= 100) {
            pause = MAX_PAUSE_MS;
        } else {
            float factor = ((rssi - txPower)/(100 - txPower));
            pause = (int) (MIN_PAUSE_MS + (MAX_PAUSE_MS - MIN_PAUSE_MS) * factor);
        }
    }

    private void startScan() {
        bus.post(new RequestBeaconScanEvent(persistentState.getSelectedRegion()));
        if (persistentState.getSelectedRegion() != null) {
            handler.post(toneRunner);
        }
    }

    private void stopSound() {
        handler.removeCallbacks(toneRunner);
    }
}




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