Question

I'm trying to access the variable "adapter" declared in the onCreate section in the onCreateView section. It's a List. Every time I check the contents outside the onCreate it's empty.

Here is the code

package com.example.beacon;

import java.util.List;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;

import android.app.Fragment;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentBeacon extends Fragment {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
    private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId",
            ESTIMOTE_PROXIMITY_UUID, null, null);

    private BeaconManager beaconManager;
    public LeDeviceListAdapter adapter = null; //<---- this variable

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new LeDeviceListAdapter(getActivity());
        beaconManager = new BeaconManager(getActivity());

        beaconManager.setRangingListener(new BeaconManager.RangingListener() {
            @Override
            public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
                Log.d(TAG, "Ranged beacons FB_onCreate: " + beacons);
                adapter.replaceWith(beacons); //<---- this variable
                Log.d(TAG, "adapters found: " + adapter.getCount());
            }
        });
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {
                    beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
                } catch (RemoteException e) {
                    Log.e(TAG, "Cannot start ranging", e);
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {
                    beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
                } catch (RemoteException e) {
                    Log.e(TAG, "Cannot start ranging", e);
                }
            }
        });

        Log.d(TAG, "Adapters found2: " + adapter.getCount()); //<---- this variable
        View rootView = inflater.inflate(R.layout.fragment_beacon, container,
                false);

        TextView[] pairs = new TextView[adapter.getCount()]; //<---- this variable
        for (int l = 0; l < adapter.getCount(); l++) {
            pairs[l] = new TextView(getActivity());
            pairs[l].setTextSize(15);

            pairs[l].setId(l);
            pairs[l].setText((l + 1) + ": something");
            ((ViewGroup) rootView).addView(pairs[l]);
        }

        return rootView;
    }
    @Override
    public void onStop() {

        // Should be invoked in #onStop.
        try {
            beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
        } catch (RemoteException e) {
            Log.e(TAG, "Cannot stop but it does not matter now", e);
        }
        super.onStop();
    }

    @Override
    public void onDestroy() {

        // When no longer needed. Should be invoked in #onDestroy.
        beaconManager.disconnect();
        super.onDestroy();
    }

}

When I run it from onBeaconsDiscovered the count is != 0 which is correct. But anywhere else it's always 0.

Is it that I have to use a bundle from inside the same fragment?.

Was it helpful?

Solution

As Sim told: onBeaconsDiscovered is being run asynchronously. To be precise it is invoked by default once per second (according to Estimote SDK docs).

In onCreateView this onBeaconsDiscovered wasn't yet called and that's why you see count == 0. You need to update UI (or construct UI elements) from onBeaconsDiscovered method.

OTHER TIPS

The onBeaconsDiscovered method will run asynchronous and most probably after the onCreateView was called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top