2

I'm working on a project where I collect 4 categories of sensor data via bluetooth low energy and display this data within a tabbed swipe view in individual fragments. Currently I have the BLE functionality fully working, and am receiving values as expected. However, after implementing a Fragment class, a FragmentPagerAdapter class, and the MainActivity, my graphs are not updating with the incoming data.

Right now I'm having trouble determining if I'm not referencing the fragment correctly or if I implemented the GraphView functionality incorrectly. I've tried referencing the GraphView documentation to no avail. Additionally, a TextView within the fragment is updating as expected, it's just that the GraphView never draws any data.

DemoFragment class:

public class DemoFragment extends Fragment {
    private TextView textView;
    private LineGraphSeries<DataPoint> series;
    private GraphView graph;

    private int position;

    //UpdateSessionData updateIt;

    /*public DemoFragment() {
        // Required empty public constructor
    }*/

    public static DemoFragment newInstance(int pos){
        Bundle args = new Bundle();
        args.putInt("position", pos);
        DemoFragment fragment = new DemoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        position = getArguments().getInt("position");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_demo, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        graph = (GraphView) view.findViewById(R.id.graph);
        textView = view.findViewById(R.id.classification_txt);

        graph.getViewport().setScalable(true);
        graph.getViewport().setScalableY(true);
        graph.getViewport().setScrollable(true);
        graph.getViewport().setScrollableY(true);

        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMaxY(4095);
        graph.getViewport().setMinY(0);
        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setMaxX(100);
        graph.getViewport().setMinX(0);

        series = new LineGraphSeries<DataPoint>();
        graph.addSeries(series);

        textView.setText("Classification: " + position);
    }

    public void addDataPoint(DataPoint point){
        series.appendData(point, true, 10000);
        graph.refreshDrawableState();
    }

    /*interface UpdateSessionData{
        void updateFragment(int x, int y);
    }*/
}

ViewPagerAdapter class:

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private String tabTitles[] = new String[] { "O2", "GSR", "HR", "EMG"};

    public ViewPagerAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public Fragment getItem(int position){
        return DemoFragment.newInstance(position + 1);
    }

    @Override
    public int getCount(){
        return 4;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position){
        return tabTitles[position];
    }
}

Relevant MainActivity code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    o2frag = DemoFragment.newInstance(0);
    ft.add(R.id.pager, o2frag, "o2");
    gsrFrag = DemoFragment.newInstance(1);
    ft.add(R.id.pager, gsrFrag, "gsr");
    hrFrag = DemoFragment.newInstance(2);
    ft.add(R.id.pager, hrFrag, "hr");
    emgFrag = DemoFragment.newInstance(3);
    ft.add(R.id.pager, emgFrag, "emg");
    ft.show(emgFrag);
    ft.commit();

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                displayFragment(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    protected void displayFragment(int fragPosition){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        DemoFragment fragment = (DemoFragment) adapter.getItem(fragPosition);
        ft.show(fragment);
        ft.commit();

}

    @Override
        public void onCharacteristicWriteRequest(BluetoothDevice device,
                                                 int requestId,
                                                 BluetoothGattCharacteristic characteristic,
                                                 boolean preparedWrite,
                                                 boolean responseNeeded,
                                                 int offset,
                                                 final byte[] value) {
            super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
            Log.i(TAG, "onCharacteristicWriteRequest "+characteristic.getUuid().toString());

            if (UARTProfile.RX_WRITE_CHAR.equals(characteristic.getUuid())) {

                //IMP: Copy the received value to storage
                storage = value;
                if (responseNeeded) {
                    mGattServer.sendResponse(device,
                            requestId,
                            BluetoothGatt.GATT_SUCCESS,
                            0,
                            value);
                    Log.d(TAG, "Received  data on "+characteristic.getUuid().toString());
                    Log.d(TAG, "Received data "+ bytesToHex(value));

                }

                //IMP: Respond
                sendOurResponse();

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //Toast.makeText(LoginActivity.this, "We received data", Toast.LENGTH_SHORT).show();
                        //Toast.makeText(SessionActivity.this, "We received data: " + bytesToHex(value), Toast.LENGTH_SHORT).show();

                        short [] dataArray = byteArrayToShortArray(value);
                        Toast.makeText(SwipeDataActivity.this, "We received data: " + Arrays.toString(dataArray), Toast.LENGTH_SHORT).show();
                        currentTime = SystemClock.elapsedRealtime();
                        long elapsedTime = currentTime - startTime;
                        DataPoint [] points = new DataPoint[4];
                        for(int i = 0; i < dataArray.length; i++){
                            points[i] = new DataPoint(currentTime, (long) dataArray[i]);
                        }
                        o2frag.addDataPoint(points[0]);
                        gsrFrag.addDataPoint(points[1]);
                        hrFrag.addDataPoint(points[2]);
                        emgFrag.addDataPoint(points[3]);
                    }
                });
            }
        }

Any help in understanding why the GraphView isn't updating would be greatly appreciated! Thank you in advance for your time, I'm sorry for the large blocks of code but I wanted to make sure the relevant information was available.

0 Answers0